CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

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

    For WM_ERASEBKGND, wParam is the required dc. See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Why do you think that calling SetBkColor() for WM_ERASEBKGND message is going to change the colour of the background? Have you read the article to which I provided a link in post #13?
    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)

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

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

    To change the background colour of a message box as per your original question, try something like this
    Code:
    #include <iostream>
    #include <Windows.h>
    using namespace std;
    
    LRESULT CALLBACK CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    PCWPRETSTRUCT prs = (PCWPRETSTRUCT)lParam;
    HWND md = FindWindow(NULL, "Caption");
    
    	if ((md != NULL) && (prs->hwnd == md) && (prs->message == WM_ERASEBKGND)) {
    			HBRUSH brush = CreateSolidBrush(RGB(255, 0, 0));
    			RECT rect;
    			GetClientRect(prs->hwnd, &rect);
    			FillRect((HDC)prs->wParam, &rect, brush);
    			DeleteObject(brush);
    			return TRUE;
    	}
    
    	return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
    
    int main()
    {
    HHOOK	oldHook = SetWindowsHookEx(WH_CALLWNDPROCRET, CallWndRetProc, NULL, GetCurrentThreadId());
    
    	MessageBox(NULL, "Text", "Caption", MB_OK);
    
    	UnhookWindowsHookEx(oldHook);
    }
    Last edited by 2kaud; January 6th, 2015 at 05:42 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)

  3. #18
    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?

    I can't help but notice that the code would be far more maintainable if the OP would simply create his own MessageBox or at least use the system colors/themes.

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

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

    Quote Originally Posted by Arjay View Post
    I can't help but notice that the code would be far more maintainable if the OP would simply create his own MessageBox or at least use the system colors/themes.
    True.
    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)

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

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

    Try this (not tested)
    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
         switch (uMsg)
         {
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    
    	case WM_ERASEBKGND:		
                            {
                            HBRUSH brush = CreateSolidBrush(RGB(255, 0, 0));
    			RECT rect;
    			GetClientRect(hwnd, &rect);
    			FillRect((HDC)wParam, &rect, brush);
    			DeleteObject(brush);
    			return TRUE;
                    	}
            }
    	return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    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)

  6. #21
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

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

    First note that WM_CTLCOLORMSGBOX message is discontinued and actually has no use.
    However, we can customize a dialog box shown with MessageBox or related functions as follows:
    1. Before calling MessageBox create a CBT hook.
    2. In the hook procedure, catch message box window creation and subclass it (assign an app-defined window procedure).
    3. In the app-defined window procedure associated with the message box, handle WM_CTLCOLORDLG (and other messages if needed).
    4. Remove the hook procedure when MessageBox exits.

    If using MFC, we can override CWinApp::DoMessageBox in order to create a custom message box (based on above steps) when call AfxMessageBox.

    See implementation details and demo projects in the following articles:
    Last edited by ovidiucucu; January 9th, 2015 at 09:45 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Page 2 of 2 FirstFirst 12

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