1

How do I change the background color of a button control created using CreateWindow?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user484458
  • 168
  • 3
  • 11

2 Answers2

2

The Windows API does not offer many options to customize the appearance of standard controls anymore.

  • WM_CTLCOLORBTN can be handled by the parent window of a button to control some aspects of a buttons appearance, but uxtheme buttons only use the background brush to paint the area behind the button. The appearance of the face is determined by the current theme.

  • WM_DRAWITEM can also be handled by the parent window, by setting the BS_OWNERDRAW style on the button. This allows the parent window to completely replace the normal buttons painting logic.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148
1

To manage the color of the controls on your dialog box, add a handler to the WM_CTLCOLOR message in your dialog class.
You will then have to add few lines like that :

HBRUSH CYourDialogClass::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    if (pWnd->GetDlgCtrlID() == IDC_OF_YOUR_BUTTON)
    {
        pDC->SetBkColor (RGB(0, 0, 255)); // BLUE color for background
        pDC->SetTextColor (RGB(255, 0, 0)); // RED color for text
    }

    return hbr;
}
oaimac
  • 784
  • 2
  • 12
  • 27