【问题标题】:How to draw line when mouse up?鼠标向上时如何画线?
【发布时间】:2015-06-04 18:54:41
【问题描述】:

我试图在 LMB 向下时设置起点,并在 LMB 向上时从起点到当前鼠标位置画一条线,就像 MSPaint 所做的那样。

我的问题是当 LMB 启动时,我似乎无法让线条出现在图片框上。有人可以请教我吗?

编辑:对不起,伙计们,我意识到问题出在其他地方,但我在此过程中学到了很多东西,感谢所有输入。

public partial class FormPaint : Form
{    
    Point? startPoint = Point.Empty;
    Point? endPoint = Point.Empty;
    bool isMouseDown = new Boolean();

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (Control.MouseButtons == MouseButtons.Left)
        {
            startPoint = e.Location;
            isMouseDown = true;
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {

         brush = new SolidBrush(color);
         using (Graphics g = Graphics.FromImage(pictureBox1.Image))
         {
              g.DrawLine(new Pen(brush), startPoint.Value, endPoint.Value);
              pictureBox1.Invalidate();

         }

        isMouseDown = false;

    }
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
      endPoint = e.Location;
    }
     private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
         using (brush = new SolidBrush(color))
         {
               e.Graphics.DrawLine(new Pen(brush, 5), startPoint.Value, endPoint.Value);
         }
    }
}

【问题讨论】:

  • 只是好奇......为什么你的Points 可以为空?另外,bool isMouseDown = false; 可能更清楚。
  • pictureBox1_MouseMove中你不检查鼠标按钮是否被按下,在任何情况下都给endPoint赋值。
  • MouseMove 期间你也需要失效。
  • @EdS。我正在浏览一些论坛以查找类似的问题并看到使用它的帖子,所以我认为它可能会以某种方式做一些事情
  • 了解程序中的每一行代码是个好主意。这是一篇似乎相关的文章:en.wikipedia.org/wiki/Cargo_cult_programming

标签: c# winforms drawing


【解决方案1】:

当您调用Invalidate 时,它会强制图片框重新绘制。问题在于它会丢弃您之前绘制的所有内容。然后调用图片框上的Paint

我建议将绘图数据保存到一个列表中,并使用该保存的数据在图片框的Paint 事件中执行绘图。

另请阅读How do I draw a circle and line in the picturebox?

【讨论】:

    【解决方案2】:

    带有线条预览的完整示例,尽情享受吧。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WinForm
    {
        public partial class frmMain : Form
        {
            /// <summary>
            /// form constructor
            /// </summary>
            public frmMain()
            {
                InitializeComponent();
            }
    
            private PictureBox imgCanvas;
            private bool isMouseDown;
            private Point startPoint;
            private Point currentPoint;
    
            /// <summary>
            /// form load
            /// </summary>
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
    
                imgCanvas = new PictureBox
                {
                    Location = new Point(8, 8),
                    Size = new Size(this.ClientSize.Width - 16, this.ClientSize.Height - 16),
                    Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom,
                    BorderStyle = BorderStyle.Fixed3D,
                };
                imgCanvas.MouseDown += imgCanvas_MouseDown;
                imgCanvas.MouseUp += imgCanvas_MouseUp;
                imgCanvas.MouseMove += imgCanvas_MouseMove;
                imgCanvas.Paint += imgCanvas_Paint;
                this.Controls.Add(imgCanvas);
            }
    
            void imgCanvas_Paint(object sender, PaintEventArgs e)
            {
                if (isMouseDown)
                {
                    e.Graphics.DrawLine(Pens.Red, startPoint, currentPoint);
                }
            }
    
            void imgCanvas_MouseMove(object sender, MouseEventArgs e)
            {
                if (isMouseDown)
                {
                    currentPoint = e.Location;
                    (sender as PictureBox).Refresh();
                }
            }
    
            void imgCanvas_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    isMouseDown = true;
                    startPoint = e.Location;
                }
            }
    
            void imgCanvas_MouseUp(object sender, MouseEventArgs e)
            {
                if (isMouseDown)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        isMouseDown = false;
    
                        PictureBox pb = sender as PictureBox;
    
                        // create image
                        if (pb.Image == null)
                        {
                            pb.Image = new Bitmap(pb.ClientSize.Width, pb.ClientSize.Height);
                        }
    
                        // draw
                        using (Graphics g = Graphics.FromImage(pb.Image))
                        {
                            g.DrawLine(Pens.Green, startPoint, e.Location);
                            pb.Refresh();
                        }
                    }
                }
            }
        }
    }
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      • 2015-09-25
      • 2012-04-23
      • 2015-04-15
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      相关资源
      最近更新 更多