自动关闭MessageBox

自动关闭MessageBox
BY thammadi
 
介绍
 
     我曾看到许多这样的文章,但是都比较复杂,因此我就想我要写一个简单的一点的一个。
 
 
理解CMsgBox 类
 
     CMsgBox是一个实现了自动关闭功能的类。这个类从CWnd类继承。 它提供了一个叫做“MessageBox()”的方法,反过来这个方法调用CWnd::MessageBox()来显示消息对话框。
获得自动关闭的功能非常简单,在CMsgBox::MessageBox()方法中,调用CWnd::MessageBox API之前,我们开启一个定时器(SetTimer()方法)。在OnTimer方法中,我们尝试通过使用窗口名字(caption)查找MessageBox窗口。一旦找到,我们发送WM_CLOSE消息来关闭这个消息窗口。就是这样!
 
void CMsgBox::MessageBox(CString sMsg, CString sCaption, UINT nSleep,  UINT nFlags, bool bAutoClose)
{
    // Save the caption, for finding this
     // message box window later
    m_Caption = sCaption;
      // If auto close then, start the timer.
  if(bAutoClose) SetTimer(100, nSleep, NULL);
  // Show the message box
 CWnd::MessageBox(sMsg, sCaption, nFlags);
}
 
 void CMsgBox::OnTimer(UINT nIDEvent)
{
   // TODO: Add your message handler code here and/or call default
  BOOL bRetVal = false;
 // Find the message box window using the caption
  CWnd* pWnd = FindWindow(NULL, m_Caption);
  if(pWnd != NULL)
 {
    // Send close command to the message box window
   ::PostMessage(pWnd->m_hWnd, WM_CLOSE, 0, 0);
 }
 // Kill the timer
 KillTimer(100);
 CWnd::OnTimer(nIDEvent);
}
 
  使用代码
      将“MsgBox.cpp”和“MsgBox.h”两个文件加入到你的工程中。
      #include “MsgBox.h”在适当的地方
      如下创建CMsgBox对象:
 
CMsgBox obj(this) ;
或者像这样:
CMsgBox obj; obj.SetParent(this);
使用MessageBox()方法显示消息对话框。如果你不需要自动关闭功能,设置bAutoClose参数为false。
 
obj.MessageBox("This message box will auto close in 2 seconds.", "Auto Close Msg Box", 2000, MB_OK | MB_ICONINFORMATION);
 
结 论
那很简单不是吗!这也是我第一次投递,请原谅我的错误。
参 考
delaymessagebox by Nishant Shivkumar
 
 
 
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值