본문 바로가기
C & C++/MFC 컨트롤

[Tip] 일정 시간 경과 후 사라지는 MessageBox

by izen8 2011. 4. 14.
반응형

윈도우즈가 설치되면 수많은 Dll 들이 설치가 되는데 대부분의 함수들은 Windows API 문서에 기술이 되지만 그렇지 않은 함수들도 많이 존재한다.
그런 함수들 중 하나로 일정 시간 지나면 스스로 사라지는 메세지박스가 user32.dll에 포함되어 있다.

MessageBoxTimeout 함수는 다음과 같다.

1 int MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText, 
2     IN LPCSTR lpCaption, IN UINT uType, 
3     IN WORD wLanguageId, IN DWORD dwMilliseconds);
4 int MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText, 
5     IN LPCWSTR lpCaption, IN UINT uType, 
6     IN WORD wLanguageId, IN DWORD dwMilliseconds);


이 함수를 이용하기 위해서는 라이브러리를 읽어와서 함수 포인터를 연결해 주어야한다.
 
1 static MSGBOXAAPI MsgBoxTOA = NULL;
2   
3 HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));
4 if (hUser32)
5 {
6     MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32, 
7     "MessageBoxTimeoutA");
8 }


MessageBoxTimeout 함수의 반환값 보통 메세지 박스와 같지만 시간이 경과하여 자동으로 사라질 경우에는 OK 버튼일 경우 1을 넘겨주고 YESNO 같은 선택형일 경우 MB_TIMEOUT(=3200) 값을 넘겨준다.

MessageBoxTimeout 함수를 편하게 사용하도록 정리된 헤더파일


 
01 #pragma once
02   
03 #include <windows.h>
04 #include <tchar.h>
05   
06 typedef int (__stdcall *MSGBOXAAPI)(IN HWND hWnd, 
07         IN LPCSTR lpText, IN LPCSTR lpCaption, 
08         IN UINT uType, IN WORD wLanguageId,
09         IN DWORD dwMilliseconds);
10 typedef int (__stdcall *MSGBOXWAPI)(IN HWND hWnd, 
11         IN LPCWSTR lpText, IN LPCWSTR lpCaption, 
12         IN UINT uType, IN WORD wLanguageId,
13         IN DWORD dwMilliseconds);
14   
15 int MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText, 
16     IN LPCSTR lpCaption, IN UINT uType, 
17     IN WORD wLanguageId, IN DWORD dwMilliseconds);
18 int MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText, 
19     IN LPCWSTR lpCaption, IN UINT uType, 
20     IN WORD wLanguageId, IN DWORD dwMilliseconds);
21   
22 #ifdef UNICODE
23     #define MessageBoxTimeout MessageBoxTimeoutW
24 #else
25     #define MessageBoxTimeout MessageBoxTimeoutA
26 #endif 
27   
28 #define MB_TIMEDOUT 32000
29   
30 int MessageBoxTimeoutA(HWND hWnd, LPCSTR lpText, 
31     LPCSTR lpCaption, UINT uType, WORD wLanguageId, 
32     DWORD dwMilliseconds)
33 {
34     static MSGBOXAAPI MsgBoxTOA = NULL;
35   
36     if (!MsgBoxTOA)
37     {
38         HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));
39         if (hUser32)
40         {
41             MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32, 
42                                       "MessageBoxTimeoutA");
43             //fall through to 'if (MsgBoxTOA)...'
44         }
45         else
46         {
47             //stuff happened, add code to handle it here 
48             //(possibly just call MessageBox())
49             return 0;
50         }
51     }
52   
53     if (MsgBoxTOA)
54     {
55         return MsgBoxTOA(hWnd, lpText, lpCaption, 
56               uType, wLanguageId, dwMilliseconds);
57     }
58   
59     return 0;
60 }
61   
62 int MessageBoxTimeoutW(HWND hWnd, LPCWSTR lpText, 
63     LPCWSTR lpCaption, UINT uType, WORD wLanguageId,
64     DWORD dwMilliseconds)
65 {
66     static MSGBOXWAPI MsgBoxTOW = NULL;
67   
68     if (!MsgBoxTOW)
69     {
70         HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));
71         if (hUser32)
72         {
73             MsgBoxTOW = (MSGBOXWAPI)GetProcAddress(hUser32, 
74                                       "MessageBoxTimeoutW");
75             //fall through to 'if (MsgBoxTOW)...'
76         }
77         else
78         {
79             //stuff happened, add code to handle it here 
80             //(possibly just call MessageBox())
81             return 0;
82         }
83     }
84   
85     if (MsgBoxTOW)
86     {
87         return MsgBoxTOW(hWnd, lpText, lpCaption, 
88                uType, wLanguageId, dwMilliseconds);
89     }
90   
91     return 0;
92 }

실제 함수 사용 예
01 int iRet = 0;
02 UINT uiFlags = MB_OK | MB_SETFOREGROUND |
03                MB_SYSTEMMODAL | MB_ICONINFORMATION;
04   
05 iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 2 seconds."),
06                   _T("MessageBoxTimeout Test"), uiFlags, 0, 2000);
07 //iRet will = 1
08 uiFlags = MB_YESNO | MB_SETFOREGROUND |
09           MB_SYSTEMMODAL | MB_ICONINFORMATION;
10 iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 5 seconds."),
11                   _T("MessageBoxTimeout Test"), uiFlags, 0, 5000);


반응형

댓글