当前位置:   article > 正文

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

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

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. namespace Net6_GeneralUiWinFrm
  6. {
  7. public class CircularProgressBar : Control
  8. {
  9. private int progress = 0;
  10. private int borderWidth = 20; // 增加的边框宽度
  11. public int Progress
  12. {
  13. get { return progress; }
  14. set
  15. {
  16. progress = Math.Max(0, Math.Min(100, value)); // 确保进度值在0到100之间
  17. Invalidate(); // Causes the control to be redrawn
  18. }
  19. }
  20. protected override void OnPaint(PaintEventArgs e)
  21. {
  22. base.OnPaint(e);
  23. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  24. // Draw background circle
  25. using (Pen pen = new Pen(Color.LightGray, borderWidth))
  26. {
  27. pen.DashStyle = DashStyle.Dot; // 设置点状线条
  28. e.Graphics.DrawEllipse(pen, borderWidth / 2, borderWidth / 2, this.Width - borderWidth, this.Height - borderWidth);
  29. }
  30. // Draw progress arc
  31. using (Pen pen = new Pen(Color.LightGreen, borderWidth)) //lightgreen
  32. {
  33. pen.DashStyle = DashStyle.Solid; // 进度使用实线
  34. // Calculate sweep angle
  35. float sweepAngle = (360f * progress) / 100f;
  36. e.Graphics.DrawArc(pen, borderWidth / 2, borderWidth / 2, this.Width - borderWidth, this.Height - borderWidth, -90, sweepAngle);
  37. }
  38. // Draw progress text
  39. string progressText = $"{progress}%";
  40. using (Font font = new Font("Arial", 12))
  41. using (Brush brush = new SolidBrush(Color.Black))
  42. {
  43. SizeF textSize = e.Graphics.MeasureString(progressText, font);
  44. // Calculate text position
  45. PointF textPoint = new PointF((this.Width - textSize.Width) / 2, (this.Height - textSize.Height) / 2);
  46. e.Graphics.DrawString(progressText, font, brush, textPoint);
  47. }
  48. }
  49. }
  50. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/100766
推荐阅读
相关标签
  

闽ICP备14008679号