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);
                }
            }
        }
}
相关推荐
死磕音视频8 小时前
让小模型做选择题:限定候选项并读取 logits 概率的入门实践(附 Python 代码)
开发语言·python·大模型·文本分类·transformers
顽疲10 小时前
Java vue 养老系统源码详解:月账单 care_month_bill 与 Quartz 扣费 Job 实践
java·开发语言·vue.js·vue养老·springboot养老·java养老
Wang's Blog10 小时前
Java 项目实战: 交友项目 - 安装开发工具与环境准备
java·开发语言·交友
曹牧11 小时前
C#:输入的字符串格式不正确
c#
程序喵大人12 小时前
【C++入门】编译链接模型 - 07 链接器怎样把符号对上号
开发语言·c++·编译器·编译链接·链接器
微三云 - 廖会灵 (私域系统开发)12 小时前
供应链系统实战:多渠道订单聚合与分布式库存一致性方案总结
开发语言·分布式·php
学逆向的13 小时前
C 数据溢出与精度丢失
c语言·开发语言·网络安全
霸道流氓气质13 小时前
AgentFlow 入门与实战:Task-Based 并发、EWMA 动态调度与四道门禁的 Java 生产级编排
java·开发语言
释厄62314 小时前
错误无误公理——错误正确本同源都迭代定律——逆转有价值
android·开发语言·kotlin
2601_9628857214 小时前
如何用 Python计算 CR能量指标(中间意愿指标)?
开发语言·python