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; // 刻度的總數(shù)
// 自定義顏色
Color scaleColor = Color.Gray;
Color textColor = Color.Black;
Color pointerEndColor = Color.Red; // 指針結(jié)束點(diǎn)的顏色
// 繪制刻度盤
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));
// 繪制刻度圓點(diǎn)
int dotRadius = 4;
g.FillEllipse(new SolidBrush(scaleColor), x - dotRadius, y - dotRadius, dotRadius * 2, dotRadius * 2);
// 顯示刻度上的數(shù)字
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); // 根據(jù)值計(jì)算角度
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);
}
}