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 );