Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used several Radio Button Controls in my MFC application.I want to set the corresponding backcolor when I click or click the relative Radio Button Control.

I added the WM_CTLCOLOR message into my program.Here is the code:
C++
HBRUSH xxxDlg::OnCtlColor(CDC* pDC,CWnd* pWnd,UINT nCtlColor)
{
      if(TRUE == ((CButton*)GetDlgItem(IDC_RADIO_INPUT_BUY))->GetCheck())
         {
            if(pWnd->GetDlgCtrlID() == IDC_RADIO_INPUT_BUY)
            {
              COLORREF backColor = RGB(0xFF,0x40,0x40);
              pDC->SetBkMode(TRANSPARENT);
              return CreateSolidBrush(backColor);
            }
         }
      else if(FALSE== ((CButton*)GetDlgItem(IDC_RADIO_INPUT_BUY))->GetCheck())
         {
            if(pWnd->GetDlgCtrlID() == IDC_RADIO_INPUT_BUY)
            {
              COLORREF backColor = RGB(0xFF,0xFF,0xFF);
              pDC->SetBkMode(TRANSPARENT);
              return CreateSolidBrush(backColor);
            }
         }
   
}


IDC_RADIO_INPUT_BUY is the ID of Radio Button Control.
After running my MFC program,I found that the radio button backcolor didn't change when clicking it(selected or non-selected).So is there any problems in my code?

What I have tried:

See the previous problem description.
Posted
Updated 28-Aug-17 23:28pm
v3

1 solution

It is not possible to set the background colour of check boxes and radio buttons for the standard Windows controls. These are drawn internally by the system.

The only solution would be to use owner drawn controls. But this is quite difficult for the check and radio markers.

See also my answer to a similar question for check boxes: How do I change the background colour of a checkbox in MFC?[^].

But drawing radio buttons is more sophisticated because you have to determine the circle and bullet dimensions for filling with the background colour and drawing the bullet in a similar style as done by the system.

Some information if you still want to do it:

  • Draw the unchecked button using other styles as appropriate (DrawThemeBackground resp. CDC:DrawFrameControl)
  • Get the rect of the button
  • Reduce the rect by 2 pixels in each dimension to get the rect for the background
  • Special case: When hover glove is active, reduce rect by 4 pixels
  • Fill the background using CDC::Ellipse with solid pen and brush set to the background colour
  • The rect size for the bullet is the button rect reduced by 3 pixels with themed applications or 4 pixels with classic controls
  • The resulting width is usually 13 with themed apps. Then decrement the left position and increment the bottom position
  • Colour is COLOR_WINDOWTEXT (active) or COLOR_BTNSHADOW (disabled) for classic style
  • With themed app use GetThemeColor() to get the colour for BP_RADIOBUTTON, the button style, and TMT_FILLCOLORHINT
  • Draw the bullet using CDC::Ellipse with solid pen and brush set to the background colour
Note that the above does not draw a shaded / gradient bullet with active themes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900