改变编辑框文字颜色,就是在OnCtrlColor里面设置SetTextColor就可以了。大致代码如下:
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)
pDC->SetTextColor(colour);
// TODO: Return a different brush if the default is not desired
return hbr;
}
你这个,需要在编辑框的EN_CHANGE消息响应函数中,获取文本并判断范围,将一个RGB类成员变量colour设置为需要的值。
参考文章:
http://wenku.baidu.com/view/2ed4beea5ef7ba0d4a733b49.html
//定义一个全局变量获取编辑框字符串
CString str;
void CTest3Dlg::OnChangeEdit1() //添加编辑框ENCHANGE事件
{
m_edit.GetWindowText(str);
int i=atoi(str);
if (i>10&&i<80)
{
colour = RGB(0,0,0);
}
else if (i>80)
{
colour = RGB(255,0,0);
}
else
{
colour = RGB(0,0,255);
}
m_edit.SetFocus();
}
//对话框WM_CORLOR消息
HBRUSH CTest3Dlg::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)
pDC->SetTextColor(colour);
// TODO: Return a different brush if the default is not desired
return hbr;
}