view plaincopy to clipboardPRint? using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D;
namespace AnalogClock {
public class AnalogClock : System.Windows.Forms.Form { private System.Windows.Forms.Timer timer1; private System.ComponentModel.IContainer components;
private void DrawClock(Graphics g) { const double pai = Math.PI; Point center = new Point (0, 0);
DateTime time = DateTime.Now; //读取时间; double secAng = 2.0*pai*time.Second/60.0; double minAng = 2.0*pai*(time.Minute + time.Second/60.0)/60.0; double hourAng = 2.0*pai*(time.Hour + time.Minute/60.0)/12.0; //各指针单位换算; int r = Math.Min( ClientSize.Width, ClientSize.Height ) / 2; int secHandLength = (int)(0.65*r); int minHandLength = (int)(0.6*r); int hourHandLength = (int)(0.5*r); //指针的长度定义;
Point secHand = new Point((int)(secHandLength*Math.Sin(secAng)), (int)(-secHandLength*Math.Cos(secAng))); Point minHand = new Point((int)(minHandLength*Math.Sin(minAng)), (int)(-minHandLength*Math.Cos(minAng))); Point hourHand = new Point((int)(hourHandLength*Math.Sin(hourAng)), (int)(-hourHandLength*Math.Cos(hourAng))); //刷新指针;
Pen SecPen = new Pen(Color.Red, 1); g.DrawLine(SecPen, center, secHand);
Pen MinPen = new Pen(Color.RoyalBlue, 3); g.DrawLine(MinPen, center, minHand);
Pen HourPen = new Pen(Color.CornflowerBlue, 5); g.DrawLine(HourPen, center, hourHand); //指针的样式定义; }