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);
                }
            }
        }
}
相关推荐
wjs202410 分钟前
JavaScript 条件语句
开发语言
阿里加多31 分钟前
第 1 章:Go 并发编程概述
java·开发语言·数据库·spring·golang
2301_792674861 小时前
java学习day29(juc)
java·开发语言·学习
周末也要写八哥1 小时前
MATLAB R2025a超详细下载与安装教程(附安装包)
开发语言·matlab
雪人不是菜鸡1 小时前
反射调用方法
c#
blog_wanghao2 小时前
基于Qt的串口调试助手
开发语言·qt
果汁华3 小时前
Typer:基于类型提示的现代Python CLI框架
开发语言·网络·python
赵药师3 小时前
多进程-生产者消费者C++实现
java·开发语言·jvm
雾岛听蓝3 小时前
Linux线程基础
linux·开发语言·经验分享
zhangzeyuaaa3 小时前
Python 异常机制深度剖析
开发语言·python