Edit窗口文字高亮显示

使用查找框查找文字的时候,希望在Edit控件中将查找到的文字高亮显示,这时我们都会想到使用EM_SETSEL消息。实践后发现这样不能使得文字高亮,原因是编辑框没有获得焦点,选中的文字也不能高亮显示。使用GetLastError()调试时会发现错误为无法访问。在发送EM_SETSEL消息前增加SetFocus,可以使文字高亮显示,但查找框就失去焦点了,于是我又再次使用SetFocus使查找框重新获得焦点。至此,功能上可以得到解决,可是查找的时候会频繁切换两个窗口的焦点,实在看着心烦。

解决方法:1.发送EM_SETSEL消息

                  2.发送EM_SCROLLCARET消息

                  不需要设置任何焦点转换,即可实现如下效果:Edit相应文字高亮显示,且查找框不失去焦点

关于EM_SCROLLCARET消息的MSDN解释如下:

EM_SCROLLCARET message

Scrolls the caret into view in an edit control. You can send this message to either an edit control or a rich edit control.

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.

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值