Owner-drawing a Windows.Forms TextBox
This article describes how SharpSpell is able to modify existing TextBox controls to display wavy red underlines below misspelled words.
Here’s an image to demonstrate what I mean:

(This image is borrowed from SharpSpell, but you get the point)
Overview
The .NET framework provides means to subclass native Win32 windows and controls by inheriting from the NativeWindow class.
This is exactly what we need to do, and here is the basic outline of our inherited class:
Now we need to write the custom paint routine. In order to draw underlines underneath words, we need to know where these words are in the first place. We can get these values using Win32 API functions.
I will not go into detail with these API functions because the code is pretty long and doesn’t make the object of this article. You can download the support class here: Native Win32 Methods for measuring text inside a TextBox (nativemethods.zip)
The custom paint method
This is the CustomPaint() method:
We use the DrawWave() method to draw a wavy line (zig-zag) from one point to another – you can customize it to your needs:
Now to use this class, you just need to instantiate it by passing a TextBox control to the constructor. Make sure you keep a reference to it at the module level so it doesn’t get eaten by the Garbage Collector.
CustomPaintTextBox customUnderlines = new CustomPaintTextBox(textBox1);
I haven’t really tested this stripped out version of the class, but it should work. Most of it is taken directly from the SharpSpell source code.
I hope this article helps you understand how to owner draw native Win32 controls using the .NET framework. If you have any questions please leave a comment.
[tags]Windows.Forms, C#, User Interface, .NET Framework, Win32[/tags]
Bookmark on del.icio.us
on October 9th, 2007 at 2:23 pm
wow thx for posting this. have been researching for several days how to owner-draw a text box only to find that its rly involved (fully custom control from scratch). using your technique i was able to paint a background image no problem.
on October 15th, 2007 at 11:32 am
Great article! Thanks.
on September 4th, 2008 at 7:06 pm
Exactly what I was looking for as well, thanks.
on December 4th, 2008 at 12:34 am
Rather hooking up at the windows message level you should better use WPF Adorners for squigglies (and alike).
on March 4th, 2009 at 3:07 am
show the command
on April 7th, 2009 at 6:41 am
what’s the license on the code listed here? any?
on March 2nd, 2010 at 6:56 am
Sorry for choosing this to leave a offer for all about 283 withdrawal effects from prilosec
[url=http://www.webjam.com/antocal]is prilosec used for duedenal ulcers [/url] prilosec prescribing info [url=http://www.webjam.com/antocal]prilosec pill picture [/url] aciphex 20 mg prilosec pill price prilosec [url=http://www.webjam.com/antocal]prilosec otc capsule [/url] prilosec nexium better gerd
on March 7th, 2010 at 5:12 am
Sorry for choosing this to leave a offer for all about 622 drug company for lasix
[url=http://www.ilike.com/user/BuyLasix7] lasix surgery video [/url] lasix drug interaction is lasix dangerous [url=http://www.ilike.com/user/BuyNolvadex] nolvadex prescribing info [/url] buy cheap generic nolvadex online clenbuterol hcg nolvadex [url=http://www.ilike.com/user/BuyZithromax] zithromax syphilis dosage [/url] zithromax for sinus infection zithromax cures what infections [url=http://www.ilike.com/user/BuyEffexor] effexor wellbutrin combo [/url] effexor teeth clenching does effexor xr dilate pupils [url=http://www.ilike.com/user/BuyAccutane] back pain from accutane [/url] accutane permanent joint pain hair loss while on accutane