1.给程序设置大背景色,在OnPaint()函数中添加如下代码:
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
dc.FillSolidRect(rect, RGB(, , ));
dc.FillPath();
2.给个控件修改背景颜色,先在类向导中添加WM_CTLCOLOR消息,然后就会自动添加OnCtColor()函数:
HBRUSH CMFCApplication1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CRect rc;
switch (pWnd->GetDlgCtrlID())
{
case IDC_STATIC_GROUP: //像GROUP BOX控件要用FillSolidRect()函数来填充背景,我用SetBkColor()不起作用,不知道为什么
GetDlgItem(IDC_STATIC_GROUP)->GetClientRect(&rc);
pDC->FillSolidRect(rc, RGB(, , ));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
case IDC_STATIC1:
pDC->SetBkColor(RGB(, , ));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
case IDC_STATIC2:
pDC->SetBkColor(RGB(, , ));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
case IDC_STATIC3:
pDC->SetBkColor(RGB(, , ));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
case IDC_STATIC4:
pDC->SetBkColor(RGB(, , ));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
case IDC_STATIC_GROUP2:
GetDlgItem(IDC_STATIC_GROUP2)->GetClientRect(&rc);
pDC->FillSolidRect(rc, RGB(, , ));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
case IDC_STATIC:
pDC->SetBkColor(RGB(, , ));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
case IDC_BUTTON2:
GetDlgItem(IDC_BUTTON2)->GetClientRect(&rc);
pDC->FillSolidRect(rc, RGB(, , ));
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
default:
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
return hbr;
}