开发者

Owner draw TextBox for use in WinForms

I need a lean & mean TextBox solution. A RichTextBox proves too slow, so I want to go the way of owner drawing, or custom control building.

My need is for a textbox that can handle large text content and offers simple highlighting by drawing colored backgrounds around words or single characters. Important is, that the text string itself does NOT contain markup for this, but rather, the indices of words to mark are stored separately. Indices relative to the start of the text string (also known as the Text property when talking about a .NET TextBox).

I think it will have to involve drawing the text under my own control as the Windows Edit Control will not be able to do what I need.

My application is Windows Forms. What is the proper way to make a control like this, and are there any examples?

And can one make a fast control under .NET? (already assuming native API calls will be needed). Or is this better done in C++?


Addition 1: I think the way to do this is as described by the answer from user MarkIsMobile in the SO question Drawing over a TextBox in .NET Compact Framework. See, the OnPaint of the TextBox is not really useful, as the TextBox is a rather quirky control. The approach as outlined in the answer from MarkIsMobile is as follows:

  • Replace the default Windows Procedure with your own C# delegate
  • But still keep the 'default' behavior, and call 'OldWindowsProc'
  • Do your own custom drawing after this

I haven't been trying 开发者_JS百科this myself yet, but would be interested to see more examples of this.

Also. My current approach is (the same?) to get access to the control via the NativeWindow class and override WndProc. I simply draw over the text with a degree of transparency to create the 'color marker' effect that I'm after, and this works fine actually - but is not perfect. (Are there ways to draw with bitmap blending, only colorizing the background and not the text in the foreground?)


You cannot realistically implement your own TextBox, writing your own text editor is punishing. Use something off the shelf, like ScintillaNET.

Writing your own scrollable Label is somewhat do-able. Start with a double-buffered Panel, use OnPaint to draw the text. It is only ever going to be an improvement if you don't implement word wrap, that makes it very expensive to figure out where to start drawing since you have to wrap all the text before the scrolled first visible line. Calculating the AutoScrollMinSize is expensive since you have to scan the entire text to count the line breaks, make sure you don't have to update the Text too often.

In general, the odds are good that you'll merely find out why TextBox is as slow as it is. You keep it performant by limiting the amount of text to what a human can reasonably be expected to ever want to read through. Which isn't much. I do it by keeping track of the length of the Text for each append and throwing half of it away when it goes past 65536 characters. A nice round number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜