Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Background color of edit control

Hi to all,
May i know how can i change the background color of an edit control using a clicked button? I got the OnCtlColor function as shown below, but i'm not sure on how to call this function(OnCtlColor) from my clicked button.
Your help is greatly appreciated. Thanks.

//////////////////////////////////////////////////////////////////
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
CEdit * pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);

if(nCtlColor == CTLCOLOR_EDIT && pEdit->GetSafeHwnd() == pWnd->GetSafeHwnd())
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,255,255));
return m_Brush;
}
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
return hbr;
}
//////////////////////////////////////////////////////////////////

Comments

  • weiccoweicco Member Posts: 950
    : Hi to all,
    : May i know how can i change the background color of an edit control using a clicked button? I got the OnCtlColor function as shown below, but i'm not sure on how to call this function(OnCtlColor) from my clicked button.
    : Your help is greatly appreciated. Thanks.
    :
    : //////////////////////////////////////////////////////////////////
    : HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    : {
    : CEdit * pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
    :
    : if(nCtlColor == CTLCOLOR_EDIT && pEdit->GetSafeHwnd() == pWnd->GetSafeHwnd())
    : {
    : pDC->SetBkMode(TRANSPARENT);
    : pDC->SetTextColor(RGB(255,255,255));
    : return m_Brush;
    : }
    : HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    : return hbr;
    : }
    : //////////////////////////////////////////////////////////////////
    :
    :

    You don't call this function. Controls send WM_CTLCOLOR message to parent control, when they want to be colored. MFC catches that message and calls OnCtlColor-method.

    But if I remember correctly, I was unable to get those messages on parent dialog so I derived custom edit class from CEdit and handled WM_CTLCOLOR myself using message reflection (ON_WM_CTLCOLOR_REFLECT). Like this:

    [code]
    class CMyEdit : public CEdit {
    protected:
    HBRUSH CtlColor(CDC*, UINT);
    DECLARE_MESSAGE_MAP()
    };

    BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
    ON_WM_CTLCOLOR_REFLECT()
    END_MESSAGE_MAP()

    HBRUSH CMyEdit::CtlColor(CDC *pDC, UINT nCtlColor)
    {
    pDC->SetTextColor(RGB(255, 0, 0));
    pDC->SetBkColor(RGB(0, 255, 0));

    return (HBRUSH)GetStockObject(YELLOW_BRUSH);
    }
    [/code]
Sign In or Register to comment.