Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

Drawing Lines And Rectangles Using the Mouse

Rate me:
Please Sign up or sign in to vote.
4.19/5 (29 votes)
17 Mar 20031 min read 186.8K   7.9K   40   20
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:

C#
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


Written By
Web Developer
United States United States
A software programmer hobbyist who have been developing for a number of years spanning multiple languages and platforms.

Comments and Discussions

 
QuestionForm cleaned when it minimized and remaximized Pin
Member 906895426-Jul-15 7:47
Member 906895426-Jul-15 7:47 
AnswerRe: Form cleaned when it minimized and remaximized Pin
Southmountain5-Sep-22 16:44
Southmountain5-Sep-22 16:44 
GeneralDrawing Lines And Rectangles Using the Mouse in Picture Box Pin
MrKyaw15-Jul-12 22:03
MrKyaw15-Jul-12 22:03 
GeneralRe: Drawing Lines And Rectangles Using the Mouse in Picture Box Pin
Southmountain5-Sep-22 16:46
Southmountain5-Sep-22 16:46 
GeneralMy vote of 5 Pin
Таньо Тодоров Иванов26-Apr-12 10:12
Таньо Тодоров Иванов26-Apr-12 10:12 
I'm waiting to find something like this about 2 weaks! Thank you alot. It is good resource. Thanks again.
GeneralMy vote of 5 Pin
Manoj Kumar Choubey20-Feb-12 0:04
professionalManoj Kumar Choubey20-Feb-12 0:04 
Generalwe can correct this example by this code Pin
yummysms16-Feb-10 1:12
yummysms16-Feb-10 1:12 
GeneralRe: we can correct this example by this code Pin
Southmountain5-Sep-22 16:48
Southmountain5-Sep-22 16:48 
QuestionWhere is the source code? Pin
RDurando5-Nov-08 23:18
RDurando5-Nov-08 23:18 
AnswerRe: Where is the source code? Pin
Sickness16-Feb-09 7:11
Sickness16-Feb-09 7:11 
GeneralCursor.Clip Pin
tomzhu102413-Dec-07 22:07
tomzhu102413-Dec-07 22:07 
GeneralMore Explaination! Pin
robert11029-Sep-06 6:00
robert11029-Sep-06 6:00 
GeneralRedrawing lines and rectangle Pin
mitreviper10-May-04 23:49
mitreviper10-May-04 23:49 
GeneralRe: Redrawing lines and rectangle Pin
Almighty Bob21-Jan-05 17:53
Almighty Bob21-Jan-05 17:53 
GeneralStopping th cursor leaving the form Pin
Roger Allen19-Mar-03 6:18
Roger Allen19-Mar-03 6:18 
GeneralRe: Stopping th cursor leaving the form Pin
Anonymous5-Jan-04 4:42
Anonymous5-Jan-04 4:42 
GeneralComments. Pin
73Zeppelin18-Mar-03 16:14
73Zeppelin18-Mar-03 16:14 
GeneralRe: Comments. Pin
coolshot2618-Mar-03 20:58
coolshot2618-Mar-03 20:58 
GeneralRe: Comments. Pin
Vlad Vissoultchev19-Mar-03 4:37
Vlad Vissoultchev19-Mar-03 4:37 
GeneralArticle format Pin
Steve McLenithan18-Mar-03 15:57
Steve McLenithan18-Mar-03 15:57 

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

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