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();}}}}
}``# 使用封装,继承,改造上述代码
> 继承类```cnamespace 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();}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/588596.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

两数之和-考察哈希表的运用

题目 给定一个整数数组 n u m s nums nums和一个整数目标值 t a r g e t target target&#xff0c;请你在该数组中找出和为目标值 t a r g e t target target的那 两个整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同…

04-03 周三 使用印象笔记API批量更新笔记标题

04-03 周三 使用印象笔记API批量更新笔记标题 时间版本修改人描述2024年4月3日11:13:50V0.1宋全恒新建文档 简介 安利印象笔记 在阅读这篇博客之前&#xff0c;首先给大家案例一下印象笔记这个应用&#xff0c;楼主之前使用onenote来记录自己的生活的&#xff0c;也记录了许多…

Android 自定义View 测量控件宽高、自定义viewgroup测量

1、View生命周期以及View层级 1.1、View生命周期 View的主要生命周期如下所示&#xff0c; 包括创建、测量&#xff08;onMeasure&#xff09;、布局&#xff08;onLayout&#xff09;、绘制&#xff08;onDraw&#xff09;以及销毁等流程。 自定义View主要涉及到onMeasure、…

揭秘糖尿病患者稳定控制血糖的关键!

患者在就诊之前一直使用的二甲双胍和达格列净这两种降糖药物&#xff0c;这两种药对于控制血糖是有一定效果的。北京崇文门医院朱学敏主任的建议是继续服用&#xff0c;然后患者空腹血糖在7-8mmol/L左右&#xff0c;餐后血糖稍高&#xff0c;达到9-10mmol/L&#xff0c;但总体上…

怎么把图片改大小尺寸?简单的图片处理技巧分享

图片改变大小尺寸可以通过图像处理软件或在线工具来实现&#xff0c;图片改大小可以有多种作用&#xff1a;首先&#xff0c;改变图片的大小可以使其适应不同的显示设备&#xff0c;如手机、平板电脑或电脑屏幕。这样可以确保图片在不同设备上显示时不会失真或变形;其次&#x…

z-paging 的使用 uniapp+vue3脚手架

uniapp 中没有合适的分页插件, 所以找到了 z-paging的分页插件使用,这里记录一下用法 本人记录的开发项目是在小程序中使用的 安装 (因为本人使用的uniapp是 脚手架方式的)npm install z-paging --save配置 page.json easycom"easycom": {"custom": {&…

【OpenCV】 基础入门(一)初识 Mat 类 | 通过 Mat 类显示图像

&#x1f680; 个人简介&#xff1a;CSDN「博客新星」TOP 10 &#xff0c; C/C 领域新星创作者&#x1f49f; 作 者&#xff1a;锡兰_CC ❣️&#x1f4dd; 专 栏&#xff1a;【OpenCV • c】计算机视觉&#x1f308; 若有帮助&#xff0c;还请关注➕点赞➕收藏&#xff…

IDEA无法连接虚拟机中的Redis的解决方案,无法连接Jedis,无法ping通虚拟机的解决方案

首先&#xff0c;笔者先说明一下自身的情况&#xff0c;怎么连接都连不上&#xff0c;网上的教程全部都看了一遍&#xff0c;基本上没用得上的&#xff0c;这篇文章里面的解决方案包括了笔者能在网上找到了最全面的办法总结&#xff0c;最后终于是连上了 目录 一.连接Jedis出错…

ICV:《中美两国量子技术投资现状及建议概述》

2024年4月2日&#xff0c;ICV发布了《中美两国量子技术投资现状及建议概述》报告&#xff0c;在该报告中提到作为未来科技发展的重要前沿&#xff0c;量子技术领域已引起 30 多个国家和地区的关注&#xff0c;并带动了量子技术产业的发展。量子技术以其革命性的潜力&#xff0c…

【Python项目】AI动物识别工具

目录 背景 技术简介 系统简介 界面预览 背景 成像技术在全球科技发展中扮演了关键角色。在科学研究领域&#xff0c;拍摄所得的图像成为了一种不可或缺的研究工具。特别是在生态学与动物学研究中&#xff0c;鉴于地球的广阔地域和多样的气候条件&#xff0c;利用图像技术捕…

实用技巧:Flutter应用顺利通过审核上架iOS平台

引言 &#x1f680; Flutter作为一种跨平台的移动应用程序开发框架&#xff0c;为开发者提供了便利&#xff0c;使他们能够通过单一的代码库构建出高性能、高保真度的应用程序&#xff0c;同时支持Android和iOS两个平台。然而&#xff0c;完成Flutter应用程序的开发只是第一步…

Pro版v3.0,供应商管理一站式搞定!

现在很多企业品牌做电商&#xff0c;已经不仅仅局限于自己制造生产产品&#xff0c;有的已经发展成零库存模式&#xff0c;即选择多个供应商&#xff0c;由供应商提供发货售后。所以&#xff0c;很多企业都拥有多个供应商&#xff0c;销售的产品种类也是多种多样&#xff0c;但…