关于网友提出的“ 动态改变edit控件的颜色”问题疑问,本网通过在网上对“ 动态改变edit控件的颜色”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 动态改变edit控件的颜色
描述: 在对话框上拖入一个edit控件,想实现每次鼠标点击edit控件时,改变控件的背景色。
在网上找了一些资料,我的大概思路是
1.响应对话框的OnLButtonDown消息,在这里设置标识,用于标记现在点击在哪个edit上
2.添加OnCtlColor,在其中改变edit框的背景色
好像OnLButtonDown消息被edit框截获了,所以edit控件收不到鼠标点下的消息
请教高手有什么好的思路,最好能贴上代码!谢谢
解决方案1: 消息反射啊,重新建立一个类,名叫CEditEx
CEditEx::CEditEx()
{
m_brushBKFocusOn.CreateSolidBrush(RGB(255,0,0));
m_boFocus = FALSE;
}
加入CtlColor消息处理函数
HBRUSH CEditEx::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
if (m_boFocus)
{
pDC->SetBkMode(TRANSPARENT);
return m_brushBKFocusOn;
}
else
pDC->SetTextColor(RGB(255,255,0));
// TODO: Return a non-NULL brush if the parent's handler should not be called
return NULL;
}
加入OnLButtonDown消息处理函数
void CEditEx::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_boFocus = TRUE;
Invalidate();
CEdit::OnLButtonDown(nFlags, point);
}
加入OnKillFocus消息处理函数
void CEditEx::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
// TODO: Add your message handler code here
m_boFocus = FALSE;
Invalidate();
}
解决方案2:
在父对话框类添加虚函数PreTranslateMessage(),在它里面过滤WM_LBUTTONDOWN消息,判断CRect::PtInRect();判断点击的位置是否在Edit控件上,如果是的InvalidateRect()强制刷新该Edit控件区域即可。在WM_CTLCOLOR消息响应函数中去设置你Edit控件的颜色
以上介绍了“ 动态改变edit控件的颜色”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2355504.html