博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VC++实现android的Toast消息框的功能
阅读量:4123 次
发布时间:2019-05-25

本文共 2162 字,大约阅读时间需要 7 分钟。

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_FILEstatic 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_MAPEND_MESSAGE_MAP()/// CMsgBox message handlersvoid 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 oncevoid 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 );

转载地址:http://dnopi.baihongyu.com/

你可能感兴趣的文章
iOS菜鸟学习--如何避免两个按钮同时响应
查看>>
How to access the keys in dictionary in object-c
查看>>
iOS菜鸟学习—— NSSortDescriptor的使用
查看>>
hdu 3787 hdoj 3787
查看>>
hdu 3790 hdoj 3790
查看>>
hdu 3789 hdoj 3789
查看>>
hdu 3788 hdoj 3788
查看>>
zju 1003 zoj 1003
查看>>
zju 1004 zoj 1004
查看>>
zju 1005 zoj 1005
查看>>
zju 1006 zoj 1006
查看>>
【虚拟机】虚拟化架构与系统部署(Windows系统安装)
查看>>
字节跳动安卓开发实习生面试分享
查看>>
好书分享之——《能力陷进》
查看>>
阅读笔记《c++ primer》
查看>>
阅读笔记《C++标准程序库》
查看>>
基于mirror driver的windows屏幕录像
查看>>
C语言8
查看>>
Qt实现简单延时
查看>>
qml有关矩形说明
查看>>