定义一成员变量CBrush m_brush;
OnInitDialog()中进行初始化工作m_brush.CreateSolidBrush(RGB(255,0,0));
然后在OnCtrlColor中
HBRUSH CAsdfaaaaaaaaaDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor==CTLCOLOR_EDIT &&
pWnd->GetDlgCtrlID()==IDC_EDIT1)//注意此处的(pWnd->),否则没效果
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(RGB(255,255,0));//设置文本背景色
pDC->SetBkMode(TRANSPARENT);//设置背景透明
hbr = (HBRUSH)m_brush;
}
return hbr;
}
对于nCtlColor的类型,如下:
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
OnInitDialog()中进行初始化工作m_brush.CreateSolidBrush(RGB(255,0,0));
然后在OnCtrlColor中
HBRUSH CAsdfaaaaaaaaaDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor==CTLCOLOR_EDIT &&
pWnd->GetDlgCtrlID()==IDC_EDIT1)//注意此处的(pWnd->),否则没效果
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(RGB(255,255,0));//设置文本背景色
pDC->SetBkMode(TRANSPARENT);//设置背景透明
hbr = (HBRUSH)m_brush;
}
return hbr;
}
对于nCtlColor的类型,如下:
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