Click here to Skip to main content
Licence 
First Posted 17 Mar 2003
Views 120,504
Bookmarked 31 times

Drawing Lines And Rectangles Using the Mouse

By | 17 Mar 2003 | Article
Simple program utilizing the mouse to draw lines and rectangles.

Sample Image - linerect.jpg

Introduction

This is my first program submittal to this site. I wrote this tiny snippet of code to show how to draw simple lines and rectangles in VS.NET using C#. The program is not perfect. The main problem to the procedure is, it doesn't bound the user from drawing beyond the form's boundary. Perhaps when I have time, I'll look into the ability to limit the selection process to the form. One solution would be to check the mouse pointer location on the form and restrict the user from going beyond the form's boundary. If anyone comes up with a better idea, please post your comments or solutions.

There were several submittals to this site regarding drawing selection rectangles and rubberband lines. For those of you who ran across these submittals, you might want to take a look at this tiny program to get some ideas.

The main trick to this program is in the mouse move:

if (((RadioButton)thisform.Controls[0]).Checked) 
{
   ControlPaint.DrawReversibleLine(thisform.PointToScreen(ps), 
                           thisform.PointToScreen(pe), Color.Black);
   pe = new Point(e.X, e.Y);
   ControlPaint.DrawReversibleLine(thisform.PointToScreen(ps), 
                           thisform.PointToScreen(pe), Color.Black);
} 
else 
{
   ControlPaint.DrawReversibleFrame(thisform.RectangleToScreen(SelectRect), 
                           Color.Black, FrameStyle.Dashed);
   SelectRect.Width = e.X - SelectRect.X;
   SelectRect.Height = e.Y - SelectRect.Y;
   ControlPaint.DrawReversibleFrame(thisform.RectangleToScreen(SelectRect), 
                           Color.Black, FrameStyle.Dashed);
}

The DrawReversible procedures toggle the drawing between the specified color and the background color. Also, you will need to attain the screen coordinates in order to begin drawing where the mouse is pointed to on the form.

I hope this program helps beginning control builders to gain some ideas on .NET drawing procedures.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

coolshot26

Web Developer

United States United States

Member

A software programmer hobbyist who have been developing for a number of years spanning multiple languages and platforms.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembertakerman10:12 26 Apr '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey0:04 20 Feb '12  
Generalwe can correct this example by this code Pinmemberyummysms1:12 16 Feb '10  
QuestionWhere is the source code? PinmemberRDurando23:18 5 Nov '08  
AnswerRe: Where is the source code? PinmemberSickness7:11 16 Feb '09  
GeneralCursor.Clip Pinmembertomzhu102422:07 13 Dec '07  
GeneralMore Explaination! Pinmemberrobert1106:00 29 Sep '06  
GeneralRedrawing lines and rectangle Pinmembermitreviper23:49 10 May '04  
GeneralRe: Redrawing lines and rectangle Pinmember
MadHatter ¢
17:53 21 Jan '05  
GeneralStopping th cursor leaving the form PinmemberRoger Allen6:18 19 Mar '03  
GeneralRe: Stopping th cursor leaving the form PinsussAnonymous4:42 5 Jan '04  
GeneralComments. Pinmember73Zeppelin16:14 18 Mar '03  
GeneralRe: Comments. PinsussBill Lien20:58 18 Mar '03  
GeneralRe: Comments. PinmemberVlad Vissoultchev4:37 19 Mar '03  
GeneralArticle format PinmemberSteve McLenithan15:57 18 Mar '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 18 Mar 2003
Article Copyright 2003 by coolshot26
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid