【问题标题】:how to move GraphicsPath onMouseMove如何移动 GraphicsPath onMouseMove
【发布时间】:2015-08-04 02:35:27
【问题描述】:

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

   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);

    }

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

【问题讨论】:

    标签: c# winforms transform graphicspath


    【解决方案1】:

    你试过了吗:

    • 创建线和移动线的分离逻辑?我认为您在实现中需要它来区分您正在执行的操作。

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

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

    然后别忘了通过Invalidate()重绘path

    【讨论】:

    • 我当然要分离逻辑,我已经尝试过matrix.Translate( offsetX, offsetY ); ,其中新的偏移量是鼠标位置,但这不能正常工作!
    • 新的偏移量是鼠标位置当然应该是到最后一个位置的距离..!
    • @TaW 我如何获得距离?
    • 你需要跟踪之前的位置!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2014-08-02
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多