



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



我一般放在当前项目的Debug文件夹下:

1、添加选项卡Myself

2、拖放GDIPercent.dll到Myself中






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

当 一、3的代码改为下边时,显示如下的效果:
LinearGradientBrush brush2 = new LinearGradientBrush(rectangle2, Color.White, Color.Green, 150.0f, true);【绘制的圆环,随着进度条值增大,逐渐由白色变为绿色】