Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MYMSGBOX_H_INCLUDED
- #define MYMSGBOX_H_INCLUDED
- #include <windows.h>
- class MyMsgBox
- {
- public:
- // Constructors
- MyMsgBox();
- // Destructor
- virtual ~MyMsgBox();
- // Declare custom message box Show function
- /*static*/ int Show(HWND, LPCTSTR, LPCTSTR, UINT, LPLOGFONT = NULL, COLORREF = 0, WORD = 0, WORD = 0);
- protected:
- // Declare custom message box procedure
- static LRESULT CALLBACK MyMsgBoxProc(HWND, UINT, WPARAM, LPARAM);
- public:
- // Member variables
- // Keeps data along custom message box functions and the hook procedure
- static HWND m_hWnd; // message box handle
- CHAR m_lpszText[1024]; // message box text
- CHAR m_lpszCaption[1024]; // message box caption
- UINT m_uType; // message box style
- LPLOGFONT m_lpLogFont; // font info pointer
- COLORREF m_crText; // text color
- WORD m_wIconRes; // icon resource id
- WORD m_wBmpRes; // bitmap resource id
- static HHOOK m_hHook; // hook handle
- HBRUSH m_hBrush; // background brush
- HBITMAP m_hBitmap; // handle to background bitmap
- HFONT m_hTextfont; // text font handle
- HFONT m_hButtonfont; // button font handle
- static WNDPROC m_lpMsgBoxProc; // window procedure
- private:
- // Declare CBT hook procedure
- static LRESULT CALLBACK CbtHookProc(int, WPARAM, LPARAM);
- };
- #endif // MYMSGBOX_H_INCLUDED
- MyMsgBox::MyMsgBox()
- {
- //m_hWnd = NULL;
- ::ZeroMemory(&m_lpszText, sizeof(m_lpszText));
- ::ZeroMemory(&m_lpszCaption, sizeof(m_lpszCaption));
- m_uType = 0;
- m_lpLogFont = NULL;
- m_crText = 0;
- m_wIconRes = 0;
- m_wBmpRes = 0;
- //m_hHook = NULL;
- m_hBrush = NULL;
- m_hBitmap = NULL;
- m_hTextfont = NULL;
- m_hButtonfont = NULL;
- //m_lpMsgBoxProc = NULL;
- }
- // Destructor
- MyMsgBox::~MyMsgBox()
- {
- }
- // Custom message box Show function 2
- // Call this instead of MessageBox API function in order to display
- // a customized message box.
- int MyMsgBox::Show(HWND hWnd, LPCTSTR lpszText, LPCTSTR lpszCaption, UINT uType,
- LPLOGFONT lpLogFont /*= NULL*/, COLORREF crText /*= 0*/,
- WORD wIconRes /*= 0*/, WORD wBmpRes /*= 0*/)
- {
- // init MyMsgBoxValues structure values
- //::ZeroMemory(&cmbv, sizeof(MyMsgBoxValues));
- HINSTANCE hInstance = ::GetModuleHandle(NULL);
- // installs the hook procedure into a hook chain
- m_hHook = ::SetWindowsHookEx(WH_CBT, CbtHookProc, hInstance, ::GetCurrentThreadId());
- // set the LogFont variable
- m_lpLogFont = lpLogFont;
- // set the static text control color
- m_crText = crText;
- // set the icon res id
- m_wIconRes = wIconRes;
- // set the background bitmap res id
- m_wBmpRes = wBmpRes;
- // call "standard" MessageBox Windows API function
- int nRet;
- if (!wIconRes)
- nRet = ::MessageBox(hWnd, lpszText, lpszCaption, uType);
- else
- {
- MSGBOXPARAMS msgBoxParam;
- ::ZeroMemory(&msgBoxParam, sizeof(MSGBOXPARAMS));
- msgBoxParam.cbSize = sizeof(MSGBOXPARAMS);
- msgBoxParam.dwStyle = MB_USERICON | uType;
- msgBoxParam.hInstance = hInstance;
- msgBoxParam.hwndOwner = hWnd;
- msgBoxParam.lpszCaption = lpszCaption;
- msgBoxParam.lpszIcon = MAKEINTRESOURCE(wIconRes);
- msgBoxParam.lpszText = lpszText;
- nRet = ::MessageBoxIndirect(&msgBoxParam);
- }
- // removes the hook procedure from the hook chain
- ::UnhookWindowsHookEx(m_hHook);
- return nRet;
- }
- // CBT (computer-based training) hook procedure
- // Catch the dialog box creation in order to subclass it
- LRESULT CALLBACK MyMsgBox::CbtHookProc(int nCode, WPARAM wParam, LPARAM lParam)
- {
- if (nCode < 0)
- return ::CallNextHookEx(m_hHook, nCode, wParam, lParam);
- switch (nCode)
- {
- case HCBT_CREATEWND: // a window is about to be created
- {
- LPCBT_CREATEWND lpCbtCreate = (LPCBT_CREATEWND)lParam;
- if (lpCbtCreate->lpcs->lpszClass == WC_DIALOG)
- // WC_DIALOG is the class name of message box
- // but it has not yet a window procedure set.
- // So keep in mind the handle to subclass it later
- // when its first child is about to be created
- m_hWnd = (HWND)wParam;
- else if ((m_lpMsgBoxProc == NULL) && (m_hWnd != NULL))
- // subclass the dialog
- m_lpMsgBoxProc = (WNDPROC)::SetWindowLong(m_hWnd, GWL_WNDPROC, (LONG)MyMsgBoxProc);
- }
- break;
- case HCBT_DESTROYWND: // a window is about to be destroyed
- {
- if ((HWND)wParam == m_hWnd) // it's our message box
- // so set back its default procedure
- ::SetWindowLong(m_hWnd, GWL_WNDPROC, (LONG)m_lpMsgBoxProc);
- }
- break;
- }
- return 0;
- }
- // Custom Message box window procedure
- LRESULT CALLBACK MyMsgBox::MyMsgBoxProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_INITDIALOG:
- .
- .
- .
- case WM_CTLCOLORSTATIC:
- .
- .
- .
- case WM_CTLCOLORDLG:
- .
- .
- .
- case WM_ERASEBKGND:
- .
- .
- .
- case WM_PAINT:
- .
- .
- .
- case WM_DESTROY:
- .
- .
- .
- }
- return ::CallWindowProc(m_lpMsgBoxProc, hWnd, uMsg, wParam, lParam);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement