积极答复者
WPF程序,一个矩形怎么根据拖动其左上角来实现其按其中心点旋转?

问题
答案
全部回复
-
你好,
当Rectangle有MouseDown,MouseUp,MouseMove的事件,你可以在这些事件里检测到鼠标位置,根据当前鼠标点、初始的鼠标点和你的已知的中心点的位置我们就可以计算他们之间的角度:
计算方法可以参照这里:
http://bbs.csdn.net/topics/330018316
public static float Angle(Point cen, Point first, Point second) { float dx1, dx2, dy1, dy2; float angle; dx1 = first.X - cen.X; dy1 = first.Y - cen.Y; dx2 = second.X - cen.X; dy2 = second.Y - cen.Y; float c = (float)Math.Sqrt(dx1 * dx1 + dy1 * dy1) * (float)Math.Sqrt(dx2 * dx2 + dy2 * dy2); if (c == 0) return -1; angle = (float)Math.Acos((dx1 * dx2 + dy1 * dy2) / c); return angle; }
而在WPF中我们支持RotateClass http://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform(v=vs.110).aspx
你可以看到如下代码:
// Create a RotateTransform to rotate // the Polyline 45 degrees about the // point (25,50). RotateTransform rotateTransform2 = new RotateTransform(45); rotateTransform2.CenterX = 25; rotateTransform2.CenterY = 50; polyline2.RenderTransform = rotateTransform2;
既然我们有中心点和角度,那么在Up事件里面我们就可以写出这个rectangle的RenderTransform来做成这个旋转。
(这里的点都是针对的客户区的。)
Barry
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- 已编辑 Barry WangModerator 2014年10月31日 9:46