• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1360
  • Last Modified:

CStatic color and OnCtrlColor question


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.  

  • 2
1 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.

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;

StixGPAuthor Commented:
Spot on, thank you very much.

I Didn't know how to do the wizard stuff until now.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now