using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace WindowsTime { public partial class TimeCustomControl : Control { public TimeCustomControl() { InitializeComponent(); this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true); } public DateTime Time; protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); } private void timer1_Tick(object sender, EventArgs e) { this.Time = DateTime.Now; this.Refresh(); } private void TimeCustomControl_Paint(object sender, PaintEventArgs e) { Graphics dc = e.Graphics; Pen pn = new Pen(ForeColor); SolidBrush br = new SolidBrush(ForeColor); InitCoordinates(dc); DrawDots(dc,br); DrawHourHand(dc,pn); DrawSecondHand(dc, pn); DrawMinuteHand(dc, pn); } public void InitCoordinates(Graphics dc) { if (this.Width == 0 || this.Height==0) { return; } dc.TranslateTransform(this.Width / 2, this.Height / 2); dc.ScaleTransform(this.Height/250f,this.Width/250f); } public void DrawDots(Graphics dc,Brush brush) { int iSize; for (int i = 0; i <=59; i++) { if (i % 5 ==0) { iSize = 15; } else { iSize = 5; } dc.FillEllipse(brush, -iSize / 2, -100 - iSize / 2, iSize, iSize); dc.RotateTransform(6); } } protected virtual void DrawHourHand(Graphics grfx, Pen pn) { GraphicsState gs = grfx.Save(); grfx.RotateTransform(360.0F * Time.Hour/12 +30.0F * Time.Minute / 60); grfx.DrawLine(pn, 0, 0, 0, -50); grfx.Restore(gs); } protected virtual void DrawMinuteHand(Graphics grfx, Pen pn) { GraphicsState gs = grfx.Save(); grfx.RotateTransform(360.0F * Time.Minute/60 + 6.0F * Time.Second/60); grfx.DrawLine(pn, 0, 0, 0, -70); grfx.Restore(gs); } protected virtual void DrawSecondHand(Graphics grfx, Pen pn) { GraphicsState gs = grfx.Save(); grfx.RotateTransform(360.0F * Time.Second/60); grfx.DrawLine(pn, 0, 0, 0, -100); grfx.Restore(gs); } } }