<?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>Farooq Azam &#187; draw</title>
	<atom:link href="http://www.farooqazam.net/tag/draw/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.farooqazam.net</link>
	<description>C# &#38; .NET Programming Blog</description>
	<lastBuildDate>Fri, 23 Jul 2010 20:15:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Draw a Rectangle in C# using Mouse</title>
		<link>http://www.farooqazam.net/draw-a-rectangle-in-c-sharp-using-mouse/</link>
		<comments>http://www.farooqazam.net/draw-a-rectangle-in-c-sharp-using-mouse/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 09:33:19 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[draw]]></category>
		<category><![CDATA[mouse]]></category>
		<category><![CDATA[rectangle]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=62</guid>
		<description><![CDATA[This tutorial will show you how to draw rectangle in C# using Mouse. You can normally draw a rectangle by putting the values manually. But that doesn&#8217;t always work. Sometimes you&#8217;ll require the rectangle to be drawn automatically (without putting values manually). This tutorial will show you how to do that. Let&#8217;s begin: P.S: The [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fdraw-a-rectangle-in-c-sharp-using-mouse%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fdraw-a-rectangle-in-c-sharp-using-mouse%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="alignnone size-full wp-image-65" title="Draw a Rectange in C# using Mouse" src="http://www.farooqazam.net/wp-content/uploads/2009/09/draw-rect.gif" alt="Draw a Rectange in C# using Mouse" width="425" height="294" /></p>
<p>This tutorial will show you how to draw rectangle in C# using Mouse. You can normally draw a rectangle by putting the values manually. But that doesn&#8217;t always work. Sometimes you&#8217;ll require the rectangle to be drawn automatically (without putting values manually). This tutorial will show you how to do that. Let&#8217;s begin:<span id="more-62"></span></p>
<p><strong>P.S:</strong> The source code for this tutorial is available for download. Scroll to the bottom of this post.</p>
<p>It is very simple and it isn&#8217;t as hard as it sounds.</p>
<p>First of all, declare the variable &#8220;rect&#8221;. Declaring it outside all the methods will let us access at anywhere in the current form.</p>
<pre class="brush: csharp;">Rectangle rect;

// Optional
public Form1()
{
   InitializeComponent();
   // Use the cross &quot;+&quot; cursor
   this.Cursor = System.Windows.Forms.Cursors.Cross;
   // This will reduce flicker (Recommended)
   this.DoubleBuffered = true;
}</pre>
<p>Now add <strong>MouseDown</strong> event to your form and use the following code:</p>
<pre class="brush: csharp;">private void Form1_MouseDown(object sender, MouseEventArgs e)
{
   // &quot;e.X&quot; and &quot;e.Y&quot; are used to get MousePositionX and MousePositionY
   rect = new Rectangle(e.X, e.Y, 0, 0);
   this.Invalidate();
}</pre>
<p>The above code sets the X-coordinate and Y-coordinate of  the upper-left corner of the rectangle.</p>
<p>Okay. Now add the <strong>MouseMove</strong> event to the form and use this code:</p>
<pre class="brush: csharp;">private void Form1_MouseMove(object sender, MouseEventArgs e)
{
   // This makes sure that the left mouse button is pressed.
   if (e.Button == MouseButtons.Left)
   {
       // Draws the rectangle as the mouse moves
       rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
   }
   this.Invalidate();
}</pre>
<p>Almost done! Add the <strong>Paint</strong> event to your form and use this code:</p>
<pre class="brush: csharp;">private void Form1_Paint(object sender, PaintEventArgs e)
{
   // Replace &quot;Color.Red&quot; with any color and repalce &quot;2&quot; with any size you like.
   using (Pen pen = new Pen(Color.Red, 2))
   {
      e.Graphics.DrawRectangle(pen, rect);
   }
}</pre>
<p>That&#8217;s it! We are done.</p>
<p>The source code is available for download:</p>
<p><a title="Download Source Code" href="../source/Draw_Rectangle.rar"><strong>Download Source Code</strong></a></p>
<p>If you have any questions please don&#8217;t hesitate to post a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/draw-a-rectangle-in-c-sharp-using-mouse/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
