C#实现的GDI+时钟
转载
using
System;
003 |
using System.Drawing;
|
004 |
using System.Drawing.Drawing2D;
|
007 |
private Point mickeyMouse = new Point(0, 0);
|
008 |
private void Form1_Paint( object sender, PaintEventArgs e)
|
010 |
DrawClock(e.Graphics);
|
014 |
private void DrawClock(Graphics g)
|
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);
|
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);
|
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);
|
049 |
whiteBrush.Dispose();
|
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);
|
068 |
Point minHand = new Point(( int )(80 * Math.Sin(minuteAngle)),
|
069 |
( int )(-80 * Math.Cos(minuteAngle)));
|
070 |
g.DrawLine(minutePen, centre, minHand);
|
072 |
Point secHand = new Point(( int )(80 * Math.Sin(secondAngle)),
|
073 |
( int )(-80 * Math.Cos(secondAngle)));
|
074 |
g.DrawLine(secondPen, centre, secHand);
|
080 |
private void timer1_Tick( object sender, EventArgs e)
|
084 |
private void hideToolStripMenuItem_Click( object sender, EventArgs e)
|
086 |
if (contextMenuStrip1.Items[0].ToString() == "Hide" )
|
089 |
contextMenuStrip1.Items[0].Text = "Show" ;
|
093 |
if (contextMenuStrip1.Items[0].ToString() == "Show" )
|
096 |
contextMenuStrip1.Items[0].Text = "Hide" ;
|
100 |
private void Form1_MouseDown( object sender, MouseEventArgs e)
|
102 |
mickeyMouse = new Point(-e.X, -e.Y);
|
104 |
private void Form1_MouseMove( object sender, MouseEventArgs e)
|
106 |
if (e.Button == MouseButtons.Left)
|
108 |
Point mousePos = Control.MousePosition;
|
109 |
mousePos.Offset(mickeyMouse.X, mickeyMouse.Y);
|
113 |
private void notifyIcon1_DoubleClick( object sender, EventArgs e)
|
115 |
if (contextMenuStrip1.Items[0].ToString() == "Show" )
|
118 |
contextMenuStrip1.Items[0].Text = "Hide" ;
|
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。