时钟控件代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace testgrid ... { public partial class ClockControl : UserControl ...{ DateTime dt; public ClockControl() ...{ InitializeComponent(); ResizeRedraw = true; Enabled = false; } public DateTime Time ...{ get ...{ return dt; } set ...{ Graphics g = CreateGraphics(); InitializeCoordinates(g); Pen pen = new Pen(BackColor); if (dt.Hour != value.Hour) ...{ DrawHourHand(g, pen); } if (dt.Minute != value.Minute) ...{ DrawHourHand(g, pen); DrawMinuteHand(g, pen); } if (dt.Second != value.Second) ...{ DrawMinuteHand(g, pen); DrawSecondHand(g, pen); } if (dt.Millisecond != value.Millisecond) ...{ DrawSecondHand(g, pen); } dt = value; pen = new Pen(ForeColor); DrawHourHand(g, pen); DrawMinuteHand(g, pen); DrawSecondHand(g, pen); g.Dispose(); } } protected override void OnPaint(PaintEventArgs e) ...{ //base.OnPaint(e); Graphics g = e.Graphics; Pen pen = new Pen(ForeColor); Brush br = new SolidBrush(ForeColor); InitializeCoordinates(g); DrawDots(g, br); DrawHourHand(g, pen); DrawMinuteHand(g, pen); DrawSecondHand(g, pen); } void InitializeCoordinates(Graphics g) ...{ if (Width == 0 || Height == 0) return; g.TranslateTransform(Width / 2, Height / 2); float fInches = Math.Min(Width / g.DpiX, Height / g.DpiY); g.ScaleTransform(fInches * g.DpiX / 2000, fInches * g.DpiY / 2000); } void DrawDots(Graphics g, Brush br) ...{ for (int i = 0; i < 60; i++) ...{ int iSize = i % 5 == 0 ? 100 : 30; g.FillEllipse(br, 0 - iSize / 2, -900 - iSize / 2, iSize, iSize); g.RotateTransform(6); } } protected virtual void DrawHourHand(Graphics g, Pen pen) ...{ GraphicsState gs = g.Save(); g.RotateTransform(360f * Time.Hour / 12 + 30f * Time.Minute / 60); g.DrawPolygon(pen, new Point[] ...{ new Point(0, 150), new Point(100, 0), new Point(0, -600), new Point(-100, 0) }); g.Restore(gs); } protected virtual void DrawMinuteHand(Graphics g, Pen pen) ...{ GraphicsState gs = g.Save(); g.RotateTransform(360f * Time.Minute / 60 + 6f * Time.Second / 60); g.DrawPolygon(pen, new Point[] ...{ new Point(0, 200), new Point(50, 0), new Point(0, -800), new Point(-50, 0) }); g.Restore(gs); } protected virtual void DrawSecondHand(Graphics g, Pen pen) ...{ GraphicsState gs = g.Save(); g.RotateTransform(360f * Time.Second / 60 + 6f * Time.Millisecond / 1000); g.DrawLine(pen, 0, 0, 0, -800); g.Restore(gs); } }} 窗体代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace testgrid ... { public partial class AnalogClock : Form ...{ ClockControl clk; public AnalogClock() ...{ InitializeComponent(); clk = new ClockControl(); clk.Parent = this; clk.Time = DateTime.Now; clk.Dock = DockStyle.Fill; clk.BackColor = Color.Black; clk.ForeColor = Color.White; Timer timer = new Timer(); timer.Interval = 100; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } void timer_Tick(object sender, EventArgs e) ...{ //throw new Exception("The method or operation is not implemented."); clk.Time = DateTime.Now; } }}