绘图板【或画板】
绘图板【或画板】
杨润康Bla 发表于2年前
绘图板【或画板】
  • 发表于 2年前
  • 阅读 5
  • 收藏 0
  • 点赞 2
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

<img src="http://img.blog.csdn.net/20150419002147281?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWWFuZ1J1bmthbmdCbGE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Draw
{
    public partial class PrintForm : Form
    {
        public PrintForm()
        {
            InitializeComponent();
        }  
        #region   成员变量
        Graphics g = null;//画布,在窗体加载事件中生成
        Pen p = null;//笔,在窗体加载的时间中生成
      //  Brush b = new ;//填充
        Brush b = null;
        Color color = Color.Black;//画笔的颜色,默认为黑色
        string shape = "";//绘什么图形
        float width = 1;//笔的粗细,默认为1
        Point beginPoint = new Point();//开始的点坐标,在鼠标按下事,进行获取
        Point endPoint = new Point();//结束的点坐标,在鼠标释放时,进行获取

        #endregion

        #region 加载事件只负责初始化  设置笔的粗细  颜色的选择
        /// <summary>
        /// 颜色的选择
        /// </summary>
        private void 选择颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //接受用户选择的按钮,这个按钮为颜色控件上的按钮
            DialogResult result= this.colorDialog1.ShowDialog();
            if (result == DialogResult.OK)//如果用户选择了Ok按钮
            {
                color = this.colorDialog1.Color;//获取用户选择的颜色
                
            }
        }

        private void PrintForm_Load(object sender, EventArgs e)
        {
            //在窗体的加载事件中创建Panel控件的Graphics
            g = this.panelDraw.CreateGraphics();
            p = new Pen(color, width);//设置笔的颜色和粗细
         
       
        }

        //设置笔的粗细
        private void toolStripTextBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                width = Convert.ToInt32(this.toolStripTextBox2.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效的内容");
            }
        }

        #endregion

        #region  逻辑的处理
        bool isMouseUp = false;//鼠标是否释放
        bool isMouseDown = false;//鼠标是否按下
     //   bool roundPaint = false;//控制圆只画一次
        #endregion

        //在panel控件上,按下鼠标
        private void panelDraw_MouseDown(object sender, MouseEventArgs e)
        {
            if (!isMouseUp)//在释放鼠标之前,进if
            {
                beginPoint.X = e.X;
                beginPoint.Y = e.Y;
                isMouseUp = true;//记录下来之后,就把鼠标的状态isMouseUp设为true,给下面的鼠标移动使用
                
            }
           
        }

        //在panel控件上,处理鼠标的移动
        private void panelDraw_MouseMove(object sender, MouseEventArgs e)
        {
            if (shape == "")
            {
                convenientlyPaint(sender, e);
                return;
            }
            if (shape == "橡皮擦")
            {
                xiangpicao(sender, e);
                return;
            }

            

            if (shape == "空心圆" && isMouseUp)//光是一个条件,圆会一直重复花,而且会重叠,所以得在来一个条件,就是鼠标释放时,画一次,画最后的一次
            {
                BlankRound(sender,e);
                return;
            }

            
           
           
        }

        #region 随手绘
        /// <summary>
        /// 随手绘制图形
        /// </summary>
        /// <param name="sender"></param>//巧妙的地方 传参
        /// <param name="e"></param>
        public void convenientlyPaint(object sender, MouseEventArgs e)
        {
            if (isMouseUp)//在移动过程中,如果释放
            {
                endPoint.X = e.X;
                endPoint.Y = e.Y;
                g.DrawLine(p, beginPoint, endPoint);
            }
            //重写设置起点---->轨迹
            beginPoint.X = e.X;
            beginPoint.Y = e.Y;
            //如果不设置,是使用默认的,设置了,是使用设置后的
            p.Color = color;
            p.Width = width;
        }


        public void xiangpicao(object sender, MouseEventArgs e)
        {
            if (isMouseUp)//在移动过程中,如果释放
            {
                endPoint.X = e.X;
                endPoint.Y = e.Y;
                g.DrawLine(p, beginPoint, endPoint);
            }
            //重写设置起点---->轨迹
            beginPoint.X = e.X;
            beginPoint.Y = e.Y;
            //如果不设置,是使用默认的,设置了,是使用设置后的
            p.Color = Color.White;
            p.Width = 5;
        }
        

        #endregion

        /// <summary>
        /// 绘制空心圆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BlankRound(object sender, MouseEventArgs e)
        {
           
            if (isMouseUp)//在移动过程中,如果释放
            {
                endPoint.X = e.X;
                endPoint.Y = e.Y;
           }
        }
        
        //当我鼠标释放时,停止绘图--->说白了,一切恢复到初始化之前
        private void panelDraw_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseUp = false;//鼠标是否释放
           
            isMouseDown = false;//鼠标是否按下
            //beginPoint = new Point();//起点坐标初始化,下次点击就有了---------------被这行代码坑死了
            endPoint.X = e.X;
            endPoint.Y = e.Y;

            if (shape == "空心圆")//是可以的
            {
                 p.Color = color;
                p.Width = width;
                g.DrawEllipse(p, beginPoint.X, beginPoint.Y, Math.Abs(beginPoint.X - endPoint.X), Math.Abs(beginPoint.Y - endPoint.Y));
            }

            if (shape == "实心圆")//是可以的
            {
                 p.Color = color;
                 p.Width = width;
                 SolidBrush solidBrush = new SolidBrush(color);
                Rectangle rec=new Rectangle(beginPoint.X, beginPoint.Y, Math.Abs(beginPoint.X - endPoint.X), Math.Abs(beginPoint.Y - endPoint.Y));
                g.FillEllipse(solidBrush,rec);
                g.DrawEllipse(p, beginPoint.X, beginPoint.Y, Math.Abs(beginPoint.X - endPoint.X), Math.Abs(beginPoint.Y - endPoint.Y));
            }

            if( shape == "直线")
            {
                 p.Color = color;
                 p.Width = width;
                g.DrawLine(p, beginPoint, endPoint);
            }
            if (shape == "空心正方形")
            {
                p.Color = color;
                p.Width = width;
                Rectangle rec = new Rectangle(beginPoint.X, beginPoint.Y, Math.Abs(beginPoint.X - endPoint.X), Math.Abs(beginPoint.Y - endPoint.Y));
                g.DrawRectangle(p,rec);
            }

            if (shape == "实心正方形")
            {
                p.Color = color;
                p.Width = width;

                SolidBrush solidBrush = new SolidBrush(color);
                Rectangle rec = new Rectangle(beginPoint.X, beginPoint.Y, Math.Abs(beginPoint.X - endPoint.X), Math.Abs(beginPoint.Y - endPoint.Y));
                g.FillRectangle(solidBrush, rec);
              
                g.DrawRectangle(p, rec);
            }
        }

        #region  用户选择绘制的具体图形

        private void 绘图ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            shape = "空心圆";
        }

        private void toolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            shape = "实心圆";
           
        }

        private void 直线ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            shape = "直线";
        }

        private void 正方形ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            shape = "空心正方形";
        }

        private void 实心正方形ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            shape = "实心正方形";
        }
        

        private void 笔的粗细ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shape = "";
        }
        #endregion

        private void 橡皮擦ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shape = "橡皮擦";
        }

        private void 清空画板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.panelDraw.Invalidate();
           
            
        }
    }
}

共有 人打赏支持
粉丝 15
博文 76
码字总数 0
×
杨润康Bla
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: