How to set color for static text controls

17 03 2009




Sometimes we need to change the color of the static text on our window. Actually the default colour of the static text is black.




MFC provides a CWnd::OnCtlColor function for that. Actually this function handles the WM_CTLCOLOR messages of the window. The framework calls this member function when a child control is about to be drawn.




HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);      
   if(pWnd->GetDlgCtrlID() == IDC_ST_MY_STATIC_CTRL)   
      //set the static text color to red      
      pDC->SetTextColor(RGB(255, 0, 0));   
   // TODO: Return a different brush if the default is not desired   
   return hbr;




If you want to do it in the Win32 application just handle the message WM_CTLCOLORSTATIC .



One response

13 10 2014


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: