Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I added a Rectangle to a Canvas like this:
Canvas.SetTop(myRectangle, 150);
Canvas.SetLeft(myRectangle, 80);
canvas.Children.Add(myRectangle);

Now I want to move the rectangle to other place, say (100, 100).
What is the best way to do this ?

Thanks !

I'll explain what I'm trying to do. I want user to be able to draw circles on a Canvas. First, user clicks somewhere on the Canvas to define circle's center. Then, when the mouse is moved, I would like to draw a circle according to the current mouse position (the radius is the distance between the current mouse position and the circle's center). I thought to implement this functionality by adding a new Ellipse to Canvas.Children, and then to move this Ellipse and change it's radius with each mouse movement. Am I on the right way ?

Can someone help me with this, please ?
Posted
Updated 4-Jan-10 23:15pm
v3

When you answer yourself, your question is no longer 'unanswered', which will affect how many people will look at it. It also means when someone goes to reply, they can't see your second post. Edit your post to add detail if you need to, don't reply.

Yes, I think you're on the right track.
 
Share this answer
 
This is a two part process. Part one; create circle on the mousedown and add it to the canvas. Part two; change the circle radius until a mouseup event ends the process.

First the circle must be drawn and the mousemove event must redraw the single circle as the mouse moves relative to the center of the circle. A mousedown event starts the drawing process. Once the desired circle radius is selected by a mouseup event, the circle will be locked into the view.

By keeping a reference to the circle element, its properties can be adjusted without regard to the canvas. So, on the mousedown, add the circle to the canvas. Update the circle radius and the canvas as the mouse moves. Then release the circle reference after the mouseup event.
 
Share this answer
 

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