在富文本框中插入模式 [英] Insert Mode in a Rich Text Box
问题描述
我正在创建一个包含文本编辑器的应用程序.我需要给用户一些有关编辑器框当前插入方式的指示-(它会插入字符还是覆盖字符?). RichTextBox可以很好地处理Insert键-在每次按键时切换插入模式.但是,我没有找到查询RichTextBox的方法来查找当前的插入模式.尝试谷歌周围,没有太大的运气.
我想到的一种方法是在代码中处理插入键处理和插入/覆盖逻辑-这样我就可以控制插入模式并显示通知.但这将意味着不必要地重复已经内置的功能.
我敢肯定,如果RichTextBox处理插入键,则必须有某种方式检查当前模式.有人可以帮我吗?
I am working on creating an application that includes a text editor. I need to give some indication to the user about the current insert mode of the editor box - (will it insert the characters or overwrite them?). The RichTextBox processes the Insert key well - toggling the insert mode on every key press. But, I did not find a way of querying the RichTextBox, to find what is the current insert mode. Tried to google around for it, without much luck.
One way I thought of, is to handle the insert key processing and the insert / overwrite logic, within my code - so that I can control the insert mode and display the notification. But that would mean unnecessary duplication of a functionality that is already built in.
I am sure, if the RichTextBox processes the insert key, there must be some way of checking the current mode. Can someone help me with this?
推荐答案
使用WPF,您可以检查System.Windows.Input.Keyboard.IsKeyToggled(Key.Insert)
,请参阅^ ].
不幸的是,System.Windows.Forms
中没有这样的东西.一种解决方法是通过P/Invoke使用Windows APIGetKeyState
,请参阅 ^ ].对于切换状态,您需要检查结果的低位,因此像这样结果屏蔽它= GetKeyState(/*...*/)& 1.
不幸的是,使用P/Invoke可能会破坏平台兼容性-该代码仅在Windows上有效(Forms应用程序可以在许多平台上正常运行,而无需在Mono下重新编译, ^ ], http://www.mono-project. com/ [ ^ ]).因此,您不需要P/Invoke的方法会更好.
还有一个原因:System.Windows.Forms.RichTextBox
控件的每个实例都有其自己的插入状态,该状态与全局插入状态以及所有其他RichTextBox
控件的插入状态无关.看一下这段文字: http://vb.mvps.org/hardcore/html/controllingrichtextboxinsertstate.htm [ ^ ](对不起,在VB.NET中,但您可以理解).另请参阅以下讨论: http://www.codeguru.com/forum/showthread.php?t=276369 [ ^ ].
您对功能的不必要重复是正确的,但是此功能并未公开,这是很遗憾的.因此,无论如何,您都将需要处理一些按键事件,因为即使可以轮询键盘状态,也需要一些事件来触发检查.
—SA
With WPF, you can checkupSystem.Windows.Input.Keyboard.IsKeyToggled(Key.Insert)
, see http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.iskeytoggled.aspx[^].
Unfortunately, there is no such thing inSystem.Windows.Forms
. One work-around is using Windows APIGetKeyState
, via P/Invoke, see http://msdn.microsoft.com/en-us/library/windows/desktop/ms646301%28v=vs.85%29.aspx[^]. For toggled state you need to examine low-order bit of the result, so mask it like this result = GetKeyState(/*...*/) & 1.
Unfortunately, using P/Invoke would break platform compatibility — the code will work only on Windows (a Forms application could work well on many platforms without recompilation under Mono, http://en.wikipedia.org/wiki/Mono_%28software%29[^], http://www.mono-project.com/[^]). By this reason, your approach which does not require P/Invoke is better.
There is one more reason: every instance ofSystem.Windows.Forms.RichTextBox
control has its own insert state that is independent of the global insert state as well as the insert state of all otherRichTextBox
controls. Take a look at this text: http://vb.mvps.org/hardcore/html/controllingrichtextboxinsertstate.htm[^] (sorry, it''s in VB.NET, but you can understand it). See also this discussion: http://www.codeguru.com/forum/showthread.php?t=276369[^].
You are right about unnecessary duplication of a functionality, but this functionality is not expose, which is a shame, I would say. So, you will need to handle some key event anyway, because even when you can poll the keyboard status, you need some event to trigger your check.
—SA
这篇关于在富文本框中插入模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!