C#自定义控件—流动管道

news/2024/9/21 12:32:30/文章来源:https://www.cnblogs.com/guoenshuo/p/18391637

C#用户控件之流动管道

如何绘制一个动态的流动管道(FlowPipe)?

分两步绘制

  1. 定义属性;
  2. 画布重绘;

主要技能:

  • 管道的绘制(渐变色矩形)
  /// <summary>/// 画渐变色矩形的方法/// </summary>/// <param name="g">画布</param>/// <param name="brush">画刷</param>/// <param name="pen">笔</param>/// <param name="rectangle">矩形</param>private void PaintRectangle(Graphics g, Brush brush, Pen pen, Rectangle rectangle){//填充矩形g.FillRectangle(brush, rectangle);switch (this.pipeStyle){case PipeStyle.Horizontal:g.DrawLine(pen, rectangle.X, rectangle.Y, rectangle.X + rectangle.Width, rectangle.Y);g.DrawLine(pen, rectangle.X, rectangle.Y + rectangle.Height - 1, rectangle.X + rectangle.Width, rectangle.Height);break;case PipeStyle.Vertical:g.DrawLine(pen, rectangle.X, rectangle.Y, rectangle.X, rectangle.Y + rectangle.Height);g.DrawLine(pen, rectangle.X + rectangle.Width - 1, rectangle.Y, rectangle.X + rectangle.Width - 1, rectangle.Height);break;default:break;}}
  • 管道的绘制(渐变色半圆)
/// <summary>
/// 画渐变色半圆的方法
/// </summary>
/// <param name="g">画布</param>
/// <param name="colorBlend"></param>
/// <param name="p"></param>
/// <param name="rect"></param>
/// <param name="startAngle"></param>
/// <param name="sweepAngle"></param>
private void PaintEllipse(Graphics g, ColorBlend colorBlend, Pen p, Rectangle rect, float startAngle, float sweepAngle)
{//第一步:创建GPI路径GraphicsPath path = new GraphicsPath();path.AddEllipse(rect);//第二步:渐变色填充PathGradientBrush brush = new PathGradientBrush(path);brush.CenterPoint = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);brush.InterpolationColors = colorBlend;//第三步:绘制管道g.FillPie(brush, rect, startAngle, sweepAngle);//第四步:绘制边线g.DrawArc(p, rect, startAngle, sweepAngle);
}
  • 流动条的绘制(用笔的虚线)
//画虚线,关键用笔和路径来画
Pen pen = new Pen(this.flowColor, this.flowWidth);
pen.DashStyle = DashStyle.Custom;
pen.DashPattern = new float[]
{flowLength,flowLengthGap
};
pen.DashOffset = this.startOffset;
g.DrawPath(pen, path);//流动条路径GraphicsPath path = new GraphicsPath();//虚线路径—左边、中间、右边switch (this.pipeTurnLeft){case PipeTurn.Up:path.AddArc(new Rectangle(this.Height / 2, this.Height / 2 * (-1) -1, this.Height, this.Height), 181.0f, -91.0f);break;case PipeTurn.Down:path.AddArc(new Rectangle(this.Height / 2, this.Height / 2, this.Height, this.Height), 180.0f, 90.0f);break;default:path.AddLine(-1, this.Height / 2, this.Height+1, this.Height / 2);break;}
  • 关键理解:绘制的椭圆、线(Rectangle)<x,y【圆切矩形相对于控件原点<左上角>的坐标】,宽,高,开始角度,扫描角度>理解了就好画了
    path.AddArc(new Rectangle(this.Height / 2, this.Height / 2, this.Height, this.Height), 180.0f, 90.0f);
    path.AddLine(-1, this.Height / 2, this.Height+1, this.Height / 2);

  • 可以流动的关键要素

     //流动条流动速度(刷新速度)this.myTimer = new Timer();myTimer.Interval = 50;this.myTimer.Tick += MyTimer_Tick; ;}#region 定时循环private void MyTimer_Tick(object sender, EventArgs e){this.startOffset = this.startOffset - this.moveSpeed;if (this.startOffset > this.flowLength + this.flowLengthGap || this.startOffset < (this.flowLength + this.flowLengthGap) * (-1)){ this.startOffset = 0; }this.Invalidate();}#endregion

1.定义属性

  • 管道的(两端转向、样式、边沿颜色、中间颜色、激活)
  • 流动条的(速度、长度、宽度、间隙、颜色)
//属性示例:按照示例添加以上各种属性
private float moveSpeed = 0.3f;
[Browsable(true)]
[Category("布局_G")]
[Description("流动条速度,负数为反向")]  //属性说明
public float MoveSpeed
{get { return moveSpeed; }set{this.moveSpeed = value;this.Invalidate();  //重绘}
}

2.画布重绘

【管道分为左、中、右三部分。先画管道(矩形):左、中、右;再画流动条(虚线):左、中、右】
//矩形画刷
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), pipeColorEdge, pipeColorEdge);
linearGradientBrush.InterpolationColors = colorBlend;//绘制左部分
switch (this.pipeTurnLeft)
{case PipeTurn.Up:this.PaintEllipse(g, colorBlend, p, new Rectangle(0, this.Height * (-1)-1, this.Height * 2, this.Height * 2), 90.0f, 90.0f);break;case PipeTurn.Down:this.PaintEllipse(g, colorBlend, p, new Rectangle(0, 0, this.Height * 2, this.Height * 2), 180.0f, 90.0f);break;default:this.PaintRectangle(g, linearGradientBrush, p, new Rectangle(-1, 0, this.Height+1, this.Height));break;
}//绘制右部分
switch (this.pipeTurnRight)
{case PipeTurn.Up:this.PaintEllipse(g, colorBlend, p, new Rectangle(this.Width - this.Height * 2, this.Height * (-1)-1, this.Height * 2, this.Height * 2), 0.0f, 90.0f);break;case PipeTurn.Down:this.PaintEllipse(g, colorBlend, p, new Rectangle(this.Width - this.Height * 2, 0, this.Height * 2, this.Height * 2), 270.0f, 90.0f);break;default:this.PaintRectangle(g, linearGradientBrush, p, new Rectangle(this.Width - this.Height, 0, this.Height, this.Height));break;
}//绘制中间
if (this.Width > this.Height * 2)
{this.PaintRectangle(g, linearGradientBrush, p, new Rectangle(this.Height - 1, 0, this.Width - this.Height * 2 + 2, this.Height));
}
//流动条路径
GraphicsPath path = new GraphicsPath();//虚线路径—左边
switch (this.pipeTurnLeft)
{case PipeTurn.Up:path.AddArc(new Rectangle(this.Height / 2, this.Height / 2 * (-1) -1, this.Height, this.Height), 181.0f, -91.0f);break;case PipeTurn.Down:path.AddArc(new Rectangle(this.Height / 2, this.Height / 2, this.Height, this.Height), 180.0f, 90.0f);break;default:path.AddLine(-1, this.Height / 2, this.Height+1, this.Height / 2);break;
}//虚线路径—中间
if (this.Width > this.Height * 2)
{path.AddLine(this.Height, this.Height / 2, this.Width - this.Height -1, this.Height / 2);
}//虚线路径—右边
switch (this.pipeTurnRight)
{case PipeTurn.Up:path.AddArc(new Rectangle(this.Width - 1 - this.Height * 3 / 2, -this.Height / 2-1 , this.Height, this.Height), 88f, -91.0f);break;case PipeTurn.Down:path.AddArc(new Rectangle(this.Width - 1 - this.Height * 3 / 2, this.Height / 2, this.Height, this.Height), 270.0f, 90.0f);break;default:path.AddLine(this.Width - this.Height, this.Height / 2, this.Width , this.Height / 2);break;
}//画虚线,关键用笔和路径来
Pen pen = new Pen(this.flowColor, this.flowWidth);
pen.DashStyle = DashStyle.Custom;
pen.DashPattern = new float[]
{flowLength,flowLengthGap
};
pen.DashOffset = this.startOffset;
g.DrawPath(pen, path);

格式都是一样的,掌握关键代码,肝就对了。

End

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

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

相关文章

swing

数学教材推荐: 中学PDF课本介绍和下载:https://www.zhihu.com/question/517213170/answer/3430923272 swing swing基础 1、容器与控件 1.1)、类介绍JFrame 表示一个窗口JPanel ,表示一个容器,也称为面板JButton,表示一个按钮控件JLabel ,标签控件,用于显示文本1.2)、 使…

2分钟搞懂如何计算uart速率

一、前言 1960年代,DEC(Digital Equipment Corp)公司的Gordon Bell采用大约50个分离元件设计了一个电路板,发明了UART。时至今日,已经60多年,虽然在个人消费类电子产品中,UART已近乎绝迹;但在工业、科研、国防、航空/航天等领域,UART却无处不在。 很多从事多年嵌入式开…

JVM/垃圾回收

Java的垃圾回收模型 一、介绍分为栈、堆、本地方法栈、程序计数器、方法区栈区:主要用来存储局部变量和对象地址栈区不仅存储局部变量和对象地址,还存储方法调用的上下文信息。堆区:分为很多个区域,可以存储对象的具体数据等Java 虚拟机中内存最大的一块,是被所有线程共享的…

NetSarang Xshell 8.0 beta

一、概述NetSarang Xshell 8.0 beta发布啦! 二、新功能 2.1 身份验证配置文件 2.2 触发器2.3 快速命令 2.4 RDP支持 2.5 快速启动 2.6 自定义会话图标 ◀.zstitle { width: 280px; text-align: center; font-size: 26px } .zsimgweixin { width: 280px } .zsimgali { width…

【Spring Boot配置数据源问题】Spring Boot配置数据源时遇到错误:jdbcUrl is required with driverClassName​

报错如图,需要修改数据源连接配置:修改前配置如图:将spring.datasource.url修改为spring.datasource.jdbc-url即可:寻找了网上其他解释: spring.datasource.url 数据库的 JDBC URL。 spring.datasource.jdbc-url 用来创建连接的 JDBC URL。

mssql windows 账户登录不了 要登录sa创建出来 才可以本地登录

SQL SERVER 登陆错误:18456SQL Windows身份登录失败,错误码:18456 先用sa进入 EXEC xp_instance_regread NHKEY_LOCAL_MACHINE, NSOFTWARE\Microsoft\Microsoft SQL Server\MSSQLServer, NLoginMode -- 启用TCP/IP协议EXEC xp_instance_regwrite NHKEY_LOCAL_MACHINE, NSOFTWA…

2024软件工程(第一次作业)

这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/SE2024这个作业要求在哪里 https://edu.cnblogs.com/campus/fzu/SE2024/homework/13243这个作业的目标 适应后续使用博客园完成任务,初步体会AIGC的方便学号 1022014251.个人logo文生图任务设计理念:天上的白云体现…

三次剩余特征

三次剩余特征及其性质今天推点史: Laur - Symphony Op.1 -CHAOS ZAQUVA - Speculation DJ Noriken & DJ Genki - Dream Away feat. Yukacco (Hylen Remix) 史歌后边还有史笑话: 祭丁过,两广文①争一猪大脏,各执其脏之一头。一广文稍强,尽掣得其脏,争者只两手撸得脏中…

linux进程间通信——信号量(通俗易懂,看这一篇就够了)

信号量 概念 特点信号量实际是一个计数器。信号量用于实现进程间的互斥与同步,而不是用于存储 进程间通信 数据。很多进程会访问同一资源,或者向共享内存写入一些东西,为防止争夺资源混乱。可以给一些进程上锁,让其排队等待工作原理P(sv):如果sv的值大于零,就给它减1;如…

什么是非空集合A上的等价关系?

定义:解释 既然是等价关系,那么 \(A\) 中的元素 \(a\) 一定和自己等价 如果 \(a\) 和 \(b\) 等价,那么 \(b\) 和 \(a\) 也一定等价 如果 \(a,b\) 等价, \(b,c\) 等价,那么 \(a,c\) 一定等价

NetSarang Xshell(SSH客户端软件) v7.0.0169 中文绿色版

概述 NetSarang Xshell破解版是一款免费SSH客户端软件的Linux远程监控工具.Xshell中文版,轻松管理远程主机服务器,会话管理器,支持多选项卡管理主机.Xftp 7最新版以及Xshell 7最新版支持远程协议Telnet,Rlogin,SSH/SSH PKCS#11,SFTP,Serial,具有Unicode编码支持,动态端口转发,…

中望CAD 专业 v2024 解锁版下载与安装教程 (CAD三维制图)

安装步骤 ZWCAD2024-SP1.3-x64-Chs-Setup+Crack:ZWCAD2024-SP1.3-x64-Chs-Setup+Crack.zip 1、下载解压后点击如下进行安装2、选择安装目录,最好不要安装在系统盘(C盘)3、安装中...耐心等待2-3分钟4、安装完先不要运行它,点击完成即可5、回到我们解压的文件夹里面,访问cr…