转自:http://dev.firnow.com/course/3_program/c++/cppjs/2008422/110829.html
1.重载OnCtlColor (CDC* pDC, CWnd* pWnd, UINT nCtlColor),即WM_CTLCOLOR消息。
①在CExampleDlgDlg的头文件中,添加一CBrush的成员变量:
class CExampleDlgDlg : public CDialog
{...
protected:
CBrush m_brush;
...
};
②在OnInitDialog()函数中添加如下代码:
BOOL CExampleDlgDlg::OnInitDialog()
{
...
// TODO: Add extra initialization here
m_brush.CreateSolidBrush(RGB(0, 255, 0)); // 生成一绿色刷子
...
}
③利用ClassWizard重载OnCtlColor(…),即WM_CTLCOLOR消息:
HBRUSH CExampleDlgDlg::OnCtlColor
(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
/*
** 这里不必编写任何代码!
**下行代码要注释掉
** HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
*/
return m_brush; //返加绿色刷子
}
2. 修改对话框的OnPaint,在else中添加如下代码
CPaintDC dc(this);
CRect rect;
GetClientRect(rect);
dc.FillSolidRect(rect,#000000);
CDialog::OnPaint();
3.在对话框的应用类(App)的.cpp的Initinstance()中加入代码:
//加在int nResponse=dlg.DoModal();
前一个RGB设置背景色,第二个设置字体颜色
SetDialogBkColor(#0000ff,#00ff00);
4.1.在对话框类中添加成员变量:
public:
CBrush m_brushBlue;
2.在对话框类的OnInitDialog()中添加代码:
m_brushBlue.CreateSolidBrush(#0000ff);
3.用ClassWizard在对话框类中添加成员函数OnCtlCollor(),并在其中添加代码:
if(nCtlColor==CTLCOLOR_DLG)
return m_brushBlue;