Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何绘制鼠标坐标的直线?_C#_Winforms - Fatal编程技术网

C# 如何绘制鼠标坐标的直线?

C# 如何绘制鼠标坐标的直线?,c#,winforms,C#,Winforms,当用户按下左键并移动鼠标时,从上一点到当前鼠标移动位置应显示一条直线(不是永久线)。最后,当用户释放鼠标左键时,会出现一条真正的直线。请帮帮我,我该怎么做 List<Point> points = new List<Point>(); private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseBu

当用户按下左键并移动鼠标时,从上一点到当前鼠标移动位置应显示一条直线(不是永久线)。最后,当用户释放鼠标左键时,会出现一条真正的直线。请帮帮我,我该怎么做

      List<Point> points = new List<Point>();

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
      {
         if (e.Button == MouseButtons.Left)
       {
           points.Add(e.Location);
           pictureBox1.Invalidate();
       }
     }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
     {
         if (points.Count > 1)
         e.Graphics.DrawLines(Pens.Black, points.ToArray());
     }
列表点=新列表();
私有void pictureBox1\u MouseDown(对象发送方,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
添加点(如位置);
pictureBox1.Invalidate();
}
}
私有void pictureBox1_Paint(对象发送方,PaintEventArgs e)
{
如果(点数>1)
e、 图形.绘图线(钢笔.黑色,圆点.ToArray());
}

您可以使用上面提到的代码

 Point currentPoint = new Point();
  private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);
    }

    private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Line line = new Line();

            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(this).X;
            line.Y2 = e.GetPosition(this).Y;

            currentPoint = e.GetPosition(this);

            paintSurface.Children.Add(line);
        }
    }

您可以使用上面提到的代码

 Point currentPoint = new Point();
  private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);
    }

    private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Line line = new Line();

            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(this).X;
            line.Y2 = e.GetPosition(this).Y;

            currentPoint = e.GetPosition(this);

            paintSurface.Children.Add(line);
        }
    }

您可以使用上面提到的代码

 Point currentPoint = new Point();
  private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);
    }

    private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Line line = new Line();

            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(this).X;
            line.Y2 = e.GetPosition(this).Y;

            currentPoint = e.GetPosition(this);

            paintSurface.Children.Add(line);
        }
    }

您可以使用上面提到的代码

 Point currentPoint = new Point();
  private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);
    }

    private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Line line = new Line();

            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(this).X;
            line.Y2 = e.GetPosition(this).Y;

            currentPoint = e.GetPosition(this);

            paintSurface.Children.Add(line);
        }
    }

这就是你要找的

private Stack<Point> points = new Stack<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    points.Clear();
    points.Push(e.Location);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (points.Count > 1)
    {
        points.Pop();
    }
    if (points.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        points.Push(e.Location);
        pictureBox1.Invalidate();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (points.Count > 1)
        e.Graphics.DrawLines(Pens.Black, points.ToArray());
}

这就是你要找的

private Stack<Point> points = new Stack<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    points.Clear();
    points.Push(e.Location);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (points.Count > 1)
    {
        points.Pop();
    }
    if (points.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        points.Push(e.Location);
        pictureBox1.Invalidate();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (points.Count > 1)
        e.Graphics.DrawLines(Pens.Black, points.ToArray());
}

这就是你要找的

private Stack<Point> points = new Stack<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    points.Clear();
    points.Push(e.Location);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (points.Count > 1)
    {
        points.Pop();
    }
    if (points.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        points.Push(e.Location);
        pictureBox1.Invalidate();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (points.Count > 1)
        e.Graphics.DrawLines(Pens.Black, points.ToArray());
}

这就是你要找的

private Stack<Point> points = new Stack<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    points.Clear();
    points.Push(e.Location);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (points.Count > 1)
    {
        points.Pop();
    }
    if (points.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        points.Push(e.Location);
        pictureBox1.Invalidate();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (points.Count > 1)
        e.Graphics.DrawLines(Pens.Black, points.ToArray());
}


复制MouseDown代码而不添加到MouseMove事件。请正确阅读问题及其说明复制MouseDown代码而不添加到MouseMove事件。请正确阅读问题及其说明复制MouseDown代码而不添加到MouseMove事件。请阅读问题及其说明正确复制MouseDown代码而不添加到MouseMove事件。请正确阅读问题及其解释我猜问题在WinForm中我猜问题在WinForm中我猜问题在WinForm中我猜问题在WinForm中我猜问题在WinForm中我猜问题在WinForm中你的代码工作正常,但每当我单击图片框换行时,上一条线将消失我如何解决它…所以你需要画N条线?我需要画正方形、三角形等形状…从一点到另一点…根据用户要求。@GopalNavi更新了我的答案,如果你需要更多关于如何绘制其他形状的信息,你可以问新的问题,因为这是一个不同的问题:)你的代码运行良好,但每当我单击图片框中的新行时,上一行将消失,我如何解决它…所以你需要绘制N条线?我需要绘制方形形状,三角形等。从一点到另一点…。根据用户要求。@GopalNavi更新了我的答案,如果您需要有关如何绘制其他形状的更多信息,您可以提出新问题,因为这是一个不同的问题:)您的代码工作正常,但每当我单击“图片框”以获得新行时,上一条线将消失我如何解决它…所以你需要画N条线?我需要画正方形、三角形等形状…从一点到另一点…根据用户要求。@GopalNavi更新了我的答案,如果你需要更多关于如何绘制其他形状的信息,你可以问新的问题,因为这是一个不同的问题:)你的代码运行良好,但每当我单击图片框中的新行时,上一行将消失,我如何解决它…所以你需要绘制N条线?我需要绘制方形形状,三角形等……从一点到另一点……根据用户要求。@GopalNavi更新了我的答案,如果您需要有关如何绘制其他形状的更多信息,您可以提出新问题,因为这是一个不同的问题:)