原文地址:
//注:我试用时的工程类型为MFC对话框工程具体步骤:
1,首先为所要改变颜色的对话框类添加WM_CTLCOLOR消息函数。
2,给你的工程中的对话框类添加一个CBrush变量,如:
CBrush m_bkBrush; //准备一把背景刷子
3,在对话框的初始化函数OnInitDialog中return TRUE的前面添加一行代码:
m_bkBrush.CreateSolidBrush(RGB(255,255,0)); //创建一把黄色的背景刷子
4,最后把你原来添加消息函数的代码改成如下:
HBRUSH CEX06aDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor==CTLCOLOR_DLG)
{
return m_bkBrush; //返回刚才创建的背景刷子
}
//TODO:如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
附加:在MSDN中,关于参数nCtlColor,取以下值:
nCtlColor
Contains one of the following values, specifying the type of control:
· CTLCOLOR_BTN Button control
· CTLCOLOR_DLG Dialog box
· CTLCOLOR_EDIT Edit control
· CTLCOLOR_LISTBOX List-box control
· CTLCOLOR_MSGBOX Message box
· CTLCOLOR_SCROLLBAR Scroll-bar control
· CTLCOLOR_STATIC Static control
附msdn
The framework calls this member function when a child control is about to be drawn.
afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );