未公开函数MessageBoxTimeOut 实现定时消息(ZT) MFC实现MessageBox自动消失

http://www.blogjava.net/baicker/archive/2007/07/13/130072.html

  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 <--
 90 //Call the function as follows:
 91 
 92 // Collapse
 93 //you must load user32.dll before calling the function
 94 main()
 95 {
 96     HMODULE hUser32 = LoadLibrary(_T("user32.dll"));
 97 
 98     if (hUser32)
 99     {
100          int iRet = 0;
101          UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
102 
103          iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 2 seconds."),
104                                  _T("MessageBoxTimeout Test"), uiFlags, 0, 2000);
105          //iRet will = 1
106 
107          uiFlags = MB_YESNO|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
108 
109          iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 5 seconds."),
110                                  _T("MessageBoxTimeout Test"), uiFlags, 0, 5000);
111          //iRet will = MB_TIMEDOUT if no buttons pressed, button values otherwise
112 
113          //only unload user32.dll when you have no further need
114          //for the MessageBoxTimeout function
115          FreeLibrary(hUser32);
116     }
117 }

 

 
posted @ 2014-04-16 10:45  youngt  阅读(1425)  评论(0编辑  收藏  举报