首页 > 解决方案 > 在c#中移动不规则形状

问题描述

我目前正在使用 c# PictureBox 并且我已经在图片框中进行了免提绘图,这些图片框是不规则的形状和水平线。如何用鼠标移动选中的不规则形状。

public class IrregularShape
{
    public Color ShapeColor { get; set; }
    public Point Start { get; set; }
    public Point End { get; set; }
    public List<Point> points = new List<Point>();
}

我在鼠标中选择了形状,开始,结束和该形状的点列表。

标签: c#graphics

解决方案


很高兴终于找到了这个。在鼠标向下写下这段代码:

                    isdraggingshape = true;
                    if (selectedshape != null)
                        {
                           OffsetX = selectedshape.Start.X - e.Location.X;
                           OffsetY = selectedshape.Start.Y - e.Location.Y;

                        }   

在鼠标移动中:

          if (isdraggingshape == true)
                {
                    if (OffsetX != null && OffsetY != null)
                    {
                        if (e.Button == MouseButtons.Left)
                        { int new_x1 = e.X + OffsetX;
                            int new_y1 = e.Y + OffsetY;
                            int dx = new_x1 - selectedshape.Start.X;
                            int dy = new_y1 - selectedshape.Start.Y;
                            if (dx == 0 && dy == 0) return;

                            // Move the shape
                            for (int i = 0; i < selectedshape.points.Count; i++)
                            {
                                selectedshape.points[i] = new Point(selectedshape.points[i].X + dx,selectedshape.points[i].Y + dy);
                            }

                            // Redraw.
                            ImagepictureBox.Invalidate();
                        }

推荐阅读