RichTextBox 保存“选择方向”
我有一个 WinForms 程序,每当您更改选择时,RichTextBox 都需要更改某些其他文本的颜色。为了做到这一点,它必须选择该文本,因此我失去了当前的选择。
我可以保存和加载 SelectionStart 和 SelectionLength 属性,但无法保留“选择方向”:如果用户从光标向前或向后突出显示。
关于如何保存选择方向或为文本着色而无需更改选择的任何想法?
I have a WinForms program where, whenever you change your selection, the RichTextBox needs to change the colour of certain other text. In order to do this, it has to select that text, and therefore I lose my current selection.
I can save and load the SelectionStart and SelectionLength properties, but I can't keep the "selection direction": if the user was highlighting forwards or backwards from the cursor.
Any ideas about how I can either save the selection direction, or colour the text without having to change the selection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚遇到了同样的问题,现在我通过使用 EM_EXSETSEL 解决了这个问题。当cpMin>时cpMax,它的工作原理类似于“向后选择”(插入符号位于所选文本的开头)。然而我还没有找到任何其他方法来找出当前的选择方向(EM_EXGETSEL 总是返回 cpMin < cpMax),但是遵循 SelectionStart/Length 更改...
编辑:
这就是我用来解决这个问题的方法。可能有一些更简单的方法,但至少以下对我有用。
I just hit the same problem and by now I solved this by using EM_EXSETSEL. When cpMin > cpMax, it works like "backward selection" (caret at the beginning of selected text). Yet I haven't found any other way to find out current selection direction (EM_EXGETSEL always returned cpMin < cpMax) but following SelectionStart/Length changes...
EDIT:
That's what i'm using to solve this. There might be some easier way but at least the following works for me.
哎呀,丑陋的问题。不可以,EM_SETPARAFORMAT 只能对当前选择起作用。并且 EM_EXSETSEL 始终将插入符号放在选择的末尾。您可以通过观察 SelectionStart 的变化来检测选择方向,但无法将插入符号放在正确的位置。编辑控件也有同样的问题。
这通常不是问题,因为仅当用户修改文本时才会发生重新着色,而不是在选择文本时发生。我能想到的唯一解决方法是通过注入击键来恢复选择。太丑了。
Yuck, ugly problem. No, EM_SETPARAFORMAT can only work on the current selection. And EM_EXSETSEL always puts the caret at the end of the selection. You could detect the selection direction by observing the change in SelectionStart but you can't get the caret in the right spot. An edit control has the same problem.
It isn't normally a problem because re-coloring happens only when the user modifies text, not when she's selecting text. The only workaround I can think of is restoring the selection by injecting keystrokes. That's fugly.
另一种方法是直接设置 Rtf 属性。您确实需要了解 rtf 语言的语法。
附言。这也将使选择无效。我自己进行了按键注入。
Another way could be setting the Rtf property directly. You do need to know the syntax of the rtf language.
PS. That will invalidate the selection, too. I did the keystroke injection myself.