如何改变控件内的字体颜色?
在MFC类库提供了CWnd::OnCtlColor函数,在工作框架的子窗口被重画时将调用该成员函数.因此可以重载WM_CTLCOLOR消息的响应函数.此函数的原型:
afx_msg HBRUSH OnCtlColor(CDC *pDC,CWnd *pWnd,UINT nCtlColor);
参数nCtlColor用于指定控件的类型,可以是:
.CTLCOLOR_BTN 按钮控件
.CTLCOLOR_DLG 对话框
.CTLCOLOR_EDIT 编辑框
.CTLCOLOR_LISTBOX 列表控件
.CTLCOLOR_MSGBOX 消息控件
.CTLCOLOR_SCROLLBAR 滚动条控件
.CTLCOLOR_STATIC 静态控件
[程序实现]
假设你已有了名为My的对话框工程.你有了一个Edit的控件.
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_EDIT)
{
if (IDC_edit1== pWnd->GetDlgCtrlID())//指定具体的某个控件
{
pDC->SetTextColor(RGB(255,0,0));
}
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
浙公网安备 33010602011771号