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

转自:https://blog.csdn.net/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. //Functions & other definitions required-->
  4. typedef int (__stdcall *MSGBOXAAPI)(IN HWND hWnd,
  5. IN LPCSTR lpText, IN LPCSTR lpCaption,
  6. IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);
  7. typedef int (__stdcall *MSGBOXWAPI)(IN HWND hWnd,
  8. IN LPCWSTR lpText, IN LPCWSTR lpCaption,
  9. IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);
  10. int MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText,
  11. IN LPCSTR lpCaption, IN UINT uType,
  12. IN WORD wLanguageId, IN DWORD dwMilliseconds);
  13. int MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText,
  14. IN LPCWSTR lpCaption, IN UINT uType,
  15. IN WORD wLanguageId, IN DWORD dwMilliseconds);
  16. #ifdef UNICODE
  17. #define MessageBoxTimeout MessageBoxTimeoutW
  18. #else
  19. #define MessageBoxTimeout MessageBoxTimeoutA
  20. #endif
  21. #define MB_TIMEDOUT 32000
  22. int MessageBoxTimeoutA(HWND hWnd, LPCSTR lpText,
  23. LPCSTR lpCaption, UINT uType, WORD wLanguageId,
  24. DWORD dwMilliseconds)
  25. {
  26. static MSGBOXAAPI MsgBoxTOA = NULL;
  27. if (!MsgBoxTOA)
  28. {
  29. HMODULE hUser32 = GetModuleHandle(_T( "user32.dll"));
  30. if (hUser32)
  31. {
  32. MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32,
  33. "MessageBoxTimeoutA");
  34. //fall through to 'if (MsgBoxTOA)...'
  35. }
  36. else
  37. {
  38. //stuff happened, add code to handle it here
  39. //(possibly just call MessageBox())
  40. return 0;
  41. }
  42. }
  43. if (MsgBoxTOA)
  44. {
  45. return MsgBoxTOA(hWnd, lpText, lpCaption,
  46. uType, wLanguageId, dwMilliseconds);
  47. }
  48. return 0;
  49. }
  50. int MessageBoxTimeoutW(HWND hWnd, LPCWSTR lpText,
  51. LPCWSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds)
  52. {
  53. static MSGBOXWAPI MsgBoxTOW = NULL;
  54. if (!MsgBoxTOW)
  55. {
  56. HMODULE hUser32 = GetModuleHandle(_T( "user32.dll"));
  57. if (hUser32)
  58. {
  59. MsgBoxTOW = (MSGBOXWAPI)GetProcAddress(hUser32,
  60. "MessageBoxTimeoutW");
  61. //fall through to 'if (MsgBoxTOW)...'
  62. }
  63. else
  64. {
  65. //stuff happened, add code to handle it here
  66. //(possibly just call MessageBox())
  67. return 0;
  68. }
  69. }
  70. if (MsgBoxTOW)
  71. {
  72. return MsgBoxTOW(hWnd, lpText, lpCaption,
  73. uType, wLanguageId, dwMilliseconds);
  74. }
  75. return 0;
  76. }
  77. //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. if (hUser32)
  7. {
  8. int iRet = 0;
  9. UINT uiFlags = MB_OK | MB_SETFOREGROUND | MB_SYSTEMMODAL | MB_ICONINFORMATION;
  10. iRet = MessageBoxTimeout( NULL, _T( "Test a timeout of 2 seconds."),
  11. _T( "MessageBoxTimeout Test"), uiFlags, 0, 2000);
  12. //iRet will = 1
  13. uiFlags = MB_YESNO | MB_SETFOREGROUND | MB_SYSTEMMODAL | MB_ICONINFORMATION;
  14. iRet = MessageBoxTimeout( NULL, _T( "Test a timeout of 5 seconds."),
  15. _T( "MessageBoxTimeout Test"), uiFlags, 0, 5000);
  16. //iRet will = MB_TIMEDOUT if no buttons pressed, button values otherwise
  17. //only unload user32.dll when you have no further need
  18. //for the MessageBoxTimeout function
  19. FreeLibrary(hUser32);
  20. }
  21. }


参考网址:http://blog.csdn.net/a379039233/article/details/49445207

文章标签:  VS2013 c++ mfc 微软 windows

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. //Functions & other definitions required-->
  4. typedef int (__stdcall *MSGBOXAAPI)(IN HWND hWnd,
  5. IN LPCSTR lpText, IN LPCSTR lpCaption,
  6. IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);
  7. typedef int (__stdcall *MSGBOXWAPI)(IN HWND hWnd,
  8. IN LPCWSTR lpText, IN LPCWSTR lpCaption,
  9. IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);
  10. int MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText,
  11. IN LPCSTR lpCaption, IN UINT uType,
  12. IN WORD wLanguageId, IN DWORD dwMilliseconds);
  13. int MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText,
  14. IN LPCWSTR lpCaption, IN UINT uType,
  15. IN WORD wLanguageId, IN DWORD dwMilliseconds);
  16. #ifdef UNICODE
  17. #define MessageBoxTimeout MessageBoxTimeoutW
  18. #else
  19. #define MessageBoxTimeout MessageBoxTimeoutA
  20. #endif
  21. #define MB_TIMEDOUT 32000
  22. int MessageBoxTimeoutA(HWND hWnd, LPCSTR lpText,
  23. LPCSTR lpCaption, UINT uType, WORD wLanguageId,
  24. DWORD dwMilliseconds)
  25. {
  26. static MSGBOXAAPI MsgBoxTOA = NULL;
  27. if (!MsgBoxTOA)
  28. {
  29. HMODULE hUser32 = GetModuleHandle(_T( "user32.dll"));
  30. if (hUser32)
  31. {
  32. MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32,
  33. "MessageBoxTimeoutA");
  34. //fall through to 'if (MsgBoxTOA)...'
  35. }
  36. else
  37. {
  38. //stuff happened, add code to handle it here
  39. //(possibly just call MessageBox())
  40. return 0;
  41. }
  42. }
  43. if (MsgBoxTOA)
  44. {
  45. return MsgBoxTOA(hWnd, lpText, lpCaption,
  46. uType, wLanguageId, dwMilliseconds);
  47. }
  48. return 0;
  49. }
  50. int MessageBoxTimeoutW(HWND hWnd, LPCWSTR lpText,
  51. LPCWSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds)
  52. {
  53. static MSGBOXWAPI MsgBoxTOW = NULL;
  54. if (!MsgBoxTOW)
  55. {
  56. HMODULE hUser32 = GetModuleHandle(_T( "user32.dll"));
  57. if (hUser32)
  58. {
  59. MsgBoxTOW = (MSGBOXWAPI)GetProcAddress(hUser32,
  60. "MessageBoxTimeoutW");
  61. //fall through to 'if (MsgBoxTOW)...'
  62. }
  63. else
  64. {
  65. //stuff happened, add code to handle it here
  66. //(possibly just call MessageBox())
  67. return 0;
  68. }
  69. }
  70. if (MsgBoxTOW)
  71. {
  72. return MsgBoxTOW(hWnd, lpText, lpCaption,
  73. uType, wLanguageId, dwMilliseconds);
  74. }
  75. return 0;
  76. }
  77. //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. if (hUser32)
  7. {
  8. int iRet = 0;
  9. UINT uiFlags = MB_OK | MB_SETFOREGROUND | MB_SYSTEMMODAL | MB_ICONINFORMATION;
  10. iRet = MessageBoxTimeout( NULL, _T( "Test a timeout of 2 seconds."),
  11. _T( "MessageBoxTimeout Test"), uiFlags, 0, 2000);
  12. //iRet will = 1
  13. uiFlags = MB_YESNO | MB_SETFOREGROUND | MB_SYSTEMMODAL | MB_ICONINFORMATION;
  14. iRet = MessageBoxTimeout( NULL, _T( "Test a timeout of 5 seconds."),
  15. _T( "MessageBoxTimeout Test"), uiFlags, 0, 5000);
  16. //iRet will = MB_TIMEDOUT if no buttons pressed, button values otherwise
  17. //only unload user32.dll when you have no further need
  18. //for the MessageBoxTimeout function
  19. FreeLibrary(hUser32);
  20. }
  21. }


参考网址:http://blog.csdn.net/a379039233/article/details/49445207

猜你喜欢

转载自blog.csdn.net/m0_37290785/article/details/80923508
今日推荐