Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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

1 solution

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.
 
Share this answer
 
Comments
Farhan Ghumra 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900