1

In my application I have CPaneDialog with controls (e.g. Text control). I try to set background color for this CPanelDialog. For this purpose, I overwrited OnEraseBkgnd

BOOL CBgPaneDialog::OnEraseBkgnd(CDC* pDC)
{
CBrush backBrush(RGB(255, 128, 128));
CBrush* pOldBrush = pDC->SelectObject(&backBrush);
CRect rect;
pDC->GetClipBox(&rect);     // Erase the area needed
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),PATCOPY);
pDC->SelectObject(pOldBrush);
return TRUE;
}

Unfortunately, controls on this CPaneDialog have other background. http://fotoo.pl//out.php?t=964580_text.png

I overwrot next method: OnCtlColor to set caontrol's backgorund.

HBRUSH CBgPaneDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
CBrush br;
br.CreateSolidBrush(RGB(255,255,255));
HBRUSH hbr = (HBRUSH)br;
CWnd *pCheckBox = GetDlgItem(IDC_STATIC);  // put ID of your checkbox here.

int a;

if (*pCheckBox == *pWnd)
{
    br.DeleteObject();
    br.CreateSolidBrush(a=pDC->SetBkColor(RGB(255, 128, 128)));
    hbr = (HBRUSH)br;

}
else
    hbr = CPaneDialog::OnCtlColor(pDC, pWnd, nCtlColor);

return hbr;

}

The control's background have changed, but not completely. Please see in the picture: http://fotoo.pl//out.php?i=964579_textcontrol.jpg

How can I change background completely for the text control?

Ahemski
  • 93
  • 8

1 Answers1

1

Don't return temporary brush. Your code is okay for OnEraseBkgnd() because it's using the brush, not returning it, but for OnCtlColor use this instead:

class CMyDialog ...
{
    COLORREF BkColor;
    CBrush BkBrush;
    //...
};

CMyDialog::CMyDialog...
{
    BkColor = RGB(0, 255, 255);
    BkBrush.CreateSolidBrush(BkColor);
}

HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    //add conditions...
    pDC->SetBkColor(BkColor);
    return BkBrush;
}

By the way, you can add mfc tag to your question to get faster answer in future.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77