使用 Alt-Left-Mouse 扫描在 RichTextBox 中进行矩形选择?
有相当多的应用程序允许您在按住 Alt 键的同时用鼠标扫过来选择文本框或文本矩形。例如,Visual Studio 2010 在代码编辑器中执行此操作。 Emacs 做到了。 Winword 做到了。我们都见过。似乎必须遵循一个标准模式来编码这种行为,但我似乎找不到它。
我怀疑我没有用正确的关键字进行谷歌搜索,因为我得到的只是对矩形、Alt-Left、扫描、选择等的错误点击。
我确信我可以对其进行编码,但这意味着禁用使用的正常选择代码例如,在 RichTextBox 中。这听起来很丑陋,容易出错,而且工作量可能超过其价值。
有人有关于如何执行此操作的建议(友善!:-))或如何完成此操作的示例吗?
更新:我刚刚在代码项目上找到了这篇可能适用的文章:基于列的选择< /a>
There are quite a few applications that allow you to select a box or rectangle of text by sweeping with mouse while the Alt key is pressed. Visual Studio 2010 does this in the code editor, for instance. Emacs does it. Winword does it. We've all seen it. It seems like there must be a standard pattern to follow to encode this behavior but I cannot seem to find it.
I suspect I am not Googling with the correct keywords as all I am getting are false hits on rectangle, Alt-Left, sweep, selection, etc.
I'm sure I can code it up but it would mean disabling the normal selection code used in, say, RichTextBox. And that sounds ugly, error prone and probably more work than it is worth.
Anybody have a suggestion (be nice! :-) ) of how to do this or an example of how it is done?
UPDATE: I just found this article on Code Project that might apply: Column based selection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RichTextBox 经常被误认为是编辑器。这在技术上是可行的,您需要大量代码。第一个任务是选择固定间距的字体,例如 Courier。
主要问题是您无法使用选择功能,它总是跨越行。您必须伪造它,可以使用 SelectionBackColor 属性来伪造它。实现 MouseDown 和 MouseMove 事件,检查 Control.Modifiers 属性以查看 ALT 键是否按下。使用 GetCharIndexFromPosition 查看正在选择的内容。在移动事件中,循环遍历取消/选择的列和行,使用 SelectionStart、SelectionLength 和 SelectionBackColor 属性为字符着色。
这会像廉价汽车旅馆一样闪烁。 P/Invoke SendMessage() 前后发送 WM_SETREDRAW 消息以避免这种情况。
对选择做一些事情是具有挑战性的。您需要对 RTB 进行子类化,以便可以重写 WndProc() 并检测 WM_COPY、WM_CUT、WM_PASTE 消息。其他随机问题是当鼠标靠近控件的顶部或底部时自动滚动,以及当进行另一个选择时取消选择。
或者您可以使用真正的编辑器,例如 ScintillaNET。总而言之,这个答案不太可能获得与问题一样多的支持。
RichTextBox is often mistaken for an editor. It is technically possible, you'll need a lot of code. First order of business is to select a fixed pitch font, like Courier.
Chief problem is that you cannot use the selection feature, it always spans lines. You'll have to fake it, possible by using the SelectionBackColor property. Implement the MouseDown and MouseMove events, check the Control.Modifiers property to see if the ALT key is down. Use GetCharIndexFromPosition to see what is being selected. In the move event, loop through the columns and rows that were de/selected, using the SelectionStart, SelectionLength and SelectionBackColor property to colorize characters.
This will flicker like a cheap motel. P/Invoke SendMessage() to send the WM_SETREDRAW message before and after to avoid this.
Doing something with the selection is challenging. You'll need to sub-class RTB so you can override WndProc() and detect the WM_COPY, WM_CUT, WM_PASTE messages. Other random problems is auto-scrolling when the mouse gets close to the top or bottom of the control and unselecting when another selection is being made.
Or you could use a real editor, like ScintillaNET. All and all, this answer is unlikely to get as many upvotes as the question.