How to change the background color of an Edit Box

 

How to change the background color of an Edit Box

http://www.codeproject.com/cpp/cppfaq10dec04-23jan05.asp

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.

posted on 2006-10-17 22:06  cy163  阅读(373)  评论(0编辑  收藏  举报

导航