本文主要是介绍C# Winform .net6自绘的圆形进度条,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 circleusing (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 arcusing (Pen pen = new Pen(Color.LightGreen, borderWidth)) //lightgreen{pen.DashStyle = DashStyle.Solid; // 进度使用实线// Calculate sweep anglefloat sweepAngle = (360f * progress) / 100f;e.Graphics.DrawArc(pen, borderWidth / 2, borderWidth / 2, this.Width - borderWidth, this.Height - borderWidth, -90, sweepAngle);}// Draw progress textstring 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 positionPointF textPoint = new PointF((this.Width - textSize.Width) / 2, (this.Height - textSize.Height) / 2);e.Graphics.DrawString(progressText, font, brush, textPoint);}}}
}
这篇关于C# Winform .net6自绘的圆形进度条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!