如何在WPF中在Canvas上移动形状? [英] How to move a Shape on Canvas in WPF ?

查看:388
本文介绍了如何在WPF中在Canvas上移动形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我像这样在画布上添加了一个矩形:

Hi,

I added a Rectangle to a Canvas like this:

Canvas.SetTop(myRectangle, 150);
Canvas.SetLeft(myRectangle, 80);
canvas.Children.Add(myRectangle);


现在我想将矩形移到其他位置,例如(100,100).
最好的方法是什么?

谢谢!

我会解释我要做什么.我希望用户能够在画布上绘制圆.首先,用户单击画布"上的某个位置以定义圆心.然后,当鼠标移动时,我想根据当前鼠标位置绘制一个圆(半径是当前鼠标位置与圆的中心之间的距离).我想通过向Canvas.Children中添加一个新的Ellipse,然后移动此Ellipse并随着每次鼠标移动来更改其半径来实现此功能.我走对了吗?

有人可以帮我吗?


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 ?

推荐答案

当您回答自己时,您的问题不再是未回答",这将影响有多少人看起来在它.这也意味着当有人去回复时,他们看不到您的第二条帖子.编辑您的帖子以在需要时添加详细信息,请勿回复.

是的,我认为您的做法正确.
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.


这是一个分为两部分的过程.第一部分;在mousedown上创建圆并将其添加到画布.第二部分;更改圆的半径,直到mouseup事件结束该过程为止.

首先,必须绘制圆形,并且当鼠标相对于圆形中心移动时,mousemove事件必须重新绘制单个圆形. mousedown事件将启动绘图过程.一旦通过mouseup事件选择了所需的圆半径,该圆将被锁定到视图中.

通过保留对circle元素的引用,可以在不考虑画布的情况下调整其属性.因此,在鼠标按下时,将圆添加到画布上.随着鼠标移动,更新圆半径和画布.然后在mouseup事件之后释放圆参考.
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.


这篇关于如何在WPF中在Canvas上移动形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆