In this article we will learn how to add controls to a MessageBox or how to change its button texts. You can use this method to create (full) Multilanguage programs (Just a suggestion!). There are some ways to do this and I chose the hooking method.
Steps of Hooking a MessageBox
To hook a MessageBox:
SetWindowsHookEx () function with
WH_CALLWIND and pass it to a hookproc.
- Declare a hook procedure.
- In the hookproc use the
SetWindowLong () function to handle the MessageBox window Creation process (Maybe it is a good name for this).
- Declare a
CALLBACK function to handle the MessageBox window creation process.
- Pass the
CALLBACK function as the
new long parameter in the
SetWindowLong () function.
- And at the end, place your codes in the
CALLBACK function (codes for window creation).
SetWindowsHookEx () function
This function installs a hook procedure. Using this function you can hook windows events. In other words when you install a hook procedure you tell the system to notify you when the wanted event is called.
As MSDN says: The
SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.
How to Install a hook procedure using SetWindowsHookEx () function?
g_hHook = SetWindowsHookEx (WH_CALLWNDPROC,
g_hHook is a global variable and the
SetHook is my HookProc;
What is WH_CALLWNDPROC?
Use this parameter to install a hook procedure that monitors messages before the system sends them to the destination window procedure (MSDN).
Declaring a HookProcedure
Use the following declarations in your header file:
LRESULT CALLBACK SetHook(int nCode,WPARAM wParam,LPARAM lParam);
This will declare a hook procedure by name
SetHook. See this code snippet:
LRESULT CALLBACK SetHook(int nCode,WPARAM wParam,LPARAM lParam)
CWPSTRUCT* pwp = (CWPSTRUCT*)lParam;
This code snippet is my
What is GWL_WNDPROC?
This parameter sets a new address for the window procedure. (I think it is enough).
What is CWPSTRUCT?
This is a structure that contains information about window messages like a handle to that window (
hwnd). We pass a pointer of this structure to use the
hwnd member in the callback function.
HookWndProc is a function that processes the window creation codes.
Handling the window creation process
To do this, you must declare a
LRESULT CALLBACK HookWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
This is my
LRESULT CALLBACK HookWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
LRESULT rc = CallWindowProc( oldProc, hWnd, uMsg, wParam, lParam );
The main operations are done. I mean the
hwnd. After having this
hwnd, we can do everything to the MessageBox window. But don’t forget to call