c#仿ppt案例

画曲线


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

    
    }
}


使用封装实现 画多条线,不连接


namespace ppt2024
{
    class HwFreeLine
    {
        //线的颜色
       public Color color = Color.Pink;
        //线的宽度
       public int width = 2;
        //存放线的集合(线由点构成,传入点的位置)
       public List<Point> lstPoints = new List<Point>();


        public void Draw(Graphics g)
        {
            //画笔
            Pen pen = new Pen(color, width);
            //两点确定一条直线
            if(lstPoints.Count>1)
            {
                //画家画线
                g.DrawLines(pen, lstPoints.ToArray());
            }
        }
    }
}

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

    }
}


画矩形

可以画多个矩形


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)
        {

        }
    }
}

画带颜色的矩形

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)
        {

        }


    }
}

使用封装

namespace ppt2024
{
    class HwReactangle
    {
        //存放矩形填充颜色
       public Color reactFill = Color.Pink;
        //矩形边框颜色
       public Color reactFrame = Color.Gray;
        //矩形边框宽度
       public int frameSize = 10;

        //起始点
        public int x;
        public int y;

        //矩形宽高
        public int w;
        public int h;

        
        //存放矩形数组
        public List<Rectangle> lstRect = new List<Rectangle>();


        public void Draw(Graphics g)
        {
            //画笔
            Pen pen = new Pen(reactFrame, frameSize);
            //纯色画刷
            SolidBrush solidBrush = new SolidBrush(reactFill);

            //画矩形
            g.DrawRectangle(pen, x, y, w, h);
            //绘制矩形填充颜色
            g.FillRectangle(solidBrush, x, y, w, h);

        }

    }
}



namespace ppt2024
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //用集合存放矩形的位置信息
        List<HwReactangle> lstRects = new List<HwReactangle>();
        HwReactangle rect;

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

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

            //随机函数
            Random r = new Random();
            rect.reactFill = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
            rect.frameSize = r.Next(1, 10);

            lstRects.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<lstRects.Count;i++)
            {
                lstRects[i].Draw(g);
            }
       
        }

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

                 rect.w = e.X - rect.x;
                 rect.h = e.Y - rect.y;

                lstRects[lstRects.Count - 1] = rect;


                //使得paint方法生效
                 this.Invalidate();
            }


        }

    }
}

画椭圆

仿造之前的矩形

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

        }

画三角形

封装类


namespace ppt2024
{
    class HwTriangle
    {

        //存放填充颜色
        public Color reactFill = Color.Pink;
        //三角形边框颜色
        public Color reactFrame = Color.Gray;
        //三角形边框宽度
        public int frameSize = 10;

        //起始点
        public int x;
        public int y;

        //三角形宽高
        public int w;
        public int h;


        //存放矩形数组
        //public List<HwTriangle> lstRect = new List<HwTriangle>();


        public void Draw(Graphics g)
        {
            //画笔
            Pen pen = new Pen(reactFrame, frameSize);
            //纯色画刷
            SolidBrush solidBrush = new SolidBrush(reactFill);

            //确定三角形三个顶点
            Point p1 = new Point(x + w / 2, y);
            Point p2 = new Point(x, y - h);
            Point p3 = new Point(x + w, y - h);

            Point[] pArr = new Point[3] { p1, p2, p3 };

            g.FillPolygon(solidBrush, pArr);
            g.DrawPolygon(pen, pArr);

           

        }

    }
}


仿ppt实现不同形状的图形选择

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace ppt2024
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //用枚举
        public enum GeoType { None, FreeLine, Rect, Tri };
        public GeoType type = GeoType.None;

        //用集合存放图形的位置信息
        List<HwFreeLine> lstFreeLine = new List<HwFreeLine>();
        List<HwReactangle> lstRect = new List<HwReactangle>();
        List<HwTriangle> lstTri = new List<HwTriangle>();


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


        // 点击不同按钮实现画不同图形效果
        private void button1_Click(object sender, EventArgs e)
        {
            type = GeoType.Tri;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            type = GeoType.Rect;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            type = GeoType.FreeLine;
        }

        //鼠标点击开始画
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;

            //添加涂鸦线
            if (type == GeoType.FreeLine)
            {
                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);
            }
            //添加矩形
            else if (type == GeoType.Rect)
            {
                HwReactangle rect = new HwReactangle();
                rect.x = e.Location.X;
                rect.y = e.Location.Y;
                //随机函数
                Random r = new Random();
                rect.reactFill = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
                rect.frameSize = r.Next(1, 10);

                lstRect.Add(rect);
            }
            //添加三角形
            else if (type == GeoType.Tri)
            {
                HwTriangle tri = new HwTriangle();
                tri.x = e.Location.X;
                tri.y = e.Location.Y;

                //随机函数
                Random r = new Random();
                tri.reactFill = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
                tri.frameSize = r.Next(1, 10);
                lstTri.Add(tri);
            }


        }

        //鼠标弹起不画
        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(e.Graphics);
            }
            //画矩形
            for (int i = 0; i < lstRect.Count; i++)
            {
                lstRect[i].Draw(e.Graphics);
            }
            //画三角形
            for (int i = 0; i < lstTri.Count; i++)
            {
                lstTri[i].Draw(e.Graphics);
            }

        }

        //鼠标移动记录信息
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawing)
            {
                //更新涂鸦线
                if (type == GeoType.FreeLine)
                {
                    lstFreeLine[lstFreeLine.Count - 1].lstPoints.Add(e.Location);
                    this.Invalidate();
                }

                //矩形
                if (type == GeoType.Rect)
                {
                    lstRect[lstRect.Count - 1].w = e.Location.X - lstRect[lstRect.Count - 1].x;
                    lstRect[lstRect.Count - 1].h = e.Location.Y - lstRect[lstRect.Count - 1].y;
                    this.Invalidate();
                }
                //三角形
                if (type == GeoType.Tri)
                {
                    lstTri[lstTri.Count - 1].w = e.Location.X - lstTri[lstTri.Count - 1].x;
                    lstTri[lstTri.Count - 1].h = e.Location.Y - lstTri[lstTri.Count - 1].y;
                    this.Invalidate();
                }


            }

        }
    }
}

``


# 使用封装,继承,改造上述代码
> 继承类

```c

namespace ppt2024
{
    class HwGeometry
    {
        //图形填充颜色
        public Color fillColor = Color.Blue;
        //图形边框颜色
        public Color borderColor = Color.Black;
        //图形边框宽度
        public int borderWidth = 6;
        //图形边框样式
        public DashStyle ds = DashStyle.Dash;

        //公共的抽象方法

        public virtual void Draw(Graphics g)
        {
        }
    }
}

子类


namespace ppt2024
{
    class HwReactangle:HwGeometry
    {
        //起始点
        public int x;
        public int y;

        //矩形宽高
        public int w;
        public int h;

        
        //存放矩形数组
        public List<Rectangle> lstRect = new List<Rectangle>();


        public override void Draw(Graphics g)
        {
            //画笔
            Pen pen = new Pen(borderColor, borderWidth);
            //纯色画刷
            SolidBrush solidBrush = new SolidBrush(fillColor);

            //样式
            pen.DashStyle = ds;
            //画矩形
            g.DrawRectangle(pen, x, y, w, h);
            //绘制矩形填充颜色
            g.FillRectangle(solidBrush, x, y, w, h);

        }

    }
}


三角形,涂鸦线参照之前代码

主类

namespace ppt2024
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //用枚举
        public enum GeoType { None, FreeLine, Rect, Tri };
        public GeoType type = GeoType.None;

        //用集合存放图形的位置信息
        List<HwGeometry> lstGeo = new List<HwGeometry>();


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


        // 点击不同按钮实现画不同图形效果
        private void button1_Click(object sender, EventArgs e)
        {
            type = GeoType.Tri;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            type = GeoType.Rect;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            type = GeoType.FreeLine;
        }

        //鼠标点击开始画
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;

            //添加涂鸦线
            if (type == GeoType.FreeLine)
            {
                HwFreeLine freeLine = new HwFreeLine();
                Random r = new Random();
                freeLine.borderColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
                freeLine.borderWidth = r.Next(1, 10);
                lstGeo.Add(freeLine);
            }
            //添加矩形
            else if (type == GeoType.Rect)
            {
                HwReactangle rect = new HwReactangle();
                rect.x = e.Location.X;
                rect.y = e.Location.Y;
                //随机函数
                Random r = new Random();
                rect.borderColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
                rect.borderWidth = r.Next(1, 10);
                rect.fillColor= Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)); 

                lstGeo.Add(rect);
            }
            //添加三角形
            else if (type == GeoType.Tri)
            {
                HwTriangle tri = new HwTriangle();
                tri.x = e.Location.X;
                tri.y = e.Location.Y;

                //随机函数
                Random r = new Random();
                tri.borderColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
                tri.borderWidth = r.Next(1, 10);
                tri.fillColor= Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));

                lstGeo.Add(tri);
            }


        }

        //鼠标弹起不画
        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 < lstGeo.Count; i++)
            {
                lstGeo[i].Draw(g);
            }
        
        }

        //鼠标移动记录信息
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawing)
            {
                //更新涂鸦线
                if (type == GeoType.FreeLine)
                {
                    //更新
                    ((HwFreeLine)lstGeo[lstGeo.Count - 1]).lstPoints.Add(e.Location);
                }

                //矩形
                if (type == GeoType.Rect)
                {
              
                    ((HwReactangle)lstGeo[lstGeo.Count - 1]).w = e.Location.X - ((HwReactangle)lstGeo[lstGeo.Count - 1]).x;
                    ((HwReactangle)lstGeo[lstGeo.Count - 1]).h = e.Location.Y - ((HwReactangle)lstGeo[lstGeo.Count - 1]).y;

                }
                //三角形
                if (type == GeoType.Tri)
                {
                    ((HwTriangle)lstGeo[lstGeo.Count - 1]).w = e.Location.X - ((HwTriangle)lstGeo[lstGeo.Count - 1]).x;
                    ((HwTriangle)lstGeo[lstGeo.Count - 1]).h = e.Location.Y - ((HwTriangle)lstGeo[lstGeo.Count - 1]).y;
                }

                //开启重绘
                this.Invalidate();



            }

        }
    }
}



namespace ppt2024
{
    class HwElliptical:HwGeometry
    {
        //起始点
        public int x;
        public int y;

        //矩形宽高
        public int w;
        public int h;


        //存放矩形数组
        public List<Rectangle> lstRect = new List<Rectangle>();


        public override void Draw(Graphics g)
        {
            //画笔
            Pen pen = new Pen(borderColor, borderWidth);
            //纯色画刷
            SolidBrush solidBrush = new SolidBrush(fillColor);

            //样式
            pen.DashStyle = ds;
            //画矩形
            g.DrawEllipse(pen, x, y, w, h);
            //绘制矩形填充颜色
            g.FillEllipse(solidBrush, x, y, w, h);

        }

    }
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值