Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I am developing a whiteboard application in WPF.
 
How can I draw a rectangle and circle/ellipse by dragging the mouse like we do in Paint by dragging the mouse pointer ?
 
I am using WPF canvas for drawing.
 
P.S. : I have refered this article, but it's too complex, i am not able to understand.
Posted 13-Jun-12 3:46am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is a basic article that will help you figure out how to draw these shapes:
 
http://msdn.microsoft.com/en-us/library/ms747393.aspx[^]
 
To draw them using the mouse, just start the drawing on mouse down at the coordinates of the mouse pointer (set that as one corner of the object being drawn). Then set the other set of coordinates as the current location of the mouse pointer (and keep updating it as it moves). When the mouse up event occurs, finalize the other set of coordinates as the current location of the mouse pointer. This way you can click and drag out a box, ellipse, circle, etc. easily.
 
By the way, is this homework? I remember doing an application like this for one of my classes.
  Permalink  
Comments
Farhan Ghumra at 13-Jun-12 9:18am
   
But In WPF there is no property of coordinate for rectangle and circle/ellipse, how can i do this ?
Tim Corey at 13-Jun-12 9:27am
   
Did you read the link I posted? Each item takes a different set of coordinates or values to create the object. You will have to do some math to get them to display, but all that you need is the two sets of mouse coordinates. For instance, if your first set is at (1,1) and your second set is at (5,5) then you know that the item should be 4 high by 4 wide (5-1, 5-1). From there, you can create any shape. To put it on the canvas, you just put it at the coordinates you want. Don't forget that (0,0) on the canvas is the upper left.
Farhan Ghumra at 13-Jun-12 9:29am
   
ok thanks, i will try that, if i will have any query i will reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 200
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 13 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100