C# Winform .net6自绘的圆形进度条

cs 复制代码
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Net6_GeneralUiWinFrm
{
        public class CircularProgressBar : Control
        {
            private int progress = 0;
            private int borderWidth = 20; // 增加的边框宽度

            public int Progress
            {
                get { return progress; }
                set
                {
                    progress = Math.Max(0, Math.Min(100, value)); // 确保进度值在0到100之间
                    Invalidate(); // Causes the control to be redrawn
                }
            }

            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

                // Draw background circle
                using (Pen pen = new Pen(Color.LightGray, borderWidth))
                {
                    pen.DashStyle = DashStyle.Dot; // 设置点状线条
                    e.Graphics.DrawEllipse(pen, borderWidth / 2, borderWidth / 2, this.Width - borderWidth, this.Height - borderWidth);
                }

                // Draw progress arc
                using (Pen pen = new Pen(Color.LightGreen, borderWidth)) //lightgreen
                {
                    pen.DashStyle = DashStyle.Solid; // 进度使用实线
                                                     // Calculate sweep angle
                    float sweepAngle = (360f * progress) / 100f;
                    e.Graphics.DrawArc(pen, borderWidth / 2, borderWidth / 2, this.Width - borderWidth, this.Height - borderWidth, -90, sweepAngle);
                }

                // Draw progress text
                string progressText = $"{progress}%";
                using (Font font = new Font("Arial", 12))
                using (Brush brush = new SolidBrush(Color.Black))
                {
                    SizeF textSize = e.Graphics.MeasureString(progressText, font);
                    // Calculate text position
                    PointF textPoint = new PointF((this.Width - textSize.Width) / 2, (this.Height - textSize.Height) / 2);
                    e.Graphics.DrawString(progressText, font, brush, textPoint);
                }
            }
        }
}
相关推荐
一颗花生米。19 分钟前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼20 分钟前
Java基础-单例模式的实现
java·开发语言·单例模式
学习使我快乐0123 分钟前
JS进阶 3——深入面向对象、原型
开发语言·前端·javascript
通信仿真实验室1 小时前
(10)MATLAB莱斯(Rician)衰落信道仿真1
开发语言·matlab
勿语&1 小时前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
吾爱星辰5 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer5 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
IT良5 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
yufei-coder5 小时前
掌握 C# 中的 LINQ(语言集成查询)
windows·vscode·c#·visual studio
Kalika0-06 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法