C#【必备技能篇】使用GDI绘制进度条的代码实现
创始人
2024-03-14 06:27:48

文章目录

  • 一、使用“用户控件”生成圆环进度条的dll
    • 1、使用VS2019新建项目(类库)
    • 2、添加用户控件
    • 3 、用户控件PercentStar.cs源码编写
  • 二、引用dll,在Winform中代码实现
    • 1、新建Windows窗体应用(.NET Framework)
    • 2、添加引用(上边生成的GDIPercent.dll)
    • 3、将dll添加到工具箱【两种方法】
      • 方法一:鼠标选中GDIPercent.dll,直接拖放到工具栏【可以先添加个选项卡Myself,方便管理】
      • 方法二:通过添加 .NET Framework组件 来添加
    • 4、源码
    • 5、运行效果
    • 6、补充【变换圆环进度条颜色】

一、使用“用户控件”生成圆环进度条的dll

1、使用VS2019新建项目(类库)

在这里插入图片描述在这里插入图片描述

2、添加用户控件

在这里插入图片描述
在这里插入图片描述

3 、用户控件PercentStar.cs源码编写

PercentStar.cs源码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace GDIPercent
{public partial class PercentStar : UserControl{public PercentStar(){InitializeComponent();}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 获取画布Graphics graphics = e.Graphics;//消除锯齿graphics.SmoothingMode = SmoothingMode.AntiAlias;//文字显示效果graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;//插补模式graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;//图片呈现质量graphics.CompositingQuality = CompositingQuality.HighQuality;// 绘制底圆SolidBrush brush1 = new SolidBrush(Color.FromArgb(93, 107, 153));Rectangle rectangle1 = new Rectangle(1, 1, this.Width - 2, this.Height - 2);graphics.FillEllipse(brush1, rectangle1);//绘制扇形Rectangle rectangle2 = new Rectangle(1, 1, this.Width - 2, this.Height - 2);LinearGradientBrush brush2 = new LinearGradientBrush(rectangle2, Color.Blue, Color.Red, 150.0f, true);【绘制的圆环,随着进度条值增大,逐渐由蓝变为红色】//LinearGradientBrush brush2 = new LinearGradientBrush(rectangle2, Color.White, Color.Green, 150.0f, true);【绘制的圆环,随着进度条值增大,逐渐由白色变为绿色】this.PercentVal = (ActureValue / MaxValue) * 100;graphics.FillPie(brush2, rectangle2, -90, (ActureValue / MaxValue) * 360f);//绘制上圆SolidBrush solidBrushElipse = new SolidBrush(this.BackColor);Rectangle rectangle3 = new Rectangle(15, 15, this.Width - 30, this.Height - 30);graphics.FillEllipse(solidBrushElipse, rectangle3);//绘制文字Font font = new Font("华为宋体", 14);PointF point = new PointF(((float)this.Width) / 2.0f - 27, ((float)this.Height) / 2.0f - 10);graphics.DrawString(this.PercentVal.ToString("0.0") + "%", font, Brushes.Coral, point);}//最大值private float maxValue = 100;public float MaxValue{get { return maxValue; }set{maxValue = value;this.Invalidate();}}//实际值private float actureValue = 60;public float ActureValue{get { return actureValue; }set{actureValue = value;this.Invalidate();}}//文字显示值private float PercentVal = 0;}
}

在这里插入图片描述
在这里插入图片描述

二、引用dll,在Winform中代码实现

1、新建Windows窗体应用(.NET Framework)

在这里插入图片描述

2、添加引用(上边生成的GDIPercent.dll)

我一般放在当前项目的Debug文件夹下:
在这里插入图片描述

3、将dll添加到工具箱【两种方法】

方法一:鼠标选中GDIPercent.dll,直接拖放到工具栏【可以先添加个选项卡Myself,方便管理】

1、添加选项卡Myself
在这里插入图片描述
2、拖放GDIPercent.dll到Myself中
在这里插入图片描述

方法二:通过添加 .NET Framework组件 来添加

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4、源码

在这里插入图片描述在这里插入图片描述

using System;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void timer1_Tick(object sender, EventArgs e){if (percentStar1.ActureValue == 100){percentStar1.ActureValue = 0;return;}percentStar1.ActureValue += 1;}}
}

5、运行效果

请添加图片描述

6、补充【变换圆环进度条颜色】

一、3的代码改为下边时,显示如下的效果:

LinearGradientBrush brush2 = new LinearGradientBrush(rectangle2, Color.White, Color.Green, 150.0f, true);【绘制的圆环,随着进度条值增大,逐渐由白色变为绿色】

在这里插入图片描述

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...