public partial class Form3 : Form
{
const int s_pinlen = 250;//秒针长度
const int m_pinlen = 200; //分针长度
const int h_pinlen = 180; //时针长度
PointF center = new PointF(s_pinlen +3, s_pinlen +3);//中心点位置
SolidBrush sb = new SolidBrush(Color.Black);//时钟圆心的刷子
PointF AngleToPos(int angle, float percent)
{
PointF pos = new PointF();
double radian = angle * Math.PI / 180;
pos.Y = center.Y - s_pinlen * percent * (float)Math.Sin(radian);
pos.X = center.X + s_pinlen * percent * (float)Math.Cos(radian);
return pos;
}
void myClock(int h, int m, int s)
{
Pen pDisk = new Pen(Color.Orange, 3);//时钟背景的笔
Pen pScale = new Pen(Color.Coral);//刻度的笔
Graphics myGraphics = pictureBox1.CreateGraphics();
myGraphics.Clear(Color.White);
Pen myPen = new Pen(Color.Black,2);
Point CPoint = new Point(s_pinlen , s_pinlen);
Point SPoint = new Point((int)(CPoint.X + (Math.Sin(6 * s * Math.PI / 180)) * s_pinlen), (int)(CPoint.Y - (Math.Cos(6 * s * Math.PI / 180)) * s_pinlen));
Point MPoint = new Point((int)(CPoint.X + (Math.Sin(6 * m * Math.PI / 180)) * m_pinlen), (int)(CPoint.Y - (Math.Cos(6 * m * Math.PI / 180)) * m_pinlen));
Point HPoint = new Point((int)(CPoint.X + (Math.Sin(((30 * h) + (m / 2)) * Math.PI / 180)) * h_pinlen), (int)(CPoint.Y - (Math.Cos(((30 * h) + (m / 2)) * Math.PI / 180)) * h_pinlen));
myGraphics.FillEllipse(sb, center.X -8, center.Y - 7, 14, 14);
myGraphics.DrawLine(myPen, CPoint, SPoint);
myPen = new Pen(Color.Blue , 4);
myGraphics.DrawLine(myPen, CPoint, MPoint);
myPen = new Pen(Color.Green , 6);
myGraphics.DrawLine(myPen, CPoint, HPoint);
myGraphics.DrawEllipse(pDisk, 1, 1, s_pinlen * 2, s_pinlen * 2);//画刻度
for (int i = 0; i < 360; i += 6)
{
Pen tempPen = (i % 30 == 0) ? pDisk : pScale;
PointF qidian = AngleToPos(i, 0.87f);
PointF zhongdian= AngleToPos(i, 1.0f);
myGraphics.DrawLine(tempPen, qidian, zhongdian);
}
}
public Form3()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
myClock(h, m, s);//调用画时钟表的方法
}
}
C# 简易模拟时钟
最新推荐文章于 2022-03-18 20:49:35 发布