public partial class Form1 : Form
{
int yourValue = 110;
Timer timer=new Timer();
public Form1()
{
InitializeComponent();
pictureBox1.Paint += PictureBox1_Paint;
timer.Interval = 1000;
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object? sender, EventArgs e)
{
yourValue=new Random().Next(1,200);
pictureBox1.Refresh();
}
private void PictureBox1_Paint(object? sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias; // 减少毛边
int centerX = pictureBox1.Width / 2;
int centerY = pictureBox1.Height / 2;
int radius = Math.Min(centerX, centerY);
int totalSteps = 20; // 刻度的总数
// 自定义颜色
Color scaleColor = Color.Gray;
Color textColor = Color.Black;
Color pointerEndColor = Color.Red; // 指针结束点的颜色
// 绘制刻度盘
for (int step = 0; step <= totalSteps; step++)
{
double angle = Math.PI * (1 - step / (double)totalSteps); // 此行修改
int x = centerX + (int)(0.85 * radius * Math.Cos(angle));
int y = centerY - (int)(0.85 * radius * Math.Sin(angle));
// 绘制刻度圆点
int dotRadius = 4;
g.FillEllipse(new SolidBrush(scaleColor), x - dotRadius, y - dotRadius, dotRadius * 2, dotRadius * 2);
// 显示刻度上的数字
int value = step * 10; // 0到200的刻度值
SizeF textSize = g.MeasureString(value.ToString(), Font);
int textX = centerX + (int)(0.75 * radius * Math.Cos(angle)) - (int)(textSize.Width / 2);
int textY = centerY - (int)(0.75 * radius * Math.Sin(angle)) - (int)(textSize.Height / 2);
g.DrawString(value.ToString(), Font, new SolidBrush(textColor), new PointF(textX, textY));
}
// 绘制指针
int pointerLength = radius - 20;
double pointerAngle = Math.PI * (yourValue / 200.0); // 根据值计算角度
int pointerX = centerX + (int)(pointerLength * Math.Cos(pointerAngle));
int pointerY = centerY - (int)(pointerLength * Math.Sin(pointerAngle));
// 绘制指针线
g.DrawLine(new Pen(pointerEndColor, 3), centerX, centerY, pointerX, pointerY);
}
}