CStatic color and OnCtrlColor question

Posted on 2007-07-30
Last Modified: 2013-12-14

I am attempting to change the text and background colors of a CStatic and simmilar MFC controls.  After looking into this problem, I've come accross  afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);

but when I try to add this function to the message map is Visual Studio 2005, I get errors complaining about the wrong function definition.  The only way I seem to be able to compile is by using

LRESULT OnCtlColor(WPARAM wParam, LPARAM lParam); instead of the above.

However, when I debug this code, the wParam provided is 0 and the lparam contains a large value.  I can't find any documentation on this function, or what the parameters mean, and I'd like to know why I can't use the HBRUSH function definition in my program since its clearly documented all over the place.

My most recent attempt at this look like

LRESULT CGeoFlowDlg::OnCtlColor(WPARAM wParam, LPARAM lParam)

MyCDC = GetDC();
MyHDC = MyCDC->GetSafeHdc();

SetTextColor(MyHDC, RGB(0, 128, 0));
SetBkColor(MyHDC, RGB(0, 0, 255));

return (LRESULT)m_hDialogBrush; //brush was created at start of application and is set to dark grey

I put the above  together from various examples in hopes of getting something to work, but no joy.  

Question by:StixGP
    LVL 22

    Accepted Solution

    >>Visual Studio 2005, I get errors complaining about the wrong function definition

    When I create a test dialog based project in VS2005, I am able to add OnCtlColor handler with HBRUSH prototype.
    In properties ->Messages when I add WM_CTLCOLOR handler OnCtlColor wizard automatically adds definition :

    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);

    Try to create MFC dialog based project and handler using wizard ( properties->Messages->WM_CTLCOLOR, OnCtlColor) and try to reproduce and check if you are getting same.

    LVL 22

    Expert Comment

    and you code should be like :

    HBRUSH CViewOperator::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
       HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);  //base class

       if( nCtlColor == CTLCOLOR_STATIC) //static control
       // Set colors...........

      return (LRESULT) m_hDialogBrush;

      return hbr;


    Author Comment

    Spot on, thank you very much.

    I Didn't know how to do the wizard stuff until now.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now