Owner draws a TextBox for use in WinForms

I need a quick and medium TextBox solution. The RichTextBox is too slow, so I want to go by drawing the owner or creating a user control.

My need is for a text box that can handle large text content and offers simple highlighting by drawing colorful backgrounds around words or individual characters. The important thing is that the text line itself does NOT contain markup for this, and word indices for the mark are stored separately. Indexes relative to the beginning of a text string (also known as the Text property when it comes to .NET TextBox).

I think this will be due to bringing text under my own control, as Windows Edit Control will not be able to do what I need.

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

Is it possible to do quick control under .NET? (it is already assumed that API calls will be needed). Or is it better done in C ++?


Addendum 1: I think the way to do this is described in response from MarkIsMobile user in question SO Drawing over a TextBox in .NET. Compact Framework . See, OnPaint TextBox is not very useful, as TextBox is a rather complicated control. The approach described in the answer from MarkIsMobile is as follows:

  • Replace the standard Windows procedure with your own C # delegate
  • But still keep the default behavior and call OldWindowsProc
  • Make your own drawing after that.

I have not tried this myself yet, but it would be interesting to see more examples of this.

Moreover. My current approach (the same?) Is to access the control through the NativeWindow class and override WndProc. I just draw over the text with a degree of transparency to create the β€œcolor marker” effect that I need, and it actually works great, but not perfect. (Are there ways to draw using raster blending only by coloring the background, not the text in the foreground?)

+4
source share
1 answer

You cannot realistically implement your own TextBox; writing your own text editor is punishable. Use something off the shelf, such as ScintillaNET.

Writing your own scrollable shortcut is somewhat appropriate. Start with a double buffered panel, use OnPaint to draw text. It will only be an improvement if you do not implement word wrap, which is very expensive to figure out where to start drawing, since you must wrap all the text before the scrolled first visible line. Computing AutoScrollMinSize is expensive, since you need to scan all the text to count line breaks, make sure you don't need to refresh the text often.

In general, the chances are good that you just find out why the TextBox is as slow as it is. You hold it in force by limiting the amount of text to what a person can reasonably expect, ever want to read it. That's not a lot. I do this by tracking the length of the text for each addition and throwing half of it when it passes 65536 characters. Nice round room.

+4
source

All Articles