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 ObjectCreate(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 defaultBOOL bRetVal = false; if (m_cs->m_hWnd!=NULL){m_cs->DestroyWindow();}// Kill the timerKillTimer(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 Objectif(!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 );





更多相关文章

  1. android中能不能new Activity()对象引发的思考
  2. Android探索之路(一)——消息处理机制
  3. Android消息机制的源码理解
  4. android 使用socket与pc传递对象的问题
  5. Android Studio--Android中的消息机制
  6. Android探索之旅 | 面向对象和Java基础

随机推荐

  1. hcidump用法
  2. Android(安卓)TV -1.1- Get Started with
  3. android百度地图
  4. androidstudio加载项目问题
  5. android SharedPreferences初步
  6. Android(安卓)Tabs 中使用listview,mapvie
  7. Building the Android(安卓)JDBC Driver
  8. 【Android动画九章】-帧动画
  9. Android(安卓)Binder IPC
  10. Android(安卓)支持的媒体格式(音频,视频,