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

        }
相关推荐
★YUI★14 分钟前
学习游戏制作记录(玩家掉落系统,删除物品功能和独特物品)8.17
java·学习·游戏·unity·c#
谷宇.1 小时前
【Unity3D实例-功能-拔枪】角色拔枪(二)分割上身和下身
游戏·unity·c#·游戏程序·unity3d·游戏开发·游戏编程
LZQqqqqo1 小时前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
季春二九1 小时前
Windows 11 首次开机引导(OOBE 阶段)跳过登录微软账户,创建本地账户
windows·microsoft
芥子沫2 小时前
Jenkins常见问题及解决方法
windows·https·jenkins
Dm_dotnet4 小时前
Stylet启动机制详解:从Bootstrap到View显示
c#
三千道应用题5 小时前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
唐青枫10 小时前
别滥用 Task.Run:C# 异步并发实操指南
c#·.net
王者鳜錸11 小时前
PYTHON让繁琐的工作自动化-PYTHON基础
python·microsoft·自动化
我好喜欢你~17 小时前
C#---StopWatch类
开发语言·c#