在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → c#实现屏幕画笔

c#实现屏幕画笔

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.14M
  • 下载次数:49
  • 浏览次数:513
  • 发布时间:2019-05-20
  • 实例类别:C#语言基础
  • 发 布 人:我是小菜鸟
  • 文件格式:.zip
  • 所需积分:1
 相关标签: C# 屏幕 画笔

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

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;
using System.IO;
using System.Drawing.Imaging;
namespace 尚海涵的小画笔
{     
    public partial class Form1 : Form
    {   

        //定义list泛型集合对象,保存所有图元
        private List<Shape> _listShape = new List<Shape>();

        //定义List泛型集合对象,用于保存被撤销的图元
        private List<Shape> _listTempShape = new List<Shape>();

        //保存当前绘制的临时直线
        private Shape _tempShape = null;

        //保存当前的绘制类型(默认为直线)
        private DrawType _drawType = DrawType.Line;

        //保存bufferdGraphicsContext对象,该对象用来单独分配和管理图形缓冲区
        BufferedGraphicsContext _bufGraphCont = null;

        //保存BufferedGraphics(图形缓冲区)对象
        BufferedGraphics _bufGraph = null;

        //保存当前绘制的线宽为(默认为10)
        private int _drawWidth = 10;

        //保存颜色
        private Color _drawColor = Color.Red;
        //保存当前图形的文件名(默认为空)
        private string _fileName = "";
        //是否需要保存的标记(如果为true,表示需要弹出保存对话框)
        private Boolean _saveFlag = false;
        //保存图形缩放比例(默认为1)
        private double _zoomRatio = 1;
        //保存PanelDraw窗口的初始尺寸
        private Size _panelDrawInitSize = new Size(0, 0);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         //获取当前应用程序的BuferedGraphicsContext对象
          _bufGraphCont = BufferedGraphicsManager.Current;

        //使用于this的相同的像素格式
          _bufGraph = _bufGraphCont.Allocate(this.CreateGraphics(), this.ClientRectangle);
        //使用于this.createGraphics()相同的像素格式来创建指定大小的图像缓冲区
            _bufGraph = _bufGraphCont.Allocate(this.CreateGraphics(),this.ClientRectangle);
        //清空图形缓冲区
            _bufGraph.Graphics.Clear(Color.White);

        //设置抗锯齿平衡模式
            _bufGraph.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        //保存panelDraw窗口的初始尺寸
            _panelDrawInitSize.Width = panelDraw.Width;
            _panelDrawInitSize.Height = panelDraw.Height;


        //禁用menuitemUndo菜单
            MenuItemUndo.Enabled = false;
            toolStripButtonUndo.Enabled = false;

            //禁用menuitemUndo菜单
            MenuItemRedo.Enabled = false;
            toolStripButtonRedo.Enabled = false;
          //以“pencil.ur”
            Cursor penCur = new Cursor("pencil.cur");
            this.Cursor = penCur;
            //把菜单条,工具条,状态条的光标设置为默认的箭头光标
            menuStrip1.Cursor = Cursors.Arrow;
            toolStrip1.Cursor = Cursors.Arrow;
            statusStrip1.Cursor = Cursors.Arrow;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (_drawType != DrawType.Stop)
            {
                if (_drawType == DrawType.Line)
                {
                    _tempShape = new Line();
                    ((Line)_tempShape)._P1 = new Point(e.X, e.Y);
                
                }
                //***************************************
                else if (_drawType == DrawType.Rectangle)
                {
                    _tempShape = new Rectangle();
                    ((Rectangle)_tempShape)._P1 = new Point(e.X, e.Y);
                
                }
                //***************************************
                else if (_drawType == DrawType.Rectangle)
                {
                    _tempShape = new Rectangle();
                    ((Rectangle)_tempShape)._P1 = new Point(e.X, e.Y);

                } 
                //**********************************************
                else if (_drawType == DrawType.Circle)
                {
                    _tempShape = new Circle();
                    ((Circle)_tempShape)._PCenter = new Point(e.X, e.Y);

                }
                else if (_drawType == DrawType.Sketch)
                {
                    _tempShape = new Sketch();
                    ((Sketch)_tempShape)._PointList.Add(new Point(e.X, e.Y));

                } 


                //保存画笔线宽和颜色到临时图元(_tempShape中)
                _tempShape._PenWidth = _drawWidth;
                _tempShape._PenColor = _drawColor;

            }
        
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (_drawType != DrawType.Stop)
            {
                if (_drawType == DrawType.Line)
                    ((Line)_tempShape)._P2 = new Point(e.X,e.Y);
                
                //***************************************
                else if (_drawType == DrawType.Rectangle)

                    ((Rectangle)_tempShape)._P2 = new Point(e.X, e.Y);
                //***************************************
                else if (_drawType == DrawType.Circle)

                    ((Circle)_tempShape)._R = (float)Math.Sqrt(Math.Pow((e.X-((Circle)_tempShape)._PCenter.X),2) 
                                                                Math.Pow((e.Y-((Circle)_tempShape)._PCenter.Y),2));

                else if(_drawType == DrawType.Sketch)
                    ((Sketch)_tempShape)._PointList.Add(new Point(e.X, e.Y));
                //将改图元添加到_listShape集合中
                _listShape.Add(_tempShape);
                //设置保存标记为true
                _saveFlag = true;

                //清空
                _listTempShape.Clear();
                //禁止
                MenuItemRedo.Enabled = false;
                toolStripButtonRedo.Enabled = false;


                //启用MenuItemUndo菜单
                MenuItemUndo.Enabled = true;
                toolStripButtonUndo.Enabled = true;
                //清空图形缓冲区
                _bufGraph.Graphics.Clear(Color.White);
                //逐一绘制所有图元到图形缓冲区
                foreach (Shape shape in _listShape)
                    shape.Draw(_bufGraph.Graphics,DashStyle.Solid,_zoomRatio);
                //将图形缓冲区的图元绘制到当前窗口
                _bufGraph.Render(this.CreateGraphics());

            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //清空图形缓冲区
            _bufGraph.Graphics.Clear(Color.White);
            //逐一绘制所有图元到圆形缓冲区
            foreach (Shape shape in _listShape)
                shape.Draw(_bufGraph.Graphics,DashStyle.Solid,_zoomRatio);
            //将图形缓冲区的图元绘制到当前窗口
            _bufGraph.Render(this.CreateGraphics());

        }

        private void MenuItemLine_Click(object sender, EventArgs e)
        {
            _drawType = DrawType.Line;
        }

        private void MenuItemRectangle_Click(object sender, EventArgs e)
        {
            _drawType = DrawType.Rectangle;
        }

        private void MenuItemCircle_Click(object sender, EventArgs e)
        {
            _drawType = DrawType.Circle;
        }

        private void MenuItemStop_Click(object sender, EventArgs e)
        {
            _drawType = DrawType.Stop;
        }

        private void MenuItemDrawing_Click(object sender, EventArgs e)
        {

        }
       private void Form1_MouseMove(object sender, MouseEventArgs e)
        {   

            //判断鼠标左键是否处于下压状态
            if (e.Button == MouseButtons.Left)
            {
                if (_drawType != DrawType.Stop)
                {
                    //清空图形缓冲区
                    _bufGraph.Graphics.Clear(Color.White);
                    //逐一绘制所有图元到图形缓冲区
                    foreach (Shape shape in _listShape)
                        shape.Draw(_bufGraph.Graphics, DashStyle.Solid,_zoomRatio);
                    //根据绘图类型,保存相应的信息到临时图元(_tempShape)中
                    if (_drawType == DrawType.Line)
                        ((Line)_tempShape)._P2 = e.Location;
                    else if (_drawType == DrawType.Rectangle)
                        ((Rectangle)_tempShape)._P2 = e.Location;
                    else if (_drawType == DrawType.Circle)
                        ((Circle)_tempShape)._R = (float)Math.Sqrt(Math.Pow((e.X - ((Circle)_tempShape)._PCenter.X), 2)  
                                                                    Math.Pow((e.Y - ((Circle)_tempShape)._PCenter.Y), 2));
                    else if (_drawType == DrawType.Sketch)
                        ((Sketch)_tempShape)._PointList.Add(e.Location);
                }
                //绘制当前的临时图元(_tempShape)到图形缓冲区
                _tempShape.Draw(_bufGraph.Graphics,DashStyle.Dash,_zoomRatio);
                //将图形缓冲区的图元绘制到当前窗口
                _bufGraph.Render(this.CreateGraphics());

            }
            //显示鼠标的坐标
            StatusLabelMousePosition.Text = "鼠标:x="   e.X   ",y="   e.Y;
        }

        private void MenuItemWidth_Click(object sender, EventArgs e)
        {
            //新建一个线宽对话框对象
            DlgPenWidth dlgPenWidth = new DlgPenWidth();
            //设置线宽对话框默认显示的线宽值
            dlgPenWidth.numericUpDownWidth.Value = _drawWidth;
            //显示线宽对话框,判断是否电击了ok按钮
            if (dlgPenWidth.ShowDialog(this) == DialogResult.OK)
            { 
                //保存用户设置的线宽到_drawWidth
                _drawWidth = (int)(dlgPenWidth.numericUpDownWidth.Value);
            
            }


        }

        private void MenuItemColor_Click(object sender, EventArgs e)
        {
            //设置颜色对话框为默认选中的颜色
            colorDialog1.Color = _drawColor;
            //显示颜色对话框,判断是否点击了ok按钮
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
            { 
                //保存用户设置的颜色到_drawColor
                _drawColor = colorDialog1.Color;
            
            }
        }

        private void MenuItemUndo_Click(object sender, EventArgs e)
        {
            if (_listShape.Count != 0)
            {   
                //在删除之前,先把_listShape中的最后一个图元保存到_listTempShape
                _listTempShape.Add(_listShape[_listShape.Count-1]);
                //删除_listShape中的最后一个图元
                _listShape.RemoveAt(_listShape.Count - 1);
                //清空图形缓冲区
                _bufGraph.Graphics.Clear(Color.White);
                //逐一绘制所有图元到图形缓冲区
                foreach (Shape shape in _listShape)
                    shape.Draw(_bufGraph.Graphics, DashStyle.Solid,_zoomRatio);
                //将图形缓冲区的内容绘制到主窗口
                _bufGraph.Render(panelDraw.CreateGraphics());
                //判断是否需要禁用MenuItemUndo菜单
                if (_listShape.Count == 0)
                {
                    MenuItemUndo.Enabled = false;
                    toolStripButtonUndo.Enabled = false;

                }
                //启用MenuItemRedo菜单
                MenuItemRedo.Enabled = true;
                toolStripButtonRedo.Enabled = true;
                _saveFlag = true;
            }
        }
        private void MenuItemRedo_Click(object sender, EventArgs e)
        {
            //判断_listTempShape中是否有图元
            if(_listTempShape.Count!=0)
            {
                _listShape.Add(_listTempShape[_listTempShape.Count - 1]);

                //删除_listTempShape中的最后一个图元
                _listTempShape.RemoveAt(_listTempShape.Count - 1);
                //清空图形缓冲区
                _bufGraph.Graphics.Clear(Color.White);
                 //逐一绘制所有图元到图形缓冲区
                foreach (Shape shape in _listShape)
                    shape.Draw(_bufGraph.Graphics, DashStyle.Solid,_zoomRatio);
                //将图形缓冲区的图元绘制到当前窗口
                _bufGraph.Render(panelDraw.CreateGraphics());
                //判断是否需要禁用MenuItemUndo菜单
                if (_listTempShape.Count == 0)
                {
                    MenuItemRedo.Enabled = false;
                    toolStripButtonRedo.Enabled = false;
                }

                //启用MenuItemRedo菜单
                MenuItemUndo.Enabled = true;
                toolStripButtonUndo.Enabled = true;
                _saveFlag = true;
            
            }

        }

        private void ToolStripMenuItem1px_Click(object sender, EventArgs e)
        {
            _drawWidth = 1;
        }

        private void ToolStripMenuItem2px_Click(object sender, EventArgs e)
        {
            _drawWidth = 2;
        }

        private void ToolStripMenuItem4px_Click(object sender, EventArgs e)
        {
            _drawWidth = 4;
        }

        private void ToolStripMenuItem8px_Click(object sender, EventArgs e)
        {
            _drawWidth = 8;
        }

        private void ToolStripMenuItemRed_Click(object sender, EventArgs e)
        {
            _drawColor = Color.Red;
        }

        private void ToolStripMenuItemGreen_Click(object sender, EventArgs e)
        {
            _drawColor = Color.Green;
        }

        private void ToolStripMenuItemBlue_Click(object sender, EventArgs e)
        {
            _drawColor = Color.Blue;
        }

        private void ToolStripMenuItemYellow_Click(object sender, EventArgs e)
        {
            _drawColor = Color.Yellow;
        }

        private void ToolStripMenuItemBlack_Click(object sender, EventArgs e)
        {
            _drawColor = Color.Black;
        }

        private void MenuItemNew_Click(object sender, EventArgs e)
        {

            if (_saveFlag == true)
            {
                if (MessageBox.Show("图形已经改变啦,您需要保存吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    MenuItemSave_Click(null, null);
            }
            //清空图元
            _listShape.Clear();
            _listTempShape.Clear();

            //清空缓冲区
            _bufGraph.Graphics.Clear(Color.White);
            _bufGraph.Render(this.CreateGraphics());

            //清空当前图形的文件名
            _fileName = "";
            this.Text = "画笔--无标题";
            //禁用按钮
            _saveFlag = false;

            MenuItemUndo.Enabled = false;
            toolStripButtonUndo.Enabled = false;
            MenuItemRedo.Enabled = false;
            toolStripButtonRedo.Enabled = false;

        }

        private void MenuItemSaveAs_Click(object sender, EventArgs e)
        {
            //显示文件保存对话框,设置另存的文件名
            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                //保存用户设置的文件名
                _fileName = saveFileDialog1.FileName;
                this.Text = "画笔"   _fileName;
                //创建一个文件流对象,用于写入图形信息
                FileStream fs = new FileStream(_fileName, FileMode.Create);
                //创建一个与文件流对象相应的二进制写入流对象
                BinaryWriter bw = new BinaryWriter(fs);
            
                //把图元数量写入到文件
                bw.Write(_listShape.Count);
                //逐一把图元信息写入到文件
                foreach (Shape tempShape in _listShape)
                { 
                    //把图元类型
                    bw.Write(tempShape.GetType().ToString());
                    //把图元信息写入到文件中
                    tempShape.Write(bw);              
                }
                //关闭有关文件流对象
                bw.Close();
                fs.Close();
                _saveFlag = false;
            
            }

        }

        private void MenuItemOpen_Click(object sender, EventArgs e)
        {    
              //调用菜单‘新建’
                MenuItemNew_Click(null, null);    
            //显示文件打开对话框
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {       
                //保存用户选择的文件名
                _fileName = openFileDialog1.FileName;
                this.Text = "画笔"   _fileName;
                //创建一个文件流对象,读出
                FileStream fs = new FileStream(_fileName, FileMode.Open, FileAccess.Read);
                //创建一个与文件流对象相对应的二进制读入对象
                BinaryReader br = new BinaryReader(fs);               
                //在文件中读取图元数量
                int shapeCount = br.ReadInt32();
                //逐一从文件中读取图元信息
                for (int i = 0; i < shapeCount; i  )
                {
                    //从文件中读取
                    string ShapeType = br.ReadString();
                    //如果是直线
                    if (ShapeType == "尚海涵的小画笔.Line")
                    {
                        Line shape = new Line();
                        shape.Read(br);
                        _listShape.Add(shape);


                    }
                    else if (ShapeType == "尚海涵的小画笔.Rectangle")
                    {
                        Rectangle shape = new Rectangle();
                        shape.Read(br);
                        _listShape.Add(shape);

                    }
                    else if (ShapeType == "尚海涵的小画笔.Circle")
                    {
                        Circle shape = new Circle();
                        shape.Read(br);
                        _listShape.Add(shape);

                    }
                    else if (ShapeType == "尚海涵的小画笔.Sketch")
                    {
                        Sketch shape = new Sketch();
                        shape.Read(br);
                        _listShape.Add(shape);

                    }

                    else
                        //弹出信息提示框
                        MessageBox.Show("图元类型错误。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
              
                }
                //关闭有关文件对象 
                br.Close();
                fs.Close();
                _saveFlag = false;
                //清空图形缓冲区
                _bufGraph.Graphics.Clear(Color.White);
                //逐一绘制所有图形到图形缓冲区
                foreach (Shape shape in _listShape)
                    shape.Draw(_bufGraph.Graphics, DashStyle.Solid,_zoomRatio);
                //将图形缓冲区的图元绘制到当前窗口
                _bufGraph.Render(this.CreateGraphics());
            }
        }

        private void MenuItemSave_Click(object sender, EventArgs e)
        {

            //如果_fileName为空
            if (_fileName == "")
            { 
                //显示文件保存对话框 
                if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    //保存用户选择的文件名
                    _fileName = saveFileDialog1.FileName;
                    //设置窗口颜色
                    this.Text = "画笔"   _fileName;

                }
                else
                    return;
            
            }

            //创建一个文件流对象,
            FileStream fs = new FileStream(_fileName,FileMode.Create);
            //创建一个与文件流对相应的二进制写入刘对象
            BinaryWriter bw = new BinaryWriter(fs);

            //把图元数量写入到文件中
            bw.Write(_listShape.Count);
            //逐一把图源信息写入到文件中
            foreach (Shape tempShape in _listShape)
            {
                bw.Write(tempShape.GetType().ToString());
                tempShape.Write(bw);
                      
            }
            //关闭
            bw.Close();
            fs.Close();
            _saveFlag = false;

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

            if (_saveFlag == true)
            {
                if (MessageBox.Show("图形已经改变啦,您需要保存吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    MenuItemSave_Click(null, null);
            }



        }

        private void MenuItemclose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MenuItemSaveAsPic_Click(object sender, EventArgs e)
        {
            if (saveFileDialog2.ShowDialog(this) == DialogResult.OK)
            { 
                //创建一个位图对象,其尺寸与窗口尺寸适应
                Bitmap bitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
                //获取位图的Graphics的对象
                Graphics gBitmap = Graphics.FromImage(bitmap);
                //把图像缓冲区写入到位图的Graphics对象
                _bufGraph.Render(gBitmap);

                //获取图片文件的后缀名
                string extension = System.IO.Path.GetExtension(saveFileDialog2.FileName);
                //根据后缀名 ,存储为相对应的格式
                if (extension == ".jpg")
                    bitmap.Save(saveFileDialog2.FileName, ImageFormat.Jpeg);
                else if (extension == ".gif")
                    bitmap.Save(saveFileDialog2.FileName, ImageFormat.Gif);
                else if (extension == ".bmp")
                    bitmap.Save(saveFileDialog2.FileName, ImageFormat.Bmp);
                else
                    MessageBox.Show("对不起,暂时不支持该图片格式。", extension, MessageBoxButtons.OK, MessageBoxIcon.Error);
            
            }

        }

        private void MenuItemSketch_Click(object sender, EventArgs e)
        {
            _drawType = DrawType.Sketch;
        }

        private void panelDraw_MouseDown(object sender, MouseEventArgs e)
        {
            if (_drawType != DrawType.Stop)
            {
                if (_drawType == DrawType.Line)
                {
                    _tempShape = new Line();
                    ((Line)_tempShape)._P1 = new Point((int)(e.X / _zoomRatio), (int)(e.Y / _zoomRatio));

                }
                //***************************************
                else if (_drawType == DrawType.Rectangle)
                {
                    _tempShape = new Rectangle();
                    ((Rectangle)_tempShape)._P1 = new Point((int)(e.X / _zoomRatio), (int)(e.Y / _zoomRatio));

                }
                //***************************************
                else if (_drawType == DrawType.Rectangle)
                {
                    _tempShape = new Rectangle();
                    ((Rectangle)_tempShape)._P1 = new Point((int)(e.X / _zoomRatio), (int)(e.Y / _zoomRatio));

                }
                //**********************************************
                else if (_drawType == DrawType.Circle)
                {
                    _tempShape = new Circle();
                    ((Circle)_tempShape)._PCenter = new Point((int)(e.X / _zoomRatio), (int)(e.Y / _zoomRatio));

                }
                else if (_drawType == DrawType.Sketch)
                {
                    _tempShape = new Sketch();
                    ((Sketch)_tempShape)._PointList.Add(new Point((int)(e.X / _zoomRatio), (int)(e.Y / _zoomRatio)));

                }


                //保存画笔线宽和颜色到临时图元(_tempShape中)
                _tempShape._PenWidth = _drawWidth;
                _tempShape._PenColor = _drawColor;

            }


        }

        private void panelDraw_MouseMove(object sender, MouseEventArgs e)
        {
            
            //判断鼠标左键是否处于下压状态
            if (e.Button == MouseButtons.Left)
            {
                if (_drawType != DrawType.Stop)
                {
                    //清空图形缓冲区
                    _bufGraph.Graphics.Clear(Color.White);
                    //逐一绘制所有图元到图形缓冲区
                    foreach (Shape shape in _listShape)
                        shape.Draw(_bufGraph.Graphics, DashStyle.Solid,_zoomRatio);
                    //根据绘图类型,保存相应的信息到临时图元(_tempShape)中
                    if (_drawType == DrawType.Line)
                        ((Line)_tempShape)._P2 = new Point((int)(e.X / _zoomRatio),(int)(e.Y / _zoomRatio));
                    else if (_drawType == DrawType.Rectangle)
                        ((Rectangle)_tempShape)._P2 = new Point((int)(e.X / _zoomRatio),(int)(e.Y / _zoomRatio));
                    else if (_drawType == DrawType.Circle)
                        ((Circle)_tempShape)._R = (float)Math.Sqrt(Math.Pow(((int)(e.X/_zoomRatio) - ((Circle)_tempShape)._PCenter.X), 2)  
                                                                    Math.Pow(((int)(e.Y/_zoomRatio) - ((Circle)_tempShape)._PCenter.Y), 2));
                    else if (_drawType == DrawType.Sketch)
                        ((Sketch)_tempShape)._PointList.Add(new Point((int)(e.X/_zoomRatio),(int)(e.Y/_zoomRatio)));
                }
                //绘制当前的临时图元(_tempShape)到图形缓冲区
                _tempShape.Draw(_bufGraph.Graphics,DashStyle.Dash,_zoomRatio);
                //将图形缓冲区的图元绘制到当前窗口
                _bufGraph.Render(panelDraw.CreateGraphics());

            }
            //显示鼠标的坐标
            StatusLabelMousePosition.Text = "鼠标:x="   e.X   ",y="   e.Y;
        }

        private void panelDraw_MouseUp(object sender, MouseEventArgs e)
        {
            if (_drawType != DrawType.Stop)
            {
                if (_drawType == DrawType.Line)
                    ((Line)_tempShape)._P2 = new Point((int)(e.X / _zoomRatio), (int)(e.Y / _zoomRatio));

                //***************************************
                else if (_drawType == DrawType.Rectangle)

                    ((Rectangle)_tempShape)._P2 = new Point((int)(e.X / _zoomRatio), (int)(e.Y / _zoomRatio));
                //***************************************
                else if (_drawType == DrawType.Circle)

                    ((Circle)_tempShape)._R = (float)Math.Sqrt(Math.Pow(((int)(e.X / _zoomRatio) - ((Circle)_tempShape)._PCenter.X), 2)  
                                                                Math.Pow(((int)(e.Y / _zoomRatio) - ((Circle)_tempShape)._PCenter.Y), 2));

                else if (_drawType == DrawType.Sketch)
                    ((Sketch)_tempShape)._PointList.Add(new Point((int)(e.X / _zoomRatio), (int)(e.Y / _zoomRatio)));
                //将改图元添加到_listShape集合中
                _listShape.Add(_tempShape);
                //设置保存标记为true
                _saveFlag = true;

                //清空
                _listTempShape.Clear();
                //禁止
                MenuItemRedo.Enabled = false;
                toolStripButtonRedo.Enabled = false;
                //启用MenuItemUndo菜单
                MenuItemUndo.Enabled = true;
                toolStripButtonUndo.Enabled = true;
                //清空图形缓冲区
                _bufGraph.Graphics.Clear(Color.White);
                //逐一绘制所有图元到图形缓冲区
                foreach (Shape shape in _listShape)
                    shape.Draw(_bufGraph.Graphics, DashStyle.Solid, _zoomRatio);
                //将图形缓冲区的图元绘制到当前窗口
                _bufGraph.Render(panelDraw.CreateGraphics());

            }
        }

        private void panelDraw_Paint(object sender, PaintEventArgs e)
        {

            //清空图形缓冲区
            _bufGraph.Graphics.Clear(Color.White);
            //逐一绘制所有图元到圆形缓冲区
            foreach (Shape shape in _listShape)
                shape.Draw(_bufGraph.Graphics, DashStyle.Solid,_zoomRatio);
            //将图形缓冲区的图元绘制到当前窗口
            _bufGraph.Render(panelDraw.CreateGraphics());


        }

        private void StripMenuItemZoomIn_Click(object sender, EventArgs e)
        {
            //保存缩放比例
            _zoomRatio = _zoomRatio * 1.1;
            //设置panelDraw的宽度和高度
            panelDraw.Width = (int)(_panelDrawInitSize.Width * _zoomRatio);
            panelDraw.Height = (int)(_panelDrawInitSize.Height * _zoomRatio);
            //使用于panelDraw相同的像素格式来创建指定大小的图形缓冲区
            _bufGraph = _bufGraphCont.Allocate(panelDraw.CreateGraphics(), panelDraw.ClientRectangle);
            //设置抗锯齿平滑模式
            _bufGraph.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            //清空缓冲区
            _bufGraph.Graphics.Clear(Color.White);
            //逐一绘制
            foreach (Shape shape in _listShape)
                shape.Draw(_bufGraph.Graphics, DashStyle.Solid,_zoomRatio);
            _bufGraph.Render(panelDraw.CreateGraphics());


        }

        private void MenuItemZoomOut_Click(object sender, EventArgs e)
        {
            //保存缩放比例
            _zoomRatio = _zoomRatio * 0.9;
            //设置panelDraw的宽度和高度
            panelDraw.Width = (int)(_panelDrawInitSize.Width * _zoomRatio);
            panelDraw.Height = (int)(_panelDrawInitSize.Height * _zoomRatio);
            //使用于panelDraw相同的像素格式来创建指定大小的图形缓冲区
            _bufGraph = _bufGraphCont.Allocate(panelDraw.CreateGraphics(), panelDraw.ClientRectangle);
            //设置抗锯齿平滑模式
            _bufGraph.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            //清空缓冲区
            _bufGraph.Graphics.Clear(Color.White);
            //逐一绘制
            foreach (Shape shape in _listShape)
                shape.Draw(_bufGraph.Graphics, DashStyle.Solid,_zoomRatio);
            _bufGraph.Render(panelDraw.CreateGraphics());

        }

        private void MenuItemScreenPen_Click(object sender, EventArgs e)
        {




        }







    }
}

标签: C# 屏幕 画笔

实例下载地址

c#实现屏幕画笔

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警