C#实现的GDI+时钟

using System;
002 using System.Data;
003 using System.Drawing;
004 using System.Drawing.Drawing2D;
005 class Clock
006 {
007         private Point mickeyMouse = new Point(0, 0);
008         private void Form1_Paint(object sender, PaintEventArgs e)
009         {
010             DrawClock(e.Graphics);
011             timer1.Start();
012         }
013  
014         private void DrawClock(Graphics g)
015         {
016             ///centre(120, 130);
017             Rectangle outRect = new Rectangle(0, 0, 240, 260);
018             Rectangle midRect = new Rectangle(7, 7, 226, 246);
019             Rectangle inRect = new Rectangle(10, 10, 220, 240);
020   
021             LinearGradientBrush outlBlueBrush = new LinearGradientBrush(outRect, Color.FromA#000064,
022                 Color.FromA#0000ff, LinearGradientMode.BackwardDiagonal);
023             LinearGradientBrush midlBlueBrush = new LinearGradientBrush(midRect, Color.FromA#0000ff,
024                 Color.FromA#000064, LinearGradientMode.BackwardDiagonal);
025             LinearGradientBrush inlBlueBrush = new LinearGradientBrush(inRect, Color.FromA#000064,
026                Color.FromA#0000ff, LinearGradientMode.BackwardDiagonal);
027   
028             g.FillEllipse(outlBlueBrush, outRect);
029             g.FillEllipse(midlBlueBrush, midRect);
030             g.FillEllipse(inlBlueBrush, inRect);
031             outlBlueBrush.Dispose();
032             midlBlueBrush.Dispose();
033             inlBlueBrush.Dispose();
034             Font myFont = new Font("Arial", 20, FontStyle.Bold);
035             SolidBrush whiteBrush = new SolidBrush(Color.White);
036             g.DrawString("12", myFont, whiteBrush, 100, 10);
037             g.DrawString("6", myFont, whiteBrush, 110, 223);
038             g.DrawString("3", myFont, whiteBrush, 210, 120);
039             g.DrawString("9", myFont, whiteBrush, 10, 120);
040             g.DrawString("1", myFont, whiteBrush, 160, 26);
041             g.DrawString("2", myFont, whiteBrush, 194, 64);
042             g.DrawString("5", myFont, whiteBrush, 156, 210);
043             g.DrawString("4", myFont, whiteBrush, 192, 174);
044             g.DrawString("11", myFont, whiteBrush, 55, 28);
045             g.DrawString("10", myFont, whiteBrush, 22, 66);
046             g.DrawString("7", myFont, whiteBrush, 64, 210);
047             g.DrawString("8", myFont, whiteBrush, 28, 174);
048             myFont.Dispose();
049             whiteBrush.Dispose();
050             //DateTime;
051             g.TranslateTransform(120, 130, MatrixOrder.Append);
052             Pen hourPen = new Pen(Color.White, 6);
053             hourPen.SetLineCap(LineCap.RoundAnchor, LineCap.ArrowAnchor, DashCap.Flat);
054             Pen minutePen = new Pen(Color.White, 4);
055             minutePen.SetLineCap(LineCap.RoundAnchor, LineCap.ArrowAnchor, DashCap.Flat);
056             Pen secondPen = new Pen(Color.Red, 2);
057             int sec = DateTime.Now.Second;
058             int min = DateTime.Now.Minute;
059             int hour = DateTime.Now.Hour;
060             double secondAngle = 2.0 * Math.PI * sec / 60.0;
061             double minuteAngle = 2.0 * Math.PI * (min + sec / 60.0) / 60.0;
062             double hourAngle = 2.0 * Math.PI * (hour + min / 60.0) / 12.0;
063             Point centre = new Point(0, 0);
064             Point hourHand = new Point((int)(40 * Math.Sin(hourAngle)),
065             (int)(-40 * Math.Cos(hourAngle)));
066             g.DrawLine(hourPen, centre, hourHand);
067   
068             Point minHand = new Point((int)(80 * Math.Sin(minuteAngle)),
069             (int)(-80 * Math.Cos(minuteAngle)));
070             g.DrawLine(minutePen, centre, minHand);
071   
072             Point secHand = new Point((int)(80 * Math.Sin(secondAngle)),
073             (int)(-80 * Math.Cos(secondAngle)));
074             g.DrawLine(secondPen, centre, secHand);
075   
076             hourPen.Dispose();
077             minutePen.Dispose();
078             secondPen.Dispose();
079         }
080         private void timer1_Tick(object sender, EventArgs e)
081         {
082             this.Invalidate();             
083         }
084         private void hideToolStripMenuItem_Click(object sender, EventArgs e)
085         {
086             if (contextMenuStrip1.Items[0].ToString() == "Hide")
087             {
088                 this.Hide();
089                 contextMenuStrip1.Items[0].Text = "Show";
090                 return;
091             }
092   
093             if (contextMenuStrip1.Items[0].ToString() == "Show")
094             {
095                 this.Show();
096                 contextMenuStrip1.Items[0].Text = "Hide";
097                 return;
098             }
099         }
100         private void Form1_MouseDown(object sender, MouseEventArgs e)
101         {
102             mickeyMouse = new Point(-e.X, -e.Y);
103         }
104         private void Form1_MouseMove(object sender, MouseEventArgs e)
105         {
106             if (e.Button == MouseButtons.Left)
107             {
108                 Point mousePos = Control.MousePosition;
109                 mousePos.Offset(mickeyMouse.X, mickeyMouse.Y);
110                 Location = mousePos;
111             }
112         }
113         private void notifyIcon1_DoubleClick(object sender, EventArgs e)
114         {
115             if (contextMenuStrip1.Items[0].ToString() == "Show")
116             {
117                 this.Show();
118                 contextMenuStrip1.Items[0].Text = "Hide";
119                 return;
120             }
121         }
122 }

你可能感兴趣的:(C#)