使用查找框查找文字的时候,希望在Edit控件中将查找到的文字高亮显示,这时我们都会想到使用EM_SETSEL消息。实践后发现这样不能使得文字高亮,原因是编辑框没有获得焦点,选中的文字也不能高亮显示。使用GetLastError()调试时会发现错误为无法访问。在发送EM_SETSEL消息前增加SetFocus,可以使文字高亮显示,但查找框就失去焦点了,于是我又再次使用SetFocus使查找框重新获得焦点。至此,功能上可以得到解决,可是查找的时候会频繁切换两个窗口的焦点,实在看着心烦。
解决方法:1.发送EM_SETSEL消息
2.发送EM_SCROLLCARET消息
不需要设置任何焦点转换,即可实现如下效果:Edit相应文字高亮显示,且查找框不失去焦点
关于EM_SCROLLCARET消息的MSDN解释如下:
EM_SCROLLCARET message
Parameters
-
wParam
-
This parameter is reserved. It should be set to zero.
lParam
-
This parameter is reserved. It should be set to zero.
Return value
The return value is not meaningful.
Remarks
Rich Edit: Supported in Microsoft Rich Edit 1.0 and later. For information about the compatibility of rich edit versions with the various system versions, see About Rich Edit Controls.