Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How to change the background color of an Edit Box?

background Box color edit
0
Posted

How to change the background color of an Edit Box?

0

You can change the background color of an edit control in a dialog by handling the WM_CTLCOLOR message in the dialog: class MyDialog : public CDialog { //… COLORREF _BkgColor; HBRUSH _BkgBrush; }; BOOL MyDialog::OnInitDialog() { //… _BkgColor = RGB(0,255,0); _BkgBrush = ::CreateSolidBrush(_BkgColor); } HBRUSH MyDialog::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ) { hbr = CDialog::OnCtlColor(pDC,pWnd,nCtlColor); if (pWnd()->GetDlgCtrlId() == IDC_MY_EDIT_CONTROL) { pDC->SetBkColor(_BkgColor); hbr = _BkgBrush; } return hbr; } In the OnInitDialog() handler for the dialog, we initialize the background color value and create a brush in that color. The WM_CTLCOLOR handler is called OnCtlColor. The edit control for which we want to change the color has the resource ID IDC_MY_EDIT_CONTROL. We set the text background to our background color using SetBkColor(), and the overall background for the control by returning the brush we created.

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123