using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Runtime.InteropServices; using System.Threading; namespace AnalogClock { public class MyClock : Form { private System.Windows.Forms.Timer timer1; private const int MIDX = 120; private const int MIDY = 120; private System.ComponentModel.IContainer components; public MyClock() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); //this.Cursor = new Cursor("c:\\swf\\winforms\\PaintBrushToolCursor.cur"); this.Cursor = new Cursor("c:\\swf\\winforms\\pb.cur"); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); timer1 = new System.Windows.Forms.Timer(); this.SuspendLayout(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); timer1.Tick += new EventHandler(Timer_Tick); this.FormBorderStyle = FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "MyClock"; this.Paint += new PaintEventHandler(f1_paint); this.Text = "Analogue clock"; this.Size = new Size(255, 270); this.Font = new Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Point, ((System.Byte)(0))); this.ResumeLayout(false); } static void Main() { Application.Run(new MyClock()); } public class PEA : PaintEventArgs { public PEA (Graphics g, Rectangle clip) : base(g, clip) { } ~PEA() { Dispose(true); } protected override void Dispose(bool disposing) { Console.WriteLine("Disposing PEA, {0}", disposing); base.Dispose (disposing); } } private void Timer_Tick(object sender, System.EventArgs e) { if (sender == timer1) { Invalidate(); } } void SubPaint(PaintEventArgs e) { //PaintEventArgs f; PEA f; //f = new PaintEventArgs(e.Graphics, e.ClipRectangle); f = new PEA(e.Graphics, e.ClipRectangle); Console.WriteLine("e.Hash={0:X}, f.Hash={1:X}", e.Graphics.GetHashCode(), f.Graphics.GetHashCode()); } protected override void OnPaint(PaintEventArgs e) { SubPaint(e); GC.Collect(); base.OnPaint (e); } private static void f1_paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Rectangle rect = new Rectangle(20, 20, 230, 230); LinearGradientBrush linearBrush = new LinearGradientBrush (rect, Color.FromArgb(0, 0, 0), Color.FromArgb(120, 120, 255), 225); g.FillEllipse(linearBrush, 20, 20, 200, 200); linearBrush.LinearColors = new Color[] {Color.FromArgb(120, 120, 255), Color.FromArgb(0, 0, 0)}; g.FillEllipse(linearBrush, 30, 30, 180, 180); linearBrush.LinearColors = new Color[] {Color.FromArgb(0, 0, 0), Color.FromArgb(120, 120, 255)}; g.FillEllipse(linearBrush, 33, 33, 174, 174); SolidBrush numeralBrush = new SolidBrush(Color.White); Font textFont = new Font("Arial Bold", 12F); g.DrawString("12", textFont, numeralBrush, 109, 40 ); g.DrawString("11", textFont, numeralBrush, 75, 50); g.DrawString("10", textFont, numeralBrush, 47, 75); g.DrawString("9", textFont, numeralBrush, 43, 110); g.DrawString("8", textFont, numeralBrush, 52, 145); g.DrawString("7", textFont, numeralBrush, 75, 170); g.DrawString("6", textFont, numeralBrush, 113, 180); g.DrawString("5", textFont, numeralBrush, 150, 170); g.DrawString("4", textFont, numeralBrush, 173, 145); g.DrawString("3", textFont, numeralBrush, 182, 110); g.DrawString("2", textFont, numeralBrush, 173, 75); g.DrawString("1", textFont, numeralBrush, 150, 50); g.TranslateTransform(120, 120, MatrixOrder.Append); int hour = DateTime.Now.Hour; int min = DateTime.Now.Minute; int sec = DateTime.Now.Second; SolidBrush hourBrush = new SolidBrush(Color.White); SolidBrush minuteBrush = new SolidBrush(Color.LightGray); Pen secondPen = new Pen(Color.Red, 1); double secondAngle = 2.0 * Math.PI * sec / 60.0; double minuteAngle = 2.0 * Math.PI * (min + sec / 60.0) / 60.0; double hourAngle = 2.0 * Math.PI * (hour + min / 60.0) / 12.0; Point centre = new Point(0, 0); GraphicsPath gpHour = new GraphicsPath(); Point[] HourArrow = new Point[4] { new Point((int)(40 * Math.Sin(hourAngle)), (int)(-40 * Math.Cos(hourAngle))), new Point((int)(-5 * Math.Cos(hourAngle)), (int)(-5 * Math.Sin(hourAngle))), new Point((int)(5 * Math.Cos(hourAngle)), (int)(5 * Math.Sin(hourAngle))), new Point((int)(40 * Math.Sin(hourAngle)), (int)(-40 * Math.Cos(hourAngle))) }; gpHour.AddPolygon(HourArrow); g.FillPath(hourBrush, gpHour); g.FillEllipse(hourBrush, -5, -5, 10, 10); GraphicsPath gpMinute = new GraphicsPath(); Point[] MinuteArrow = new Point[4] { new Point((int)(70 * Math.Sin(minuteAngle)), (int)(-70 * Math.Cos(minuteAngle))), new Point((int)(-5 * Math.Cos(minuteAngle)), (int)(-5 * Math.Sin(minuteAngle))), new Point((int)(5 * Math.Cos(minuteAngle)), (int)(5 * Math.Sin(minuteAngle))), new Point((int)(70 * Math.Sin(minuteAngle)), (int)(-70 * Math.Cos(minuteAngle))) }; gpMinute.AddPolygon(MinuteArrow); g.FillPath(minuteBrush, gpMinute); g.FillEllipse(minuteBrush, -5, -5, 10, 10); Point secHand = new Point((int)(70 * Math.Sin(secondAngle)), (int)(-70 * Math.Cos(secondAngle))); g.DrawLine(secondPen, centre, secHand); linearBrush.Dispose(); numeralBrush.Dispose(); textFont.Dispose(); hourBrush.Dispose(); minuteBrush.Dispose(); secondPen.Dispose(); } } }