Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #ifndef MYMSGBOX_H_INCLUDED
  2. #define MYMSGBOX_H_INCLUDED
  3.  
  4. #include <windows.h>
  5.  
  6. class MyMsgBox
  7. {
  8. public:
  9. // Constructors
  10. MyMsgBox();
  11. // Destructor
  12. virtual ~MyMsgBox();
  13. // Declare custom message box Show function
  14. /*static*/ int Show(HWND, LPCTSTR, LPCTSTR, UINT, LPLOGFONT = NULL, COLORREF = 0, WORD = 0, WORD = 0);
  15.  
  16. protected:
  17. // Declare custom message box procedure
  18. static LRESULT CALLBACK MyMsgBoxProc(HWND, UINT, WPARAM, LPARAM);
  19.  
  20. public:
  21. // Member variables
  22. // Keeps data along custom message box functions and the hook procedure
  23. static HWND m_hWnd; // message box handle
  24. CHAR m_lpszText[1024]; // message box text
  25. CHAR m_lpszCaption[1024]; // message box caption
  26. UINT m_uType; // message box style
  27. LPLOGFONT m_lpLogFont; // font info pointer
  28. COLORREF m_crText; // text color
  29. WORD m_wIconRes; // icon resource id
  30. WORD m_wBmpRes; // bitmap resource id
  31. static HHOOK m_hHook; // hook handle
  32. HBRUSH m_hBrush; // background brush
  33. HBITMAP m_hBitmap; // handle to background bitmap
  34. HFONT m_hTextfont; // text font handle
  35. HFONT m_hButtonfont; // button font handle
  36. static WNDPROC m_lpMsgBoxProc; // window procedure
  37.  
  38. private:
  39. // Declare CBT hook procedure
  40. static LRESULT CALLBACK CbtHookProc(int, WPARAM, LPARAM);
  41. };
  42.  
  43. #endif // MYMSGBOX_H_INCLUDED
  44.  
  45. MyMsgBox::MyMsgBox()
  46. {
  47. //m_hWnd = NULL;
  48. ::ZeroMemory(&m_lpszText, sizeof(m_lpszText));
  49. ::ZeroMemory(&m_lpszCaption, sizeof(m_lpszCaption));
  50. m_uType = 0;
  51. m_lpLogFont = NULL;
  52. m_crText = 0;
  53. m_wIconRes = 0;
  54. m_wBmpRes = 0;
  55. //m_hHook = NULL;
  56. m_hBrush = NULL;
  57. m_hBitmap = NULL;
  58. m_hTextfont = NULL;
  59. m_hButtonfont = NULL;
  60. //m_lpMsgBoxProc = NULL;
  61. }
  62.  
  63. // Destructor
  64. MyMsgBox::~MyMsgBox()
  65. {
  66. }
  67.  
  68. // Custom message box Show function 2
  69. // Call this instead of MessageBox API function in order to display
  70. // a customized message box.
  71. int MyMsgBox::Show(HWND hWnd, LPCTSTR lpszText, LPCTSTR lpszCaption, UINT uType,
  72. LPLOGFONT lpLogFont /*= NULL*/, COLORREF crText /*= 0*/,
  73. WORD wIconRes /*= 0*/, WORD wBmpRes /*= 0*/)
  74. {
  75. // init MyMsgBoxValues structure values
  76. //::ZeroMemory(&cmbv, sizeof(MyMsgBoxValues));
  77.  
  78. HINSTANCE hInstance = ::GetModuleHandle(NULL);
  79. // installs the hook procedure into a hook chain
  80. m_hHook = ::SetWindowsHookEx(WH_CBT, CbtHookProc, hInstance, ::GetCurrentThreadId());
  81. // set the LogFont variable
  82. m_lpLogFont = lpLogFont;
  83. // set the static text control color
  84. m_crText = crText;
  85. // set the icon res id
  86. m_wIconRes = wIconRes;
  87. // set the background bitmap res id
  88. m_wBmpRes = wBmpRes;
  89.  
  90. // call "standard" MessageBox Windows API function
  91. int nRet;
  92.  
  93. if (!wIconRes)
  94. nRet = ::MessageBox(hWnd, lpszText, lpszCaption, uType);
  95. else
  96. {
  97. MSGBOXPARAMS msgBoxParam;
  98. ::ZeroMemory(&msgBoxParam, sizeof(MSGBOXPARAMS));
  99. msgBoxParam.cbSize = sizeof(MSGBOXPARAMS);
  100. msgBoxParam.dwStyle = MB_USERICON | uType;
  101. msgBoxParam.hInstance = hInstance;
  102. msgBoxParam.hwndOwner = hWnd;
  103. msgBoxParam.lpszCaption = lpszCaption;
  104. msgBoxParam.lpszIcon = MAKEINTRESOURCE(wIconRes);
  105. msgBoxParam.lpszText = lpszText;
  106. nRet = ::MessageBoxIndirect(&msgBoxParam);
  107. }
  108.  
  109. // removes the hook procedure from the hook chain
  110. ::UnhookWindowsHookEx(m_hHook);
  111.  
  112. return nRet;
  113. }
  114.  
  115. // CBT (computer-based training) hook procedure
  116. // Catch the dialog box creation in order to subclass it
  117. LRESULT CALLBACK MyMsgBox::CbtHookProc(int nCode, WPARAM wParam, LPARAM lParam)
  118. {
  119. if (nCode < 0)
  120. return ::CallNextHookEx(m_hHook, nCode, wParam, lParam);
  121.  
  122. switch (nCode)
  123. {
  124. case HCBT_CREATEWND: // a window is about to be created
  125. {
  126. LPCBT_CREATEWND lpCbtCreate = (LPCBT_CREATEWND)lParam;
  127.  
  128. if (lpCbtCreate->lpcs->lpszClass == WC_DIALOG)
  129. // WC_DIALOG is the class name of message box
  130. // but it has not yet a window procedure set.
  131. // So keep in mind the handle to subclass it later
  132. // when its first child is about to be created
  133. m_hWnd = (HWND)wParam;
  134. else if ((m_lpMsgBoxProc == NULL) && (m_hWnd != NULL))
  135. // subclass the dialog
  136. m_lpMsgBoxProc = (WNDPROC)::SetWindowLong(m_hWnd, GWL_WNDPROC, (LONG)MyMsgBoxProc);
  137. }
  138. break;
  139.  
  140. case HCBT_DESTROYWND: // a window is about to be destroyed
  141. {
  142. if ((HWND)wParam == m_hWnd) // it's our message box
  143. // so set back its default procedure
  144. ::SetWindowLong(m_hWnd, GWL_WNDPROC, (LONG)m_lpMsgBoxProc);
  145. }
  146. break;
  147. }
  148.  
  149. return 0;
  150. }
  151.  
  152. // Custom Message box window procedure
  153. LRESULT CALLBACK MyMsgBox::MyMsgBoxProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  154. {
  155. switch (uMsg)
  156. {
  157. case WM_INITDIALOG:
  158. .
  159. .
  160. .
  161.  
  162. case WM_CTLCOLORSTATIC:
  163. .
  164. .
  165. .
  166.  
  167. case WM_CTLCOLORDLG:
  168. .
  169. .
  170. .
  171.  
  172. case WM_ERASEBKGND:
  173. .
  174. .
  175. .
  176.  
  177. case WM_PAINT:
  178. .
  179. .
  180. .
  181.  
  182. case WM_DESTROY:
  183. .
  184. .
  185. .
  186.  
  187. }
  188.  
  189. return ::CallWindowProc(m_lpMsgBoxProc, hWnd, uMsg, wParam, lParam);
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement