CodeDoctorJL
CodeDoctorJL

Reputation: 1254

MFC: How do I change the background color of the whole editBox?

My questions is similar to: win32 : display editbox with black color in text area on windows mobile 5

However I'm using MFC which doesn't have the same solution available as the one in the above link.

How do I change the background color of the whole background, not just the background behind the text of an edit box?

Below is my code, that only changes the background behind the text, not the whole background of the edit box.

HBRUSH CGadgetStandardDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CStandardDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    pDC->SetBkColor(RGB(255,255,255));

    return hbr;
}

Upvotes: 3

Views: 25492

Answers (2)

Mahmut EFE
Mahmut EFE

Reputation: 5248

Rename your button resource like below.

CButton  m_StopButtonto;

to

CMFCButton  m_StopButton;

Change some visible features

// Set the background color for the button text.
  m_StopButton.SetFaceColor(RGB(255,0,0),true);
  m_StopButton.SetTextColor(RGB(0,0,255));
  // Set the tooltip of the button.
  m_StopButton.SetTooltip(_T("This is my Stop Button!"));

I tried this solution for button and it worked for me. I guess it will work for the other components.

Upvotes: 1

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10415

In addition to calling SetBkColor you need to return a HBRUSH of the desired background color. So create a brush earlier (say, in the dialog constructor):

m_brBack.CreateSolidBrush(RGB(0, 255, 0));

And then return that brush when called for the control of interest:

HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_EDIT2)
{
    pDC->SetBkColor(RGB(0,255,0));
    hbr = m_brBack;
}

Upvotes: 8

Related Questions