发送 WM_SETTEXT 时如何避免 EN_CHANGE 通知? [英] How to avoid EN_CHANGE notifications when sending WM_SETTEXT?
问题描述
我有一个 CEdit 派生控件,当 undelying 数据为空时,它显示字符串N/A".我最近添加了代码来清空控件 (SetWindowText("");) 当它获得焦点并在焦点丢失时设置如果回到 "N/A"(SetWindowText("N/A")) 如果用户离开控制空.
I have a CEdit derived control that displays the string "N/A" when the undelying data is null. I recently added code to empty the control(SetWindowText("");) when it gains focus and set if back to "N/A"(SetWindowText("N/A")) when the focus is lost if the user left the control empty.
唯一的问题是将窗口文本设置为 "" 或 "N/A" 会触发 EN_CHANGE,因此我的对话框认为数据已更改.
The only problem is that setting the window text to "" or "N/A" triggers EN_CHANGE, so my dialog thinks that the data has changed.
如何避免在调用 SetWindowText (WM_SETTEXT) 时触发 EN_CHANGE?
How can I avoid EN_CHANGE from being fired when calling SetWindowText (WM_SETTEXT)?
注意事项
-我知道我可以将编辑控件设置为 Multiline=TRUE 但这对我来说是不可接受的.
-I know I can set the edit control to Multiline=TRUE but that's not accpectable for me.
-我的应用程序是 MBCS 所以我不能使用 SetCueBanner
-My application is MBCS so I can't use SetCueBanner
-我想要一个优雅的解决方案.暂时将父窗口设置为 NULL 不是一个优雅的解决方案.
-I want an elegant solution. Setting the parent window to NULL temporarily is not an elegant solution.
-我希望解决方案在我的自定义控件中,而不是在每个对话框中
-I want the solution to be in my custom control, not in each dialog
谢谢
推荐答案
我终于找到了适合我的问题的解决方案.
I finally found a suitable solution to my problem.
首先,我在派生控件的头文件中添加了一个标志,并在构造函数中将其初始化为 false
First, I added a flag to my derived control's header file and I initialized it to false in the constructor
bool m_bNoEnChange;
<小时>
我在派生控件的头文件中覆盖了 OnChildNotify,并且在实现中,我使用 EN_CHANGE 参数检查了 WM_COMMAND 消息.然后我返回 TRUE 以防止将消息发送到父级(对话框/页面)
I overrode the OnChildNotify in my derived control's header file and in the implementation, I checked for the WM_COMMAND message with the EN_CHANGE parameter. I then returned TRUE to prevent the message from being sent to the parent(dialog/page)
virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult);
<小时>
BOOL CADEdit::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
{
if(message == WM_COMMAND && HIWORD(wParam) == EN_CHANGE)
{
//If the flag is set, don't send the message to the parent window
if(m_bNoEnChange)
return TRUE;
}
return CEdit::OnChildNotify(message, wParam, lParam, pLResult);
}
<小时>
最后,当控件获得和失去焦点时,我用我的标志包裹了有问题的 SetWindowText
Finally, when the control gains and loses focus, I wrapped the problematic SetWindowText with my flag
m_bNoEnChange = true;
SetWindowText(_T(""));
m_bNoEnChange = false;
<小时>
这个解决方案对我来说是最好的,因为我不必修改每个对话框.
This solution is the best in my case because I don't have to modify each dialog.
这篇关于发送 WM_SETTEXT 时如何避免 EN_CHANGE 通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!