Example
// This OnCtlColor handler will change the color of a static control
// with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog
// class has an initialized and created CBrush member named m_brush.
// The control will be painted with red text and a background
// color of m_brush.
HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call the base class implementation first! Otherwise, it may
// undo what we are trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
{
// Set the text color to red.
pDC->SetTextColor(RGB(255, 0, 0));
// Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT);
// Return handle to our CBrush object.
hbr = m_brush;
}
return hbr;
}
假设你已有了名为 My 的对话框工程,你有了一个 STATIC 的控件, IDC_STATIC1
HBRUSH CMyDlg::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_STATIC)
{
pDC->SetTextColor(RGB(255,0,0));// 字体颜色
pDC->SetBkColor(RGB(0,0,255));// 字体背景颜色
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
如果要指定某个特定控件可以这样写:
ID 为 IDC_STATIC1
if(pWnd->GetDlgCtlID()==IDC_STATIC1)
{
pDC->SetTextColor(RGB(255,0,0));//
pDC->SetBkMode(TRANSPARENT);// 设置字体背景为透明
return (HBRUSH)::GetStockObject(BLACK_BRUSH);// 设置背景色
}
Return hbr;