CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    Is there any way to change the background color of a messagebox?

    Here is what I did,
    Code:
    oldHook = ::SetWindowsHookEx(WH_CALLWNDPROCRET, CallWndRetProc, NULL, ::GetCurrentThreadId());
    
    MessageBox(...);
    
    UnhookWindowsHookEx(oldHook);
    Then within the callback function CallWndRetProc, I tried to change the background color of the dialog,
    Code:
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(msg->hwnd, &ps);
    
    ::SetBkColor(hdc, RGB(255, 0, 0));
    
    EndPaint(msg->hwnd, &ps);
    But it doesn't change the background color of the dialog. I wonder what is the right way to do that? Thanks.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Is there any way to change the background color of a messagebox?

    Is this your process or are you hooking into another process?

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Is there any way to change the background color of a messagebox?

    Does the SetWindowsHookEx() function succeed or fail as I don't see any code to check the returned value.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Is there any way to change the background color of a messagebox?

    Quote Originally Posted by Arjay View Post
    Is this your process or are you hooking into another process?
    The call to SetWindowHookEx and the call to the callback function CallWndRetProc are in the same process.

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Is there any way to change the background color of a messagebox?

    Quote Originally Posted by 2kaud View Post
    Does the SetWindowsHookEx() function succeed or fail as I don't see any code to check the returned value.
    Yes, it succeeds. Actually I can make use of the callback function CallWndRetProc to change the position of the dialog but I can't change the color of the dialog within the same callback function CallWndRetProc.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Is there any way to change the background color of a messagebox?

    See SetBkColor function

    From the docs you'll see a link to Using Menus which shows SetBkColor done in OnDrawItem. Not sure if that helps you.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Is there any way to change the background color of a messagebox?

    For what message are you testing in your CallWndRetProc() when you execute a BeginPaint()/EndPaint() sequence?

    As a message box is a dialog box, have you tried hooking the WM_CTLCOLORDLG message? See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Is there any way to change the background color of a messagebox?

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Is there any way to change the background color of a messagebox?

    BeginPaint/EndPaint should ONLY be called in the handler for WM_PAINT using them at any other time can have weird results and sideeffects.

    WM_CTLCOLORDLG is for controls on a dialog,
    you probably want the WM_ERASEBKGND
    though you will probably ALSO need to do some work in WM_CTLCOLORDLG to have the controls on the dialog patch with the dialog.

    what you're asking for is highly "abnormal" and things will behave differently on different flavours of windows. You'll probably have an easier time to simply not use MessageBox() at all, but rather, create your own dialog with a custom background that displays the desired message.

    The above may or may not work in combination with windows themes which have a different way of doing things, and your color changes may not fit into the theme at all. It may also have an assortment of issues when used with remote desktop.

  10. #10
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Is there any way to change the background color of a messagebox?

    Quote Originally Posted by OReubens View Post
    BeginPaint/EndPaint should ONLY be called in the handler for WM_PAINT using them at any other time can have weird results and sideeffects.

    WM_CTLCOLORDLG is for controls on a dialog,
    you probably want the WM_ERASEBKGND
    though you will probably ALSO need to do some work in WM_CTLCOLORDLG to have the controls on the dialog patch with the dialog.

    what you're asking for is highly "abnormal" and things will behave differently on different flavours of windows. You'll probably have an easier time to simply not use MessageBox() at all, but rather, create your own dialog with a custom background that displays the desired message.

    The above may or may not work in combination with windows themes which have a different way of doing things, and your color changes may not fit into the theme at all. It may also have an assortment of issues when used with remote desktop.
    Now I created my own custom window and then within window procedure I tried to process the window message WM_ERASEBKGND, here is the code,
    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	HDC hdc;
    
    	switch (uMsg)
    	{
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	case WM_ERASEBKGND:		
    		PAINTSTRUCT ps;
    		hdc = BeginPaint(hwnd, &ps);
    
    		::SetBkColor(hdc, RGB(255, 0, 0));
    
    		EndPaint(hwnd, &ps);
    		return 0;	
    	}
    	return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    But it doesn't work. Anything wrong? Thanks.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Is there any way to change the background color of a messagebox?

    BeginPaint/EndPaint should ONLY be called in the handler for WM_PAINT using them at any other time can have weird results and sideeffects.
    So why are you calling BeginPaint() for the WM_ERASEBKGND message??

    If you want a dc when not processing a WM_PAINT message, use GetDC() and ReleaseDC().
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    Last edited by 2kaud; January 6th, 2015 at 01:39 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Is there any way to change the background color of a messagebox?

    Quote Originally Posted by 2kaud View Post
    So why are you calling BeginPaint() for the WM_ERASEBKGND message??

    If you want a dc when not processing a WM_PAINT message, use GetDC() and ReleaseDC().
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    SO I changed the code,
    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	HDC hdc;
    
    	switch (uMsg)
    	{
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	case WM_ERASEBKGND:		
    		hdc = GetDC(hwnd);
    		::SetBkColor(hdc, RGB(255, 0, 0));
    		ReleaseDC(hwnd, hdc);
    		return 0;	
    	}
    
    	return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    But it still doesn't work. Thanks.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Is there any way to change the background color of a messagebox?

    If you process WM_ERASEBKGND, then you should return TRUE. See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    This may be of help
    http://stackoverflow.com/questions/2...g-box-win32api
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Is there any way to change the background color of a messagebox?

    Quote Originally Posted by 2kaud View Post
    If you process WM_ERASEBKGND, then you should return TRUE. See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    This may be of help
    http://stackoverflow.com/questions/2...g-box-win32api
    Even I return TRUE in processing WM_ERASEBKGND, it still doesn't work. The window looks transparent but I expect it to be red. What went wrong? Thanks.

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Is there any way to change the background color of a messagebox?

    Quote Originally Posted by LarryChen View Post
    SO I changed the code,
    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	HDC hdc;
    
    	switch (uMsg)
    	{
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	case WM_ERASEBKGND:		
    		hdc = GetDC(hwnd);
    		::SetBkColor(hdc, RGB(255, 0, 0));
    		ReleaseDC(hwnd, hdc);
    		return 0;	
    	}
    
    	return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    But it still doesn't work. Thanks.
    Note then WM_ERASEBKND is sent with wParam set to the device context. So you don't need to GetDC/ReleaseDC.

    BTW, did you try to handle WM_CTLCOLORDLG?
    Victor Nijegorodov

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured