c#仿ppt案例

画曲线

c 复制代码
namespace ppt2024
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //存放所有点的位置信息
        List<Point> lstPosition = new List<Point>();

        //控制开始画的时机
        bool isDrawing = false;
        //鼠标点击开始画
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
        }

        //鼠标弹起不画

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
        }

        /// <summary>
        /// pait 方法不会随时调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //画家
            Graphics g = e.Graphics;
            //画线
            if(lstPosition.Count>1)
            {
                g.DrawLines(Pens.Pink, lstPosition.ToArray());
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
             if(isDrawing)
            {
                lstPosition.Add(e.Location);
                //使得paint方法生效
                this.Invalidate();
            }
        }

    
    }
}

画多条线,不连接

c 复制代码
namespace ppt2024
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //用集合存放线的位置信息
        List<HwFreeLine> lstFreeLine = new List<HwFreeLine>();

        //控制开始画的时机
        bool isDrawing = false;

        //鼠标点击开始画
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
            //创建线对象
            HwFreeLine freeLine = new HwFreeLine();
            //设置线的样式----使用随机函数
            Random r = new Random();
            freeLine.color = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
            freeLine.width = r.Next(1,10);

            //集合添加
            lstFreeLine.Add(freeLine);
         
        }

        //鼠标弹起不画
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
         

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //画家
            Graphics g = e.Graphics;
         

            //绘制填充
            for(int i=0;i<lstFreeLine.Count;i++)
            {
                lstFreeLine[i].Draw(g);
            }

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
             if(isDrawing)
            {
                //替换掉集合的最后一个点的位置
                lstFreeLine[lstFreeLine.Count - 1].lstPoints.Add(e.Location);
                //使得paint方法生效
                this.Invalidate();
            }
        }

    }
}

画矩形

可以画多个矩形

c 复制代码
namespace ppt2024
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //存放矩形的位置信息
        List<Rectangle> lstRect = new List<Rectangle>();

        //控制开始画的时机
        bool isDrawing = false;
        Rectangle rect;

        //鼠标点击开始画
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
            rect = new Rectangle();
            //矩形起点
            rect.X = e.X;
            rect.Y = e.Y;

            lstRect.Add(rect);
        }

        //鼠标弹起不画
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
         

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //画家
            Graphics g = e.Graphics;

            for(int i=0;i<lstRect.Count;i++)
            {
                g.DrawRectangle(Pens.Blue, lstRect[i]);

            }

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
             if(isDrawing)
            {

                rect.Width = e.X - rect.X;
                rect.Height = e.Y - rect.Y;
            
                 lstRect[lstRect.Count - 1] = new Rectangle(rect.X, rect.Y, (e.X - rect.X), (e.Y - rect.Y));
          
                //使得paint方法生效
                this.Invalidate();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }
    }
}

画带颜色的矩形

c 复制代码
namespace ppt2024
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //存放矩形的位置信息
        List<Rectangle> lstRect = new List<Rectangle>();
        //存放矩形填充颜色
        Color reactFill = Color.Pink;
        //矩形边框颜色
        Color reactFrame = Color.Gray;
        //矩形边框宽度
        int frameSize = 10;

        //控制开始画的时机
        bool isDrawing = false;
        Rectangle rect;

        //鼠标点击开始画
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
            rect = new Rectangle();
            //矩形起点
            rect.X = e.X;
            rect.Y = e.Y;

            lstRect.Add(rect);
        }

        //鼠标弹起不画
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
         

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //画家
            Graphics g = e.Graphics;
            //画笔
            Pen pen = new Pen(reactFrame, 10);
            //纯色画刷
            SolidBrush solidBrush = new SolidBrush(reactFill);

            //画矩形
            for(int i=0;i<lstRect.Count;i++)
            {
                g.DrawRectangle(pen, lstRect[i]);

            }

            //绘制填充
            for(int i=0;i<lstRect.Count;i++)
            {
                g.FillRectangle(solidBrush, lstRect[i]);
            }

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
             if(isDrawing)
            {

                rect.Width = e.X - rect.X;
                rect.Height = e.Y - rect.Y;
            
                 lstRect[lstRect.Count - 1] = new Rectangle(rect.X, rect.Y, (e.X - rect.X), (e.Y - rect.Y));
          
                //使得paint方法生效
                this.Invalidate();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }


    }
}

画椭圆

仿造之前的矩形

c 复制代码
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //画家
            Graphics g = e.Graphics;
            //画笔
            Pen pen = new Pen(reactFrame, 5);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;

            //纯色画刷
            SolidBrush solidBrush = new SolidBrush(reactFill);

            //画矩形
            for(int i=0;i<lstRect.Count;i++)
            {
                g.DrawEllipse(pen, lstRect[i]);

            }

            //绘制填充
            for(int i=0;i<lstRect.Count;i++)
            {
                g.FillEllipse(solidBrush, lstRect[i]);
            }

        }
相关推荐
柳鲲鹏4 小时前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
lijingguang6 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
专注VB编程开发20年7 小时前
开机自动后台运行,在Windows服务中托管ASP.NET Core
windows·后端·asp.net
¥-oriented7 小时前
【C#中路径相关的概念】
开发语言·c#
ArabySide7 小时前
【WCF】通过AOP实现基于JWT的授权与鉴权的实践
c#·jwt·aop·wcf
xiaowu0808 小时前
C# Task异步的常用方法
c#
阿蒙Amon8 小时前
C# Linq to Objects 详解:集合处理的终极方案
c#·solr·linq
钢铁男儿8 小时前
C# 委托(调用带引用参数的委托)
java·mysql·c#
番茄小能手8 小时前
【全网唯一】C# 纯本地离线文字识别Windows版dll插件
开发语言·c#
葬歌倾城9 小时前
waferMap图像渲染
c#·wpf