Click here to Skip to main content
13,044,115 members (117,412 online)
Rate this:
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 2:46am

1 solution

Rate this: bad
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:[^]

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 13 Jun 2012
Copyright © CodeProject, 1999-2017
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