[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1257
  • Last Modified:

CStatic color and OnCtrlColor question

Hello,

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)
{
HDC MyHDC;
CDC* MyCDC;

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.  

Thanks
 
0
StixGP
Asked:
StixGP
  • 2
1 Solution
 
mahesh1402Commented:
>>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.

-MAHESH
0
 
mahesh1402Commented:
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;
}

-MAHESH
0
 
StixGPAuthor Commented:
Spot on, thank you very much.

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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