标记单词的所有实例(Delphi、RichText)
好吧,这是一个一直困扰我的问题,我找不到明确的答案。 如何查找并标记某个单词的所有实例?
我的意思是,我搜索一个词(例如:人)。如果该单词存在,则 I 标记(使用红色或任何颜色)该单词在 richedit 中的所有实例。如果我按 Esc 键,它就会被取消选择。
有什么想法吗?
代码表示赞赏。
OK, this is a problem that's been nagging and I can't see to find a definitive answer.
How do you find and mark all instances of a word?
What I mean is, I search for a word (say: Person). If the word exists the I mark (using red or whatever color) all instances of that word in the richedit. If I press Esc then it gets deselected.
Any ideas?
code is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好奇者,我写了这段代码,我希望它有用:
再见。
wonderer, I wrote this code, I hope it will be useful :
Bye.
以下代码将搜索 Rich Edit 控件中所有出现的给定单词(区分大小写),将字体颜色更改为红色,最后恢复控件的原始选择(我希望所有闪烁都尽可能少):
现在您只需将匹配项与原始文本属性一起保存到一个列表中,并使用该列表中的信息在按
Esc
时恢复所有更改。然而,如果您假设匹配项可能包含不同的字体样式、颜色等,那么正确执行此操作可能会非常棘手。因此,我没有提供任何代码来保存格式,这取决于您的要求。哦,请确保在再次更改文本之前删除突出显示的匹配项,否则您将无法正确恢复原始文本格式。
The following code will search all occurrences of the given word (case sensitive) in the rich edit control, change the font colour to red, and finally restore the original selection of the control (all with as little flicker as possible I hope):
Now you only need to save the matches together with the original text attributes to a list, and use the information in this list to revert all the changes on the press of
Esc
. This can however get quite tricky to do correctly, if you assume that the matches may contain different font styles, colours and such. I have therefore not provided any code to save the formatting, it depends on your requirements.Oh, make sure that the highlighted matches are removed before the text can be changed again, otherwise you will not correctly restore the original text formatting.