C# 图形-绘制线-绘制线并移动它

C# 图形-绘制线-绘制线并移动它,c#,.net,animation,drawing,C#,.net,Animation,Drawing,在.NETC#程序中,我使用文本框中的值绘制了几行代码(我使用DrawLine函数)。我想能够通过点击移动这条线之一,并用鼠标移动这条线-这是可能的吗 public class LineMover : Form { public LineMover() { this.DoubleBuffered = true; this.Paint += new PaintEventHandler(LineMover_Paint); this.MouseMove += ne

在.NETC#程序中,我使用文本框中的值绘制了几行代码(我使用DrawLine函数)。我想能够通过点击移动这条线之一,并用鼠标移动这条线-这是可能的吗

public class LineMover : Form
{
  public LineMover()
  {

    this.DoubleBuffered = true;

    this.Paint += new PaintEventHandler(LineMover_Paint);
    this.MouseMove += new MouseEventHandler(LineMover_MouseMove);
    this.MouseDown += new MouseEventHandler(LineMover_MouseDown);
    this.MouseUp += new MouseEventHandler(LineMover_MouseUp);

    this.Lines = new List<GraphLine>()
    {
      new GraphLine (10, 10, 100, 200),
      new GraphLine (10, 150, 120, 40),
    };
  }

  void LineMover_MouseUp(object sender, MouseEventArgs e)
  {
    if (Moving != null)
    {
      this.Capture = false;
      Moving = null;
    }
    RefreshLineSelection(e.Location);

  }

  void  LineMover_MouseDown(object sender, MouseEventArgs e)
  {
    RefreshLineSelection(e.Location);
    if (this.SelectedLine != null && Moving == null)
    {
      this.Capture = true;
      Moving = new MoveInfo 
       {
          Line = this.SelectedLine, 
          StartLinePoint = SelectedLine.StartPoint, 
          EndLinePoint = SelectedLine.EndPoint, 
          StartMoveMousePoint = e.Location 
       };
    }
    RefreshLineSelection(e.Location);
  }

  void LineMover_Paint(object sender, PaintEventArgs e)
  {
    e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    foreach (var line in Lines)
    {
      var color = line == SelectedLine ? Color.Red : Color.Black;
      var pen = new Pen(color, 2);
      e.Graphics.DrawLine(pen, line.StartPoint, line.EndPoint);
    }
  }
  void LineMover_MouseMove(object sender, MouseEventArgs e)
  {
    if (Moving != null)
    {
      Moving.Line.StartPoint = new PointF(Moving.StartLinePoint.X + e.X - Moving.StartMoveMousePoint.X, Moving.StartLinePoint.Y + e.Y - Moving.StartMoveMousePoint.Y);
      Moving.Line.EndPoint = new PointF(Moving.EndLinePoint.X + e.X - Moving.StartMoveMousePoint.X, Moving.EndLinePoint.Y + e.Y - Moving.StartMoveMousePoint.Y);
    }
    RefreshLineSelection(e.Location);
  }

  private void RefreshLineSelection(Point point)
  {
    var selectedLine = FindLineByPoint(Lines, point);
    if (selectedLine != this.SelectedLine)
    {
      this.SelectedLine = selectedLine;
      this.Invalidate();
    }
    if (Moving != null)
      this.Invalidate();

    this.Cursor =
        Moving != null ? Cursors.Hand :
        SelectedLine != null ? Cursors.SizeAll :
          Cursors.Default;

  }



  public List<GraphLine> Lines = new List<GraphLine>();
  GraphLine SelectedLine = null;
  MoveInfo Moving = null;


  static GraphLine FindLineByPoint(List<GraphLine> lines, Point p)
  {
    var size = 10;
    var buffer = new Bitmap(size * 2, size * 2);
    foreach (var line in lines)
    {
      //draw each line on small region around current point p and check pixel in point p 

      using (var g = Graphics.FromImage(buffer))
      {
        g.Clear(Color.Black);
        g.DrawLine(new Pen(Color.Green, 3), line.StartPoint.X - p.X + size, line.StartPoint.Y - p.Y + size, line.EndPoint.X - p.X + size, line.EndPoint.Y - p.Y + size);
      }

      if (buffer.GetPixel(size, size).ToArgb() != Color.Black.ToArgb())
        return line;
    }
    return null;
  }

  public static void Main()
  {
    Application.Run(new LineMover());
  }
}

public class MoveInfo
{
  public GraphLine Line;
  public PointF StartLinePoint;
  public PointF EndLinePoint;
  public Point StartMoveMousePoint;
}
public class GraphLine
{
  public GraphLine(float x1, float y1, float x2, float y2)
  {
    this.StartPoint = new PointF(x1, y1);
    this.EndPoint = new PointF(x2, y2);
  }
  public PointF StartPoint;
  public PointF EndPoint;
}
公共类LineMover:表单
{
公共线路移动器()
{
this.DoubleBuffered=true;
this.Paint+=新的PaintEventHandler(LineMover\u Paint);
this.MouseMove+=新的MouseEventHandler(LineMover\u MouseMove);
this.MouseDown+=新的MouseEventHandler(LineMover\u MouseDown);
this.MouseUp+=新的MouseEventHandler(LineMover\u MouseUp);
this.Lines=新列表()
{
新GraphLine(10,10,100,200),
新GraphLine(1015012040),
};
}
void LineMover_MouseUp(对象发送器,MouseEventArgs e)
{
如果(正在移动!=null)
{
这个.Capture=false;
移动=空;
}
刷新线路选择(如位置);
}
void LineMover\u MouseDown(对象发送器,MouseEventArgs e)
{
刷新线路选择(如位置);
if(this.SelectedLine!=null&&Moving==null)
{
这个.Capture=true;
移动=新移动信息
{
Line=this.SelectedLine,
StartLinePoint=SelectedLine.StartPoint,
EndLinePoint=SelectedLine.EndPoint,
StartMoveMousePoint=e.位置
};
}
刷新线路选择(如位置);
}
void LineMover_Paint(对象发送器,PaintEventArgs e)
{
e、 Graphics.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;
e、 Graphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
foreach(行中的var行)
{
var color=line==SelectedLine?color.Red:color.Black;
var笔=新笔(颜色,2);
e、 绘图线(笔、线、起点、线、终点);
}
}
void LineMover\u MouseMove(对象发送器,MouseEventArgs e)
{
如果(正在移动!=null)
{
Moving.Line.StartPoint=新点F(Moving.StartLinePoint.X+e.X-Moving.StartMoveMousePoint.X,Moving.StartLinePoint.Y+e.Y-Moving.startMousePoint.Y);
Moving.Line.EndPoint=新点F(Moving.EndLinePoint.X+e.X-Moving.StartMoveMousePoint.X,Moving.EndLinePoint.Y+e.Y-Moving.StartMoveMousePoint.Y);
}
刷新线路选择(如位置);
}
专用空心刷新线选择(点)
{
var selectedLine=FindLineByPoint(线,点);
if(selectedLine!=此.selectedLine)
{
this.SelectedLine=SelectedLine;
这个。使无效();
}
如果(正在移动!=null)
这个。使无效();
这是光标=
移动!=空?光标。手:
SelectedLine!=null?Cursors.SizeAll:
游标。默认值;
}
公共列表行=新列表();
GraphLine SelectedLine=null;
MoveInfo Moving=null;
静态图形线FindLineByPoint(列表线,点p)
{
变量大小=10;
var buffer=新位图(大小*2,大小*2);
foreach(行中的var行)
{
//在当前点p周围的小区域上绘制每条线,并检查点p中的像素
使用(var g=Graphics.FromImage(缓冲区))
{
g、 清晰(颜色为黑色);
g、 DrawLine(新钢笔(颜色为绿色,3),line.StartPoint.X-p.X+尺寸,line.StartPoint.Y-p.Y+尺寸,line.EndPoint.X-p.X+尺寸,line.EndPoint.Y-p.Y+尺寸);
}
if(buffer.GetPixel(size,size.ToArgb()!=Color.Black.ToArgb())
回流线;
}
返回null;
}
公共静态void Main()
{
运行(newlinemover());
}
}
公共类移动信息
{
公共石墨线;
公共点;公共点;
公共点F端点;
公共点开始移动鼠标点;
}
公共类石墨线
{
公共图形线(浮点x1、浮点y1、浮点x2、浮点y2)
{
this.StartPoint=新的点f(x1,y1);
this.EndPoint=新的点f(x2,y2);
}
公共点;起点;
公共点F端点;
}

当然可以。当然你可以想出一个更好的问题?那么怎么做呢?:)我有类似的东西,我想点击其中一行并改变它的位置好吧,到目前为止你做了什么?您是否至少创建了一个行列表,是否编写了MouseDown事件处理程序来检查用户是否单击了一行?你到底在哪里被卡住了?你写了你使用
DrawLine
方法,所以我想你已经画了一条你想用鼠标移动的线了。您还应该创建一个集合,其中包含要检测的每条线两端的坐标。然后,您需要处理
MouseDown
事件(如Hans Passant在前面的评论中所述),以计算鼠标是否在该行上,并处理
MouseMove
事件,并通过在不同位置重新绘制该行来相应地移动该行。如果您有任何问题,请详细描述问题所在。+1是一个很好的工作解决方案,尽管我不理解
CheckLineSelection()
中的逻辑。这非常完美。比我读过的任何“初学者教程”都要好。我一看就明白了这是怎么回事。