Making a rectangular selection in a RichTextBox with Alt-Left-Mouse sweep?

牧云@^-^@ 提交于 2019-12-04 07:44:09

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!