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]);
            }

        }
相关推荐
研华嵌入式3 小时前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
未来之窗软件服务4 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther5 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间6 小时前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec6 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
Tiger_shl7 小时前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#
带娃的IT创业者8 小时前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet10 小时前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠11 小时前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu11 小时前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化