Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WPF graphics
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 4-Jan-10 9:38am
Edited 5-Jan-10 0:15am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 16 May 2010
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