MFC中给各个控件填充背景颜色的方法

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;
}
上一篇:Jqplot 使用总结之一(线条及节点颜色)


下一篇:【Excle数据透视表】如何按照地区交替填充背景颜色