Click here to Skip to main content
12,503,591 members (52,597 online)
Click here to Skip to main content
Add your own
alternative version

Stats

86.4K views
4.7K downloads
67 bookmarked
Posted

How to change the MessageBox window (Add controls and change texts)?

, 6 Apr 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes how to change the MessageBox window (Add controls and change texts).

Sample Image - dMsgBox.jpg

Introduction

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:

  • Use 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,
                               (HOOKPROC)SetHook,
                               NULL,
                               GetCurrentThreadId ());

Where 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)
{
if (nCode==HC_ACTION)
{
    CWPSTRUCT* pwp = (CWPSTRUCT*)lParam;
    
    if (pwp->message==WM_INITDIALOG)
        
    oldProc=(WNDPROC)SetWindowLong(pwp>hwnd,
GWL_WNDPROC,(LONG)HookWndProc); 
}
return CallNextHookEx(g_hHook,nCode,wParam,lParam);
}

This code snippet is my SetHook procedure.

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.

And the HookWndProc is a function that processes the window creation codes.

Handling the window creation process

To do this, you must declare a CALLBACK function.

LRESULT CALLBACK HookWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

This is my HookWndProc procedure.

LRESULT CALLBACK HookWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    //Let OS to perform basic operation
    LRESULT rc = CallWindowProc( oldProc, hWnd, uMsg, wParam, lParam );

    //If MessageBox wanted to initialized, do something
    if (uMsg==WM_INITDIALOG)
    {
        if (m_hIcon)
            SetIcon(hWnd,m_hIcon);
            CreateButton(hWnd);    

        if (m_strCaption)
            SetCaption(hWnd);
    }
    if (uMsg==WM_COMMAND&&wParam==IDC_CHECKBOX)
        //If user clicked the check box handle it!
        SetOut(wParam);
    if (uMsg==WM_NCDESTROY)
        //On exit Uninstall the hook procedure
        //If we don't do that every message box will have 
        //A check box on it!
        UnInstallHook(g_hHook);
return rc;
}

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 CallWindowProc function.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

dSolariuM
Web Developer
United States United States
I started Visual C++ on 2003 and I am a beginner now,I am working on a Network program and I am searching for some partners.I am intrested in DLLs and GUI in programing.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionHow do I change the background color of AfxMessageBox and Open/Save dialogs? Pin
kajitams27-Jan-07 6:35
memberkajitams27-Jan-07 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 6 Apr 2005
Article Copyright 2005 by dSolariuM
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid