I am writing a custom replacement for the Textbox/Label controls. It needs to provide similar functionality but will be optimized and (hopefully) much quicker in the context of my application. It needs to be able to support multi-line text selection. I'm wondering if there is a simple way to draw selected text? Right now it looks like the only way might be to calculate where the selection rectangle should be drawn and draw it manually. I was hoping to get recommendations on the easiest way to accomplish this or any alternative methods. Thanks.
Rewriting basic controls for an application is a major task and shouldn't be done unless as an absolute last resort (IMO). If the included controls aren't meeting your exceptations I would recommend purchasing a 3rd party control library that has their business built around designing and supporting these controls. I've had alot of success using Telerik's ASP.NET controls I've seen many positive statements about their other lines of controls.
There's also many other sources of 3rd party controls you could look into.
Use System.Windows.Forms.TextRenderer.
Just override methods from System.Windows.Forms.Control:
protected override void OnPaint(PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics, Text, Font, new Rectangle(0, 0, Width, Height), ForeColor);
}
public override Size GetPreferredSize(Size proposedSize)
{
return TextRenderer.MeasureText(Text, Font);
}
Of course, you'll need to handle a couple more events such as OnFontChanged or OnSizeChanged if you need custom behavior implemented.
If what you are looking for is targeting specific text (selected text) in those controls, you might want to take a look at this article. Source code of a spell-checking library(SharpSpell) is also linked from this article.
Disclamer: I couldn't post the link for the source code since I cannot post more than one hyperlink, apparently. It's in the article (CTRL+F > nativemethods.zip).