需要帮助将中心点分配给折线自定义形状并更改WPF中的位置 [英] Need help to assign centrepoint to a polyline custom shape and change location in WPF

查看:97
本文介绍了需要帮助将中心点分配给折线自定义形状并更改WPF中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我已经在画布上绘制了折线形状,并计算了折线形状的中心点.
此处,折线形状是通过在画布上单击鼠标的位置(点)形成的,并存储在列表中.

现在,我想通过单击另一个按钮时分配另一个中心点来更改折线形状的位置.

如何使用WPF和C#更改折线自定义shape.iam的位置.

Hello,

i have drawn a polyline shape on the canvas and calculated the centrepoint of the polyline shape.
Here the polyline shape is formed with the mouse click locations(points) on the canvas and stored in a list.

Now i want to change the location of the polyline shape by assigning another centre point when clicked on another button.

How can i change the location of the polyline custom shape.iam using WPF and C#.

推荐答案

您具有鼠标单击的目标中心位置.
您具有变量的原始中心位置.
计算偏移量并将其应用于所有点(包括原始中心点)以逻辑地移动折线.
然后Invalidate()您的画布,以便它使用新的坐标重新绘制折线.


偏移量是两点之间的差.确保不要混合尺寸(X值仅从其他X值计算得出)

您可以像处理点一样将偏移量存储在点变量中,尽管这样做可能会使读者感到困惑.

在MyCanvas.cs
You have the target center position where the mouse clicks.
You have the original center position in a variable.
Calculate the offset and apply that to all points (including the original center point) to move the polyline logically.
Then Invalidate() your canvas so that it repaints the polyline using the new co-ordinates.


An offset is the difference between two points. Be sure not to mix dimensions (X-values are calculated solely from other x-values)

You can store an offset in a point variable just as you do with points, although doing so may confuse readers.

In MyCanvas.cs
protected override void OnMouseUp(MouseEventArgs e)
{
    System.Drawing.Point mousePosition = this.PointToClient(Cursor.Position);
    System.Drawing.PointF centerOffset = new PointF(
        mousePosition.X - storedCenter.X,
        mousePosition.Y - storedCenter.Y
    );

    // No foreach here because we're going to alter the list items themselves
    for(int i = 0; i < polyLinePoints.Count; i++)
    {
        polyLinePoints[i] = new PointF(
            polyLinePoints[i].X + centerOffset.X,
            polyLinePoints[i].Y + centerOffset.Y
        );
    }

    storedCenter = mousePosition;

    this.Invalidate();
}



[/Edit]



[/Edit]


这篇关于需要帮助将中心点分配给折线自定义形状并更改WPF中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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