<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coded - Web Development and Programming Blog &#187; SharpSpell</title>
	<atom:link href="http://www.codedblog.com/category/sharpspell/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codedblog.com</link>
	<description>C#, ASP.NET, Google, Remoting, AJAX, Silverlight, Web Development</description>
	<lastBuildDate>Wed, 16 Jul 2008 13:15:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Owner-drawing a Windows.Forms TextBox</title>
		<link>http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/</link>
		<comments>http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/#comments</comments>
		<pubDate>Mon, 17 Sep 2007 12:23:20 +0000</pubDate>
		<dc:creator>Andrei Alecu</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SharpSpell]]></category>
		<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/</guid>
		<description><![CDATA[This article describes how SharpSpell is able to modify existing TextBox controls to display wavy red underlines below misspelled words. Here&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[	<p>This article describes how <a href="http://www.tachyon-labs.com/sharpspell.aspx">SharpSpell</a> is able to modify existing TextBox controls to display wavy red underlines below misspelled words.</p>
	<p>Here&#8217;s an image to demonstrate what I mean:</p>
	<p><img src="http://www.tachyon-labs.com/Portals/0/winspellasyoutype.png" alt="SharpSpell - ASP.NET spell checker"/><br />
(This image is borrowed from <a href="http://www.tachyon-labs.com/sharpspell.aspx">SharpSpell</a>, but you get the point)<span id="more-17"></span></p>
	<h3>Overview</h3>
	<p>The .NET framework provides means to subclass native Win32 windows and controls by inheriting from the <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.nativewindow.aspx">NativeWindow</a> class. </p>
	<p>This is exactly what we need to do, and here is the basic outline of our inherited class:</p>
	<p><textarea name="code" class="csharp">
 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Windows.Forms;
 using System.Drawing;
 namespace CodedBlog
 {
    public class CustomPaintTextBox : NativeWindow
    {
        private TextBox parentTextBox;
        private Bitmap bitmap;
        private Graphics textBoxGraphics;
        private Graphics bufferGraphics;
        // this is where we intercept the Paint event for the TextBox at the OS level
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 15: // this is the WM_PAINT message
                    // invalidate the TextBox so that it gets refreshed properly
                    parentTextBox.Invalidate();
                    // call the default win32 Paint method for the TextBox first
                    base.WndProc(ref m);
                    // now use our code to draw extra stuff over the TextBox
                    this.CustomPaint();
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
        public CustomPaintTextBox(TextBox textBox)
        {
            this.parentTextBox = textBox;
            this.bitmap = new Bitmap(textBox.Width, textBox.Height);
            this.bufferGraphics = Graphics.FromImage(this.bitmap);
            this.bufferGraphics.Clip = new Region(textBox.ClientRectangle);
            this.textBoxGraphics = Graphics.FromHwnd(textBox.Handle);
            // Start receiving messages (make sure you call ReleaseHandle on Dispose):
            this.AssignHandle(textBox.Handle);
        }
        private void CustomPaint()
        {
            // code goes here, see below
        }
    }
 }</textarea></p>
	<p>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.</p>
	<p>I will not go into detail with these API functions because the code is pretty long and doesn&#8217;t make the object of this article. You can download the support class here: <a href='http://www.codedblog.com/wp-content/uploads/2007/09/nativemethods.zip' title='SharpSpell Native Win32 Methods for measuring text inside a TextBox'>Native Win32 Methods for measuring text inside a TextBox (nativemethods.zip)</a></p>
	<h3>The custom paint method</h3>
	<p>This is the <strong>CustomPaint()</strong> method: </p>
	<p><textarea name="code" class="csharp">
 private void CustomPaint()
 {
     // get the index of the first visible line in the TextBox
     int curPos = TextBoxAPIHelper.GetFirstVisibleLine(textBox);
     curPos = TextBoxAPIHelper.GetLineIndex(textBox, curPos);
     // clear the graphics buffer
     bufferGraphics.Clear(Color.Transparent);
     // * Here&#8217;s where the magic happens
     // For simplicity we&#8217;ll just draw a line underneath a character range.
     // This will be from character 1 to character 5 in this sample.
     // You can of course modify this to underline specific words.
     Point start = TextBoxAPIHelper.PosFromChar(textBox, 1);
     Point end = TextBoxAPIHelper.PosFromChar(textBox, 5);
     // The position above now points to the top left corner of the character.
     // We need to account for the character height so the underlines go
     // to the right place.
     end.X += 1;
     start.Y += TextBoxAPIHelper.GetBaselineOffsetAtCharIndex(textBox, 1);
     end.Y += TextBoxAPIHelper.GetBaselineOffsetAtCharIndex(textBox, 5);
     // Draw the wavy underline.
     DrawWave(start, end);
     // Now we just draw our internal buffer on top of the TextBox.
     // Everything should be at the right place.
     textBoxGraphics.DrawImageUnscaled(bitmap, 0, 0);
 }</textarea></p>
	<p>We use the <strong>DrawWave()</strong> method to draw a wavy line (zig-zag) from one point to another &#8211; you can customize it to your needs:</p>
	<p><textarea name="code" class="csharp">
 private void DrawWave(Point start, Point end)
 {
     Pen pen = Pens.Red;
     if ((end.X &#8211; start.X) > 4)
     {
         ArrayList pl = new ArrayList();
         for (int i = start.X; i <= (end.X &#8211; 2); i += 4)
         {
             pl.Add(new Point(i, start.Y));
             pl.Add(new Point(i + 2, start.Y + 2));
         }
         Point [] p = (Point[])pl.ToArray(typeof(Point));
         bufferGraphics.DrawLines(pen, p);
     }
     else 
     {
         bufferGraphics.DrawLine(pen, start, end);
     }
 }</textarea></p>
	<p>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&#8217;t get eaten by the Garbage Collector.</p>
	<p><code>CustomPaintTextBox customUnderlines = new CustomPaintTextBox(textBox1);</code></p>
	<p>I haven&#8217;t really tested this stripped out version of the class, but it should work. Most of it is taken directly from the <a href="http://www.tachyon-labs.com/sharpspell.aspx">SharpSpell</a> source code.</p>
	<p>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.</p>
	<p>[tags]Windows.Forms, C#, User Interface, .NET Framework, Win32[/tags]</p>

 ]]></content:encoded>
			<wfw:commentRss>http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How does SharpSpell work?</title>
		<link>http://www.codedblog.com/2007/08/23/how-does-sharpspell-work/</link>
		<comments>http://www.codedblog.com/2007/08/23/how-does-sharpspell-work/#comments</comments>
		<pubDate>Thu, 23 Aug 2007 17:17:59 +0000</pubDate>
		<dc:creator>Andrei Alecu</dc:creator>
				<category><![CDATA[SharpSpell]]></category>

		<guid isPermaLink="false">http://www.codedblog.com/?p=4</guid>
		<description><![CDATA[Many people are wondering how SharpSpell is able to provide wavy underline spell checking for regular HTML TEXTAREA elements. Other competitors replace TEXTAREAs with editable IFRAMEs to provide the same functionality, but this is an invasive method that we strongly discourage. The trick is to try to replicate the content of the TextArea as best [...]]]></description>
			<content:encoded><![CDATA[	<p>Many people are wondering how <a href="http://www.tachyon-labs.com/sharpspell.aspx" title="Real time ASP.NET AJAX spell checker">SharpSpell</a> is able to provide wavy underline spell checking for regular HTML TEXTAREA elements. Other competitors replace TEXTAREAs with editable IFRAMEs to provide the same functionality, but this is an invasive method that we strongly discourage.</p>
	<p>The trick is to try to replicate the content of the TextArea as best as possible using a DIV that is positioned directly underneath the textbox. The TextArea&#8217;s background is then set to transparent, and the DIV shows up through it.</p>
	<p>We then send the text in the TEXTAREA to the server using AJAX calls, retrieve misspellings and suggestions, and we  then recreate the DIV on the client using CSS to display the underlines.</p>
	<p>[tags]SharpSpell, Spell Checker, JavaScript, AJAX[/tags]</p>

 ]]></content:encoded>
			<wfw:commentRss>http://www.codedblog.com/2007/08/23/how-does-sharpspell-work/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>First Blog Post</title>
		<link>http://www.codedblog.com/2007/08/23/first-blog-post/</link>
		<comments>http://www.codedblog.com/2007/08/23/first-blog-post/#comments</comments>
		<pubDate>Thu, 23 Aug 2007 16:49:41 +0000</pubDate>
		<dc:creator>Andrei Alecu</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[SharpSpell]]></category>

		<guid isPermaLink="false">http://www.codedblog.com/?p=3</guid>
		<description><![CDATA[Howdy, Since everyone seems to be doing it 1, I decided to start my own blog to write about my day to day experiences with web development and programming. 1. yes, I realize that I&#8217;m starting way too late on the blogging phenomenon timeline, but better late than never right 2 ? 2. what better [...]]]></description>
			<content:encoded><![CDATA[	<h3>Howdy,</h3>
	<p>Since everyone seems to be doing it <sup>1</sup>, I decided to start my own blog to write about my day to day experiences with web development and programming.</p>
	<p><sup>1</sup>. yes, I realize that I&#8217;m starting way too late on the blogging phenomenon timeline, but better late than never right <sup>2</sup> ?<br />
<sup>2</sup>. what better way to start a blog than with a clichÃ©?</p>
	<h3>Who am I?</h3>
	<p>My name is Andrei Alecu and I&#8217;m a C# developer with over 5 years of experience in .NET, owner and lead developer of <a href="http://www.tachyon-labs.com/">Tachyon Labs</a>, a company that started as an outsourcing studio but later developed as a .NET component developer and web development company. Tachyon Labs is also the company behind SharpSpell, a real-time spell checker control for ASP.NET and WinForms.<span id="more-3"></span></p>
	<p>We <sup>3</sup> first launched <strong><a href="http://www.tachyon-labs.com/sharpspell.aspx" title="Real time ASP.NET AJAX spell checker">SharpSpell</a></strong> back in 2004, and I was the sole developer of my company back then. The thing about SharpSpell was that it was the first real-time wavy underline spell checker for the Web.</p>
	<p>This amazing <sup>4</sup> functionality was actually implemented by chance. I was approached by <a href="http://www.davefrank.com/" title="Dave Frank's Blog">Dave Frank</a> of the <a href="http://www.botw.org" title="Best of the Web directory">Best of the Web</a> directory, looking to buy a spell check product for their administrative back-end. He eventually suggested that a real-time spell check function like in Microsoft Word would add huge value to the product, but in my own mind I thought something like that would never be possible using pure HTML and JavaScript in a web-browser. Flash maybe, but pure HTML never.</p>
	<p>After a couple of months of work, I managed to get the first working version of a wavy underline spell checker using JavaScript and Internet Explorer&#8217;s DOM model. The code was really messy, but the tech demo made a great impact on the community and I received many words of praise from both interested parties and random people thinking how cool it was.</p>
	<p>Since then the component was rewritten several times, and is now at a state where I could safely say, without false modesty, that it is the most technologically advanced spell check component on the market.</p>
	<p>Over the following blog posts I&#8217;ll describe how I managed to overcome the obstacles I came across when first writing SharpSpell, and other development experiences and setbacks I came across.</p>
	<p><sup>3</sup>. I<br />
<sup>4</sup>. It was definitely &#8220;amazing&#8221; back then, when AJAX was just a term you heard about in technology news feeds, but there weren&#8217;t many concrete examples of what it was and did.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://www.codedblog.com/2007/08/23/first-blog-post/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

