基本信息
源码名称:C# 画葡萄 示例源码(自动填充图形颜色)
源码大小:9.44M
文件格式:.rar
开发语言:C#
更新时间:2018-12-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 1 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using RegisterName;

namespace 画葡萄
{
    public partial class MyDraw : Form
    {
#region 变量定义
        Register reg = new Register(Application.ExecutablePath);

        private List<Line> lines;  //葡萄线列表 
        private Line drawingLine;  //单个葡萄线
        
        Bitmap bmp;  //画板内存   
        private bool DDpic = false;
#endregion

#region 启动创建实例
        public MyDraw()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); //双缓冲
            lines = new List<Line>();
        }

        private void MyDraw_Load(object sender, EventArgs e)
        {
#region 注册代码
#if DEBUG
#else
            if (reg.IsNotReg)
            {
                if (DialogResult.OK == (new FormReg(reg)).ShowDialog())
                    reg.IsNotReg = false;
                else
                {
                    this.Opacity = 0;
                    Application.Exit();
                    return;
                }
            }
#endif
#endregion
            FormFullScreen();//启动时全屏  
            panelpic.Top = 0;
            panelpic.Left = 0;
            panelpic.Width = this.Width;
            panelpic.Height = this.Height;
            panelpic.BackColor = Color.Transparent;

            Drawp.BackColor = Color.Red;
        }
#endregion

#region 鼠标事件,完成实例创建与删除
        /// <summary>
        /// 鼠标按下事件
        /// </左键创建对象、右键删除对象>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelpic_MouseDown(object sender, MouseEventArgs e)//葡萄创建与删除
        {
            Point location = e.Location; //读取鼠标所在点
            if (e.Button == MouseButtons.Left)
            {
                if (!DDpic)
                {
                    drawingLine = new Line(location);
                    lines.Add(drawingLine);
                }
                else DelBmp(location);
            }
            else
            {
                DelBmp(location);
            }
        }        
        /// <summary>
        /// 鼠标移动事件
        /// </画出轨迹>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelpic_MouseMove(object sender, MouseEventArgs e)//画线
        {
            Graphics g = panelpic.CreateGraphics();
            if (e.Button == MouseButtons.Left)
            {
                if (!DDpic) 
                {
                    Point location = e.Location;
                    drawingLine.Add(location);
                    foreach (var line in lines)
                    {
                        line.Draw(g);
                    }
                    g.Dispose();
                }
            }
        }
        /// <summary>
        /// 鼠标松开事件
        /// </填充轨迹图>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelpic_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if(!DDpic)DrawBmp();
            }
        }
#endregion

#region 绘图代码
        /// <summary>
        /// 删除葡萄对象
        /// </输入所在点>
        /// </summary>
        private void DelBmp(Point p)
        {
            foreach (var line in lines)
            {
                if (line.Choose(p))
                {
                    lines.Remove(line); //从列表中移除选中对象
                    break;
                }
            }
            ReDraw();//画布重绘
        }
        /// <summary>
        /// 画布绘图
        /// </summary>
        private void DrawBmp()
        {
            Graphics g = Graphics.FromImage(bmp);
            
            foreach (var line in lines)//绘画实例对象
            {
                line.Draw(g);
                line.Fill(g);
            }
            g.Dispose();
                
            panelpic.CreateGraphics().DrawImage(bmp, 0, 0);
        }
        /// <summary>
        /// 画布重绘
        /// </summary>
        private void ReDraw()
        {
            if (bmp != null) bmp.Dispose();
            bmp = new Bitmap(panelpic.Width, panelpic.Height);//画布背景还原
            DrawBmp();
            panelpic.Invalidate();  //控件重绘
        }
#endregion

#region 全屏操作代码
        Boolean m_IsFullScreen = false;//标记是否全屏
        /// <summary>
        /// 全屏切换
        /// </summary>
        [DllImport("user32.dll", EntryPoint = "ShowCursor", CharSet = CharSet.Auto)]
        public static extern int ShowCursor(bool bShow);
        private void FormFullScreen()
        {
            m_IsFullScreen = !m_IsFullScreen;//点一次全屏,再点还原。  

            if (m_IsFullScreen)//全屏 
            {
                this.SetVisibleCore(false);
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
                this.SetVisibleCore(true);
                //隐藏鼠标
                //ShowCursor(false);
            }
            else//还原 TODO:还原后的窗体应该与全屏前的大小一致
            {
                this.SetVisibleCore(false);
                this.FormBorderStyle = FormBorderStyle.Sizable;
                this.WindowState = FormWindowState.Normal;
                this.SetVisibleCore(true);
                //显示鼠标
                ShowCursor(true);
            }
        }
#endregion

#region other事件处理
        /// <summary>
        /// 键盘按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyDraw_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) FormFullScreen();//按Esc键,全屏切换
            if (e.KeyCode == Keys.Delete)
            {
                lines.Clear();
                ReDraw();//画布重绘
            }
        }
        
        /// <summary>
        /// 控件大小改变事件
        /// </完成重绘>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelpic_SizeChanged(object sender, EventArgs e)
        {
            ReDraw();
        }
        /// <summary>
        /// 控件重绘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelpic_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(bmp, 0, 0);
        }

        private void Drawp_Click(object sender, EventArgs e)
        {
            Delp.BackColor = Color.Transparent;
            DDpic = false;
            Drawp.BackColor = Color.Red;
        }

        private void Delp_Click(object sender, EventArgs e)
        {
            Drawp.BackColor = Color.Transparent;
            DDpic = true;
            Delp.BackColor = Color.Red;
        }

        private void Delp_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                lines.Clear();
                ReDraw();//画布重绘
            }
        }
#endregion
    }
}