Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
GeneralRe: Ellipse Pin
Richard Andrew x6415-Oct-10 17:36
professionalRichard Andrew x6415-Oct-10 17:36 
GeneralRe: Ellipse Pin
WebMaster16-Oct-10 8:24
WebMaster16-Oct-10 8:24 
AnswerRe: Ellipse Pin
Not Active16-Oct-10 8:47
mentorNot Active16-Oct-10 8:47 
GeneralRe: Ellipse Pin
WebMaster16-Oct-10 9:02
WebMaster16-Oct-10 9:02 
GeneralRe: Ellipse Pin
Lamrin17-Oct-10 0:09
Lamrin17-Oct-10 0:09 
GeneralRe: Ellipse Pin
WebMaster17-Oct-10 7:57
WebMaster17-Oct-10 7:57 
GeneralRe: Ellipse Pin
WebMaster17-Oct-10 8:05
WebMaster17-Oct-10 8:05 
GeneralRe: Ellipse Pin
Lamrin17-Oct-10 15:46
Lamrin17-Oct-10 15:46 
Hi Wayne Macdaid,

I think I need to give a little more explanation for my code. And I am sorry that I omitted to mention that you need to drag and drop two buttons on the form! Any way, here is a more detailed explanation of the code:

First create a 'Windows application' project (not console application) in Visual Studio (VS).

By default, you will have one blank form open in VS. Drag and drop two buttons. Name the first button "None" and the second "Ellipse". Now clicking the button1 sets the variable 'SelectedShape' to "None". So the application stops drawing ellipses. When you click button2 it sets the variable 'SelectedShape' to "Ellipse" and the application now starts drawing the ellipse whenever you start clicking and dragging on the form. In short, these buttons switch 'On' and switch 'Off' the drawing of the ellipse. By default, the variable 'SelectedShape' is set to 'None' at the time of initialisation of the application.

Now about the events.

We want to draw an ellipse only when 'SelectedShape' is set to 'Ellipse'. So when you press the mouse button down (see code Form1_MouseDown event)we check whether 'SelectedShape' has been set to "Ellipse". If so we store the X,Y co-ordinates from the mouse event as the top-left co-ords. of the SelectionRectangle.

After you press the left mouse button down, you have to drag the mouse to draw the ellipse ( and also, if needed, the bounding rectangle i.e. SelectionRectangle). When you move the mouse over the form, Windows generates 'MouseMove' events. So we have to handle this event when the mouse moves. We do this inside 'Form1_MouseMove'. Here, we first check if the left mouse button is still down. (Otherwise, the mouse button has been released). If so, we calculate the width and height of the SelectionRectangle using the the top-left co-ords. of the SelectionRectangle and the current mouse pointer position. Now one important point to note here is that we want to show a 'rubberbanding' effect in drawing the rectangle and ellipse. This will actually be done by the 'Form1_Paint' event. Since any changes in the size of the SelectionRectangle should be reflected on the Form immediately. So we first 'Invalidate' the form and then call 'Update' method. This will cause the 'Form1_Paint' event to be executed immediately.

Inside the 'Form1_Paint' event, we first check to see if the 'SelectedShape' is "Ellipse". If it is so, we proceed to draw the rectangle and the ellipse.

When we finish drawing the ellipse and releases the mouse button, we just store the re-calculated width and height of the 'SelectionRectangle' and again call 'Invalidate' and 'Update' and exit.

One important thing to note while doing graphics under Windows is that, Windows re-draws the contents of a window every now and then. i.e. when it is minimized, maximized, moved, is overlapped by any window or part of it etc. When redrawing it clears everything on its surface first. So what has been drawn outside of the window's 'OnPaint' event gets erased. So all drawing should be done inside the 'Paint' event. This is one major error committed by beginners to graphics under Windows.

Hope this clears your doubts.

Happy coding,

Lamrin
GeneralRe: Ellipse Pin
WebMaster18-Oct-10 5:16
WebMaster18-Oct-10 5:16 
GeneralRe: Ellipse Pin
WebMaster20-Oct-10 8:21
WebMaster20-Oct-10 8:21 
GeneralRe: Ellipse Pin
Lamrin23-Oct-10 6:24
Lamrin23-Oct-10 6:24 
GeneralRe: Ellipse Pin
WebMaster24-Oct-10 8:10
WebMaster24-Oct-10 8:10 
GeneralRe: Ellipse Pin
Lamrin25-Oct-10 5:15
Lamrin25-Oct-10 5:15 
GeneralRe: Ellipse Pin
WebMaster25-Oct-10 9:27
WebMaster25-Oct-10 9:27 
QuestionOOB design - class fields Pin
Dell.Simmons15-Oct-10 13:41
Dell.Simmons15-Oct-10 13:41 
AnswerRe: OOB design - class fields Pin
ricmil4215-Oct-10 14:29
ricmil4215-Oct-10 14:29 
GeneralRe: OOB design - class fields Pin
Dell.Simmons15-Oct-10 14:34
Dell.Simmons15-Oct-10 14:34 
AnswerRe: OOB design - class fields Pin
Abhinav S15-Oct-10 22:25
Abhinav S15-Oct-10 22:25 
AnswerRe: OOB design - class fields Pin
Keith Barrow16-Oct-10 5:40
professionalKeith Barrow16-Oct-10 5:40 
AnswerRe: OOB design - class fields Pin
Nish Nishant16-Oct-10 6:27
sitebuilderNish Nishant16-Oct-10 6:27 
AnswerRe: OOB design - class fields Pin
Paul Michalik17-Oct-10 0:45
Paul Michalik17-Oct-10 0:45 
QuestionRead the 'raw' FileVersion and ProductVersion values from a file's version resource. Pin
Blake Miller15-Oct-10 13:02
Blake Miller15-Oct-10 13:02 
QuestionApplication Settings "Save Location" Pin
I Believe In GOD15-Oct-10 10:30
I Believe In GOD15-Oct-10 10:30 
AnswerRe: Application Settings "Save Location" Pin
Ian Shlasko15-Oct-10 10:34
Ian Shlasko15-Oct-10 10:34 
GeneralRe: Application Settings "Save Location" Pin
I Believe In GOD15-Oct-10 10:46
I Believe In GOD15-Oct-10 10:46 

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.