| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- public class MyCircleGaugeControl : Control
- {
- // 构造函数
- public MyCircleGaugeControl()
- {
- // 启用双缓冲,防止闪烁
- this.SetStyle(
- ControlStyles.AllPaintingInWmPaint |
- ControlStyles.UserPaint |
- ControlStyles.DoubleBuffer |
- ControlStyles.ResizeRedraw |
- ControlStyles.SupportsTransparentBackColor,
- true);
- // 设置背景为透明
- this.BackColor = Color.Transparent;
- Points = new List<Point>();
- }
- [Category("Appearance")]
- [Description("线的粗细")]
- public float LineWidth
- {
- get { return lineWidth; }
- set { if (value< 1) value = 1; lineWidth = value; this.Invalidate(); } // Invalidate 触发重绘
- }
- private float lineWidth = 1;
- [Category("Appearance")]
- [Description("圆和点的颜色")]
- public Color LineColor
- {
- get { return _lineColor; }
- set { _lineColor = value; this.Invalidate(); } // Invalidate 触发重绘
- }
- private Color _lineColor = Color.Black;
- [Category("Appearance")]
- [Description("点的数量")]
- public int PointsNum
- {
- get { return pointsNum; }
- set { pointsNum = value; this.Invalidate(); } // Invalidate 触发重绘
- }
- private int pointsNum = 40;
- [Category("Appearance")]
- [Description("起始角度")]
- public int StartDegree
- {
- get { return startDegree; }
- set { startDegree = value; this.Invalidate(); } // Invalidate 触发重绘
- }
- private int startDegree = 0;
- [Category("Appearance")]
- [Description("显示数字索引")]
- [DefaultValue(true)]
- public bool ShowIndex
- {
- get { return showIndex; }
- set { showIndex = value; this.Invalidate(); } // Invalidate 触发重绘
- }
- private bool showIndex = true;
- public List<Point> Points;
- public event EventHandler PaintCompleted;
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- Graphics g = e.Graphics;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // 抗锯齿
- int diameter = Math.Min(this.Width, this.Height)-10;
- if(diameter < 10) diameter = 10;
- int radius = diameter / 2;
- Point center = new Point(this.Width / 2, this.Height / 2);
- // 绘制主圆
- using (Pen pen = new Pen(_lineColor, lineWidth))
- {
- g.DrawEllipse(pen, center.X - radius, center.Y - radius, diameter, diameter);
- }
- if (pointsNum > 1)
- {
- Points.Clear();
- using (Pen pen = new Pen(_lineColor, lineWidth * 2))
- {
- for (int i = 0; i < pointsNum; i++)
- {
- double angle = -(i * 360.0 / pointsNum+startDegree) * Math.PI / 180.0; // 转弧度
- float x = (float)(center.X + (radius) * Math.Cos(angle));
- float y = (float)(center.Y + (radius) * Math.Sin(angle));
- if(showIndex)
- 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));
- Points.Add(new Point((int)x, (int)y));
- }
- }
- //float pointRadius = 2; // 点的半径(可调)
- //using (Brush pbrush = Brushes.Black)
- //{
- // for (int i = 0; i < pointsNum; i++)
- // {
- // double angle = i * 360.0 / pointsNum * Math.PI / 180.0; // 转弧度
- // float x = (float)(center.X + (radius) * Math.Cos(angle));
- // float y = (float)(center.Y + (radius) * Math.Sin(angle));
- // //绘制小圆点
- // g.FillEllipse(pbrush,
- // x - pointRadius,
- // y - pointRadius,
- // pointRadius * 2,
- // pointRadius * 2);
- // }
- //}
- }
- PaintCompleted?.Invoke(this, EventArgs.Empty);
- }
- // 可选:当背景色改变时,确保仍是透明
- protected override void OnBackColorChanged(EventArgs e)
- {
- base.OnBackColorChanged(e);
- if (this.BackColor != Color.Transparent)
- this.BackColor = Color.Transparent;
- }
- }
|