参考一:
主要是重载OnCtlColor()函数,下面以EditBox为说明:
EditBox中,当将其属性设定为只读时,其默认的背景色将变为灰色,看着很不雅,尤其是当和ComboBox一起
组合使用时,又是灰色又是白色的,不统一。这是如果我们想把EditBox设定为只读属性,同时还要求背景色
设为白色,就需要重载OnCtlColor().
-----------------------------------------------------------------------------------------------
=======Sample=======
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if( pWnd->GetSafeHwnd() == m_editMinute.GetSafeHwnd() ||
pWnd->GetSafeHwnd() == m_editYear.GetSafeHwnd() )
{
//将背景色设为白色
static HBRUSH hbrEdit = ::CreateSolidBrush( RGB(255,255,255) );
pDC->SetBkMode( TRANSPARENT );
return hbrEdit;
}
// TODO:
return hbr;
}
参考二:
基于对话框工程,名为:Draweditbox
在CDraweditboxDlg中添加如下变量:
private:
HBRUSH m_hYellowBrush;//背景色变量
HBRUSH m_hRedBrush;//背景色变量
在CDraweditboxDlg中添加响应函数WM_CTLCOLOR。
/
//背景色绘制函数
HBRUSH CDraweditboxDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
/
//绘制背景色(全部)
if( nCtlColor == CTLCOLOR_EDIT )//editbox背景色
{
pDC->SetTextColor(RGB(0,230,0));
pDC->SetBkColor(RGB(0,0,0));
return m_hYellowBrush;
}
if( nCtlColor == CTLCOLOR_DLG )//对话框背景色
{
pDC->SetBkColor(RGB(255,0,0));
return m_hRedBrush;
}
if( nCtlColor == CTLCOLOR_STATIC )//静态文本框背景色
{
pDC->SetBkColor(RGB(255,0,0));
pDC->SetTextColor(RGB(255,255,255));
return m_hRedBrush;
}///
// TODO: Return a different brush if the default is not desired
return hbr;
}
在BOOL CDraweditboxDlg::OnInitDialog()中添加如下内容:
//背景色设置/
m_hYellowBrush = CreateSolidBrush(RGB(0,0,0));
m_hRedBrush = CreateSolidBrush(RGB(255,250,250));