关于弹出消息窗口的自动关闭

转自:https://blog.youkuaiyun.com/saiolive/article/details/51907954

 

1.方法1

1.1. 直接在代码中添加

 

 
  1. // 弹出消息窗口自动关闭,需要指出的是,Windows 2000的user32.dll没有导出这个函数。

  2. extern "C"

  3. {

  4. int WINAPI MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);

  5. int WINAPI MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText, IN LPCWSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);

  6. };

  7. #ifdef UNICODE

  8. #define MessageBoxTimeout MessageBoxTimeoutW

  9. #else

  10. #define MessageBoxTimeout MessageBoxTimeoutA

  11. #endif

 

 

1.2. 调用

 

 
  1. MessageBoxTimeout(this->GetSafeHwnd(), _T("弹出5秒后会自动关闭!这是一个模态对话框。"), _T("会自动关闭的MessageBox"), MB_ICONINFORMATION, GetSystemDefaultLangID(), 5000);

  2. //参数说明:父窗口的句柄为NULL的情况下,将弹出非模态对话框;延时关闭的时间为0的情况下,弹出的MessageBox需要手动关闭

  3. MessageBoxTimeout(NULL, _T("弹出5秒后会自动关闭!这是一个非模态对话框。"), _T("会自动关闭的MessageBox"), MB_ICONINFORMATION, GetSystemDefaultLangID(), 5000);



参考网址:http://blog.sina.com.cn/s/blog_4b0f3b420100mglb.html

 

 

win7 x86,vs2013,测试通过

 

2.方法2

 

2.1.此api是微软的一个未公开api,在user32.dll中,功能就是弹出一个对话框MessageBox,并定时自动退出。 
下面为头文件,随便取个名字,我取的是MsgBoxTimeout.h 。

 

 
  1. #include <windows.h>

  2. #include <tchar.h>

  3.  
  4. //Functions & other definitions required-->

  5. typedef int (__stdcall *MSGBOXAAPI)(IN HWND hWnd,

  6. IN LPCSTR lpText, IN LPCSTR lpCaption,

  7. IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);

  8. typedef int (__stdcall *MSGBOXWAPI)(IN HWND hWnd,

  9. IN LPCWSTR lpText, IN LPCWSTR lpCaption,

  10. IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);

  11.  
  12. int MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText,

  13. IN LPCSTR lpCaption, IN UINT uType,

  14. IN WORD wLanguageId, IN DWORD dwMilliseconds);

  15. int MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText,

  16. IN LPCWSTR lpCaption, IN UINT uType,

  17. IN WORD wLanguageId, IN DWORD dwMilliseconds);

  18.  
  19. #ifdef UNICODE

  20. #define MessageBoxTimeout MessageBoxTimeoutW

  21. #else

  22. #define MessageBoxTimeout MessageBoxTimeoutA

  23. #endif

  24.  
  25. #define MB_TIMEDOUT 32000

  26.  
  27. int MessageBoxTimeoutA(HWND hWnd, LPCSTR lpText,

  28. LPCSTR lpCaption, UINT uType, WORD wLanguageId,

  29. DWORD dwMilliseconds)

  30. {

  31. static MSGBOXAAPI MsgBoxTOA = NULL;

  32.  
  33. if (!MsgBoxTOA)

  34. {

  35. HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));

  36. if (hUser32)

  37. {

  38. MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32,

  39. "MessageBoxTimeoutA");

  40. //fall through to 'if (MsgBoxTOA)...'

  41. }

  42. else

  43. {

  44. //stuff happened, add code to handle it here

  45. //(possibly just call MessageBox())

  46. return 0;

  47. }

  48. }

  49.  
  50. if (MsgBoxTOA)

  51. {

  52. return MsgBoxTOA(hWnd, lpText, lpCaption,

  53. uType, wLanguageId, dwMilliseconds);

  54. }

  55.  
  56. return 0;

  57. }

  58.  
  59. int MessageBoxTimeoutW(HWND hWnd, LPCWSTR lpText,

  60. LPCWSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds)

  61. {

  62. static MSGBOXWAPI MsgBoxTOW = NULL;

  63.  
  64. if (!MsgBoxTOW)

  65. {

  66. HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));

  67. if (hUser32)

  68. {

  69. MsgBoxTOW = (MSGBOXWAPI)GetProcAddress(hUser32,

  70. "MessageBoxTimeoutW");

  71. //fall through to 'if (MsgBoxTOW)...'

  72. }

  73. else

  74. {

  75. //stuff happened, add code to handle it here

  76. //(possibly just call MessageBox())

  77. return 0;

  78. }

  79. }

  80.  
  81. if (MsgBoxTOW)

  82. {

  83. return MsgBoxTOW(hWnd, lpText, lpCaption,

  84. uType, wLanguageId, dwMilliseconds);

  85. }

  86.  
  87. return 0;

  88. }

  89. //End required definitions <--


2.2. 调用方式

 

 

 
  1. #include "MsgBoxTimeout.h"

  2. void CTestDlg::OnBnClickedButton2()

  3. {

  4. //you must load user32.dll before calling the function

  5. HMODULE hUser32 = LoadLibrary(_T("user32.dll"));

  6.  
  7. if (hUser32)

  8. {

  9. int iRet = 0;

  10. UINT uiFlags = MB_OK | MB_SETFOREGROUND | MB_SYSTEMMODAL | MB_ICONINFORMATION;

  11.  
  12. iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 2 seconds."),

  13. _T("MessageBoxTimeout Test"), uiFlags, 0, 2000);

  14. //iRet will = 1

  15.  
  16. uiFlags = MB_YESNO | MB_SETFOREGROUND | MB_SYSTEMMODAL | MB_ICONINFORMATION;

  17.  
  18. iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 5 seconds."),

  19. _T("MessageBoxTimeout Test"), uiFlags, 0, 5000);

  20. //iRet will = MB_TIMEDOUT if no buttons pressed, button values otherwise

  21.  
  22. //only unload user32.dll when you have no further need

  23. //for the MessageBoxTimeout function

  24. FreeLibrary(hUser32);

  25. }

  26. }

 

 

参考网址:http://blog.youkuaiyun.com/a379039233/article/details/49445207

文章标签: VS2013c++mfc微软windows

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值