C# picturebox画图 C#控件picturebox实现画图功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C# picturebox画图 C#控件picturebox实现画图功能

无名小卒1990   2021-03-30 我要评论

在Form上添加 一个pictureBox,一个button控件

如图所示:

这样我们的绘画面板就弄好了,把pictureBox的dock属性设置为fill,按键为清屏的作用。

private Point p1, p2;//定义两个点(启点,终点)
private static bool drawing=false;//设置一个启动标志
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  {
    
    p1 = new Point(e.X, e.Y);
    p2 = new Point(e.X, e.Y);
    drawing = true;
   
  }
 
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  {
   drawing = false;
  }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
 
  {
   
   Graphics g = pictureBox1.CreateGraphics();
   if(e.Button ==MouseButtons.Left)
   {
    if (drawing)
    {
     //drawing = true;
     Point currentPoint = new Point(e.X, e.Y);
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
     g.DrawLine(new Pen(Color.Blue, 2), p2,currentPoint);
     
     p2.X = currentPoint.X;
     p2.Y = currentPoint.Y;
    }
 
   }
   
  }
//清屏操作
private void button1_Click(object sender, EventArgs e)
{
 Graphics g = pictureBox1.CreateGraphics();
 g.Clear(Color.White);
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们