using System;
using System.Drawing;
using System.Windows.Forms;
public class ClockForm : Form
{
private readonly Timer timer = new Timer { Interval = 1000 };
private const int ClockSize = 200;
private const int CenterX = ClockSize / 2;
private const int CenterY = ClockSize / 2;
public ClockForm()
{
ClientSize = new Size(ClockSize, ClockSize);
DoubleBuffered = true;
timer.Tick += (_, __) => Invalidate();
timer.Start();
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
DrawClockFace(g);
DrawClockHands(g);
}
private void DrawClockFace(Graphics g)
{
g.FillEllipse(Brushes.White, 0, 0, ClockSize, ClockSize);
g.DrawEllipse(Pens.Black, 0, 0, ClockSize, ClockSize);
for (int i = 1; i <= 12; i++)
{
var angle = i * 30 * Math.PI / 180;
var x = (int)(CenterX + 80 * Math.Sin(angle));
var y = (int)(CenterY - 80 * Math.Cos(angle));
g.DrawString(i.ToString(), Font, Brushes.Black, x - 7, y - 7);
}
}
private void DrawClockHands(Graphics g)
{
var now = DateTime.Now;
DrawHand(g, now.Hour % 12 / 6f * (float)Math.PI, 60, Pens.Black, 6);
DrawHand(g, now.Minute / 30f * (float)Math.PI, 80, Pens.Blue, 4);
DrawHand(g, now.Second / 30f * (float)Math.PI, 90, Pens.Red, 2);
}
private void DrawHand(Graphics g, float angle, int length, Pen pen, int width)
{
g.DrawLine(new Pen(pen.Color, width),
CenterX, CenterY,
CenterX + (int)(length * Math.Sin(angle)),
CenterY - (int)(length * Math.Cos(angle)));
}
public static void Main() => Application.Run(new ClockForm());
}