MyCircleGaugeControl.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. public class MyCircleGaugeControl : Control
  7. {
  8. // 构造函数
  9. public MyCircleGaugeControl()
  10. {
  11. // 启用双缓冲,防止闪烁
  12. this.SetStyle(
  13. ControlStyles.AllPaintingInWmPaint |
  14. ControlStyles.UserPaint |
  15. ControlStyles.DoubleBuffer |
  16. ControlStyles.ResizeRedraw |
  17. ControlStyles.SupportsTransparentBackColor,
  18. true);
  19. // 设置背景为透明
  20. this.BackColor = Color.Transparent;
  21. Points = new List<Point>();
  22. }
  23. [Category("Appearance")]
  24. [Description("线的粗细")]
  25. public float LineWidth
  26. {
  27. get { return lineWidth; }
  28. set { if (value< 1) value = 1; lineWidth = value; this.Invalidate(); } // Invalidate 触发重绘
  29. }
  30. private float lineWidth = 1;
  31. [Category("Appearance")]
  32. [Description("圆和点的颜色")]
  33. public Color LineColor
  34. {
  35. get { return _lineColor; }
  36. set { _lineColor = value; this.Invalidate(); } // Invalidate 触发重绘
  37. }
  38. private Color _lineColor = Color.Black;
  39. [Category("Appearance")]
  40. [Description("点的数量")]
  41. public int PointsNum
  42. {
  43. get { return pointsNum; }
  44. set { pointsNum = value; this.Invalidate(); } // Invalidate 触发重绘
  45. }
  46. private int pointsNum = 40;
  47. [Category("Appearance")]
  48. [Description("起始角度")]
  49. public int StartDegree
  50. {
  51. get { return startDegree; }
  52. set { startDegree = value; this.Invalidate(); } // Invalidate 触发重绘
  53. }
  54. private int startDegree = 0;
  55. [Category("Appearance")]
  56. [Description("显示数字索引")]
  57. [DefaultValue(true)]
  58. public bool ShowIndex
  59. {
  60. get { return showIndex; }
  61. set { showIndex = value; this.Invalidate(); } // Invalidate 触发重绘
  62. }
  63. private bool showIndex = true;
  64. public List<Point> Points;
  65. public event EventHandler PaintCompleted;
  66. protected override void OnPaint(PaintEventArgs e)
  67. {
  68. base.OnPaint(e);
  69. Graphics g = e.Graphics;
  70. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // 抗锯齿
  71. int diameter = Math.Min(this.Width, this.Height)-10;
  72. if(diameter < 10) diameter = 10;
  73. int radius = diameter / 2;
  74. Point center = new Point(this.Width / 2, this.Height / 2);
  75. // 绘制主圆
  76. using (Pen pen = new Pen(_lineColor, lineWidth))
  77. {
  78. g.DrawEllipse(pen, center.X - radius, center.Y - radius, diameter, diameter);
  79. }
  80. if (pointsNum > 1)
  81. {
  82. Points.Clear();
  83. using (Pen pen = new Pen(_lineColor, lineWidth * 2))
  84. {
  85. for (int i = 0; i < pointsNum; i++)
  86. {
  87. double angle = -(i * 360.0 / pointsNum+startDegree) * Math.PI / 180.0; // 转弧度
  88. float x = (float)(center.X + (radius) * Math.Cos(angle));
  89. float y = (float)(center.Y + (radius) * Math.Sin(angle));
  90. if(showIndex)
  91. g.DrawString((i + 1).ToString("d2"),new Font(Font.Name,Font.Size), Brushes.Black, (float)(x - Font.Size/1.2), (float)(y - Font.Size/1.5));
  92. Points.Add(new Point((int)x, (int)y));
  93. }
  94. }
  95. //float pointRadius = 2; // 点的半径(可调)
  96. //using (Brush pbrush = Brushes.Black)
  97. //{
  98. // for (int i = 0; i < pointsNum; i++)
  99. // {
  100. // double angle = i * 360.0 / pointsNum * Math.PI / 180.0; // 转弧度
  101. // float x = (float)(center.X + (radius) * Math.Cos(angle));
  102. // float y = (float)(center.Y + (radius) * Math.Sin(angle));
  103. // //绘制小圆点
  104. // g.FillEllipse(pbrush,
  105. // x - pointRadius,
  106. // y - pointRadius,
  107. // pointRadius * 2,
  108. // pointRadius * 2);
  109. // }
  110. //}
  111. }
  112. PaintCompleted?.Invoke(this, EventArgs.Empty);
  113. }
  114. // 可选:当背景色改变时,确保仍是透明
  115. protected override void OnBackColorChanged(EventArgs e)
  116. {
  117. base.OnBackColorChanged(e);
  118. if (this.BackColor != Color.Transparent)
  119. this.BackColor = Color.Transparent;
  120. }
  121. }