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

        }
相关推荐
timer_0172 小时前
微软将于 8 月 11 日关闭 Bing Search API 服务
microsoft
冰茶_3 小时前
掌握LINQ:查询语法与方法语法全解析
sql·学习·microsoft·微软·c#·linq
与火星的孩子对话3 小时前
Unity3D开发AI桌面精灵/宠物系列 【六】 人物模型 语音口型同步 LipSync 、梅尔频谱MFCC技术、支持中英文自定义编辑- 基于 C# 语言开发
人工智能·unity·c#·游戏引擎·宠物·lipsync
她说彩礼65万3 小时前
C# 中的锁
开发语言·c#
L_cl3 小时前
【NLP 75、如何通过API调用智谱大模型】
linux·服务器·windows
海尔辛3 小时前
学习黑客Active Directory 入门指南(一)
windows·学习·ad
※※冰馨※※3 小时前
彻底解决QT5 中文编译不过问题
c++·windows·qt
love530love4 小时前
【笔记】记一次PyCharm的问题反馈
ide·人工智能·windows·笔记·python·pycharm
范纹杉想快点毕业5 小时前
以项目的方式学QT开发C++(二)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·开发语言·c++·windows·vscode·qt·visual studio
兔子坨坨5 小时前
pycharm连接github(详细步骤)
windows·git·学习·pycharm·github