如何移动GraphicsPath onMouseMove

时间:2015-08-04 02:35:28

标签: c# winforms transform graphicspath

使用GraphicsPath来绘制Lines所以它就像Pen/Brush中的Paint工具

   public void OnMouseDown(object sender, MouseEventArgs e)
    {
        start = e.Location;
        path.AddLine(e.Location, new Point(e.Location.X, e.Location.Y));
    }

    public void OnMouseMove(object sender, MouseEventArgs e)
    {
        end = e.Location;
        path.AddLine(e.Location, new Point(e.Location.X, e.Location.Y));
    }

    public void OnMouseUp(object sender, MouseEventArgs e)
    {
        end = e.Location;
        path.AddLine(e.Location, new Point(e.Location.X, e.Location.Y));
    }

    public void OnPaint(object sender, Graphics g)
    {
        g.DrawPath(pen, path);
    }

我将如何Relocate,我尝试了Transform方法,但它没有效果..

 public void Relocate(MouseEventArgs e)
    {
        Matrix m = new Matrix();
        m.Translate(start.X + e.X - end.X, start.Y + e.Y - end.Y, MatrixOrder.Append);
        path.Transform(m);

    }

那我该如何以正确的方式做到这一点?移动整个绘制的形状?

1 个答案:

答案 0 :(得分:1)

你试过了吗?

  • 分离创建直线和移动线的逻辑?我认为你需要在实现中区分你正在做的操作。

  • 移动该行时,可以通过path方法转换Relocate(),以下内容就足够了:

    Matrix matrix = new Matrix();
    matrix.Translate( offsetX, offsetY ); //Where offset is the new location
    path.Transform( matrix ); //Transform the path
    

然后不要忘记通过path

重绘Invalidate()