vc 接收android消息,VC++实现android的Toast消息框的功能

这篇博客介绍了如何在VC++中自定义一个类似Android的Toast消息框。作者创建了一个名为CToastLabel的类,该类继承自CWnd,并包含一个定时器和CStatic对象。在CToastLabel类中,作者实现了显示和自动关闭消息框的功能,通过设置定时器并在定时器事件中销毁窗口来达到类似Toast的效果。调用方式简洁,只需传入消息文本、显示区域和关闭延迟时间即可。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android的Toast消息框:

通常是显示指定的字符串,三五秒钟之后隐藏消息框。

此对话框功能在android上面用途颇多,当然android上面实现此功能十分简单。

vc则需要自己动手了。

定义一个ToastLabel类,继承自CWnd类。

类包含:一个定时器对象、一个CStatic对象、一个public方法(用以传递参数并启动Toast消息。当然也可以不要,改成在构造函数中传递参数。但是我没有这么做)

public方法中启动定时器,并且New一个CStatic对象,创建对话框。

类的实现如下【类的头文件请自己补全】:

// MsgBox.cpp : implementation file

//

/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h" //Replace with your PCH file

/////////////////////////////////////////////////////////////////////////////

#include "ToastLabel.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CToastLabel, CWnd)

CToastLabel::CToastLabel(CWnd* pParent)

{

// Create a dummpy child window. It gets attached to this CWnd Object

Create(NULL,

"{62BAB41D-B6BB-402C-89EF-5B86830DF68C}",

WS_OVERLAPPED, CRect(0,0,0,0),

pParent,

1000);

m_bChildCreated = TRUE;

m_Caption = _T("");

}

CToastLabel::CToastLabel()

{

m_bChildCreated = FALSE;

m_Caption = _T("");

}

CToastLabel::~CToastLabel()

{

}

BEGIN_MESSAGE_MAP(CToastLabel, CWnd)

//{{AFX_MSG_MAP(CMsgBox)

ON_WM_TIMER()

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMsgBox message handlers

void CToastLabel::OnTimer(UINT nIDEvent)

{

// TODO: Add your message handler code here and/or call default

BOOL bRetVal = false;

if (m_cs->m_hWnd!=NULL)

{

m_cs->DestroyWindow();

}

// Kill the timer

KillTimer(100);

CWnd::OnTimer(nIDEvent);

}

void CToastLabel::MessageBox(CString sMsg,CRect showRegion, UINT nSleep, bool bAutoClose/*Default is close auto */)

{

// Save the caption, for finding this

// message box window later

// If auto close selected then, start the timer.

if(bAutoClose)

SetTimer(100, nSleep, NULL);

// Show the message box

m_cs=new CStatic;

if (m_cs->m_hWnd==NULL)

{

m_cs->Create(sMsg,WS_CHILD | WS_VISIBLE |SS_CENTER,showRegion,AfxGetApp()->GetMainWnd(),ID_SELFDEFINELABEL);

}

}

// This method called only once

void CToastLabel::SetParent(CWnd* pParent)

{

// Create a dummpy child window. It gets attached to this CWnd Object

if(!m_bChildCreated)

{

Create(NULL,

"{62BAB41D-B6BB-402C-89EF-5B86830DF68C}",

WS_OVERLAPPED, CRect(0,0,0,0),

pParent,

1000);

m_bChildCreated = TRUE;

}

}

调用方式如下:

CToastLabel Msg;

Msg.MessageBox("This message box will auto close in 2 sec.",

new CRect(0,0,200,30), 2000 );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值