简体   繁体   中英

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;
}

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM