winform时钟c#代码
代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Simpclock 11 { 12 public partial class Form1 : Form 13 { 14 DateTime date = DateTime.Now; 15 16 public Form1() 17 { 18 InitializeComponent(); 19 20 } 21 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 date = DateTime.Now; 25 26 } 27 28 private void OnPaint(object sender, PaintEventArgs e) 29 { 30 Font font = new Font("Times New Roman", 20); 31 Graphics g = CreateGraphics(); 32 g.DrawString(date.ToString(), font, Brushes.Firebrick, 10,330 ); 33 g.DrawString(date.DayOfWeek.ToString(), font, Brushes.Red, 250, 330); 34 DrawDial(g); 35 DrawSecondPointer(g); 36 DrawMinutePointer(g); 37 DrawHourPointer(g); 38 39 } 40 //刷新时间 41 private void OnTime(object sender, EventArgs e) 42 { 43 date = DateTime.Now; 44 Invalidate(); 45 } 46 //画钟表 47 //表盘部分 48 Point GetPosition(int s, Point center, double radius)//定位 49 { 50 Point p = new Point(); 51 double x = center.X + radius * Math.Sin(Math.PI / 30 * s); 52 double y = center.Y - radius * Math.Cos(Math.PI / 30 * s); 53 p.X = (int)x; 54 p.Y = (int)y; 55 return p; 56 } 57 58 void DrawDial(Graphics g)//外圆及刻度 59 { 60 int n; 61 Rectangle rect = new Rectangle(40, 10, 300, 300); 62 //g.FillEllipse(Brushes.White, 40, 10, 300, 300); 63 g.DrawEllipse(new Pen(Color.Black, 3), rect); 64 Point p1, p2; 65 Point center = new Point(190, 160); 66 for (n = 0; n < 60; n++) 67 { 68 p1 = GetPosition(n, center, 150); 69 if (n % 5 == 0) 70 { 71 p2 = GetPosition(n, center, 130); 72 g.DrawLine(new Pen(Color.Black, 2), p1, p2); 73 } 74 else 75 { 76 p2 = GetPosition(n, center, 140); 77 g.DrawLine(Pens.Red, p1, p2); 78 } 79 } 80 Font font = new Font("Times New Roman", 20); 81 n = 0; 82 p1 = GetPosition(n, center, 130); 83 g.DrawString("XII", font, Brushes.Black, p1.X - 25, p1.Y); 84 n += 15; 85 p1 = GetPosition(n, center, 130); 86 g.DrawString("III", font, Brushes.Black, p1.X - 35, p1.Y - 15); 87 n += 15; 88 p1 = GetPosition(n, center, 130); 89 g.DrawString("VI", font, Brushes.Black, p1.X - 20, p1.Y - 30); 90 n += 15; 91 p1 = GetPosition(n, center, 130); 92 g.DrawString("IX", font, Brushes.Black, p1.X, p1.Y - 15); 93 } 94 //秒针部分 95 void DrawSecondPointer(Graphics g) 96 { 97 Point center = new Point(190, 160); 98 Point p; 99 p = GetPosition(date.Second, center, 130); 100 g.DrawLine(Pens.Red, center, p); 101 g.FillEllipse(Brushes.Red, new Rectangle(p.X - 2, p.Y - 2, 4, 4)); 102 103 } 104 //分针部分 105 void DrawMinutePointer(Graphics g) 106 { 107 Point center = new Point(190, 160); 108 Point p; 109 p = GetPosition(date.Minute, center, 120); 110 g.DrawLine(Pens.Blue, center, p); 111 //g.FillEllipse(Brushes.Blue, new Rectangle(p.X - 4, p.Y - 4, 8, 8)); 112 } 113 //时针部分 114 Point GetHourPosition(Point center, double radius) 115 { 116 Point p = new Point(); 117 int h = date.Hour; 118 int m = date.Minute; 119 double t = Math.PI / 6 * h + Math.PI / 360 * m; 120 double x = center.X + radius * Math.Sin(t); 121 double y = center.Y - radius * Math.Cos(t); 122 p.X = (int)x; 123 p.Y = (int)y; 124 return p; 125 } 126 void DrawHourPointer(Graphics g) 127 { 128 Point center = new Point(190, 160); 129 Point p = GetHourPosition(center, 100); 130 g.DrawLine(new Pen(Brushes.Black, 2), center, p); 131 //去指针圆尖 http://www.cnblogs.com/sosoft/ 132 // g.FillEllipse(Brushes.Black, 133 // new Rectangle(p.X - 6, p.Y - 6, 12, 12)); 134 g.FillEllipse(Brushes.YellowGreen, 135 new Rectangle(center.X - 6, center.Y - 6, 12, 12)); 136 137 138 } 139 140 } 141 }