【问题标题】:How to draw straight line to the mouse coordinates?如何绘制直线到鼠标坐标?
【发布时间】:2014-05-02 09:09:25
【问题描述】:

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

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

【问题讨论】:

  • 复制 MouseDown 代码而不添加到 MouseMove 事件。
  • 请正确阅读问题及其解释

标签: c# winforms


【解决方案1】:

这就是你要找的东西

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

为了方便使用,我使用了Stack,您可以随意更改为您选择的任何集合。

要画多条线,你可以这样做

private Stack<Line> lines = new Stack<Line>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    lines.Push(new Line { Start = e.Location });
}

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

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    foreach (var line in lines)
    {
        e.Graphics.DrawLine(Pens.Black, line.Start, line.End);
    }
}

class Line
{
    public Point Start { get; set; }
    public Point End { get; set; }
}

【讨论】:

  • 你的代码工作正常,但是每当我点击图片框换行时,上一行就会消失我该如何解决它...
  • 所以你需要画N条线?
  • 我需要画正方形、三角形等形状......从点到点......根据用户要求。
  • @GopalNavi 更新了我的答案,如果您需要有关如何绘制其他形状的更多信息,您可以提出新问题,因为这是一个不同的问题 :)
【解决方案2】:

你可以使用提到的代码

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

【讨论】:

  • 我猜问题在 Winforms 中
猜你喜欢
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 2019-05-11
  • 2014-06-18
相关资源
最近更新 更多