MFC中当编辑框文字替某字符串时改变背景色
MFC中当编辑框文字为某字符串时改变背景色
代码:
1、对话框头文件添加定义:
CBrush m_newBR;
bool m_blChange;
2、对话框OnInitDialog()中添加:
m_newBR.CreateSolidBrush(RGB(255,0,0));
m_blChange = false;
3、对话框OnCtlColor()中添加:
if (pWnd->GetDlgCtrlID() == IDC_EDIT1 && m_blChange)
{
pDC->SetBkMode(TRANSPARENT);
return m_newBR;
}
4、重写编辑框的EN_CHANGE事件,添加:
CString str;
m_result.GetWindowText(str);
m_blChange = str == "change";
m_result.RedrawWindow();
应用场景:在信息录入界面,需要对某关键字特别提醒
高级扩展:在信息录入时,当内容不符合某些规则时提醒