public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private PictureBox pictureBox1 = new PictureBox();
private Timer time = new Timer();
private void Form1_Load(object sender, System.EventArgs e)
{
// Dock the PictureBox to the form and set its background to white.
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.BackColor = Color.White;
pictureBox1.Location = new Point(0, 0);
//pictureBox1.
// Connect the Paint event of the PictureBox to the event handler method.
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
// Add the PictureBox control to the Form.
this.Controls.Add(pictureBox1);
time.Interval = 1000;
time.Tick += new EventHandler(timer1_Tick);
time.Start();
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics g = e.Graphics;
int secondHand = 100;
int pointX = pictureBox1.Width / 2;
int pointY = pictureBox1.Height / 2;
int second = DateTime.Now.Second;
int hour = DateTime.Now.Hour;
int minu = DateTime.Now.Minute;
//textBox1.Text = second.ToString();
//hour hand
double currentXH = secondHand * Math.Sin(Math.PI * 2 / 12 * hour) * 0.5;
double currentYH = secondHand * Math.Cos(Math.PI * 2 / 12 * hour) * 0.5;
//minute hand
double currentXM = secondHand * Math.Sin(Math.PI * 2 / 60 * minu) * 0.8;
double currentYM = secondHand * Math.Cos(Math.PI * 2 / 60 * minu) * 0.8;
//second hand
double currentX = secondHand * Math.Sin(Math.PI * 2 / 60 * second);
double currentY = secondHand * Math.Cos(Math.PI * 2 / 60 * second);
//draw clock
g.DrawEllipse(Pens.Blue, pointX - secondHand, pointY - secondHand, secondHand * 2, 2 * secondHand);
Pen mi = new Pen(Color.Black, 1);
Pen ho = new Pen(Color.Red, 2);
for (int i = 0; i < 60; i++)
{
double degree = Math.PI * 2 / 60 * i;
float xx = Convert.ToSingle(Math.Sin(degree)*secondHand+pointX);
float yy = Convert.ToSingle(pointY-Math.Cos(degree) * secondHand);
float xxmove = Convert.ToSingle(Math.Sin(degree) * 5);
float yymove = Convert.ToSingle(Math.Cos(degree) * 5);
if (i % 5 == 0)
{
g.DrawLine(ho, xx- xxmove, yy + yymove, xx, yy);
}
else
g.DrawLine(mi, xx - xxmove, yy + yymove, xx, yy);
}
g.DrawLine(new Pen(Color.Blue, 4), pointX, pointY, Convert.ToInt32(pointX + currentXH), Convert.ToInt32(pointY - currentYH));
g.DrawLine(new Pen(Color.Tomato, 2), pointX, pointY, Convert.ToInt32(pointX + currentXM), Convert.ToInt32(pointY - currentYM));
g.DrawLine(Pens.Black, pointX, pointY, Convert.ToInt32(pointX + currentX), Convert.ToInt32(pointY - currentY));
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
}