Always wanted to learn about cos/sin so I am trying to make an analog clock. The problem is more in the GDI+ then the actual clock. This is what I am using.
How should I do the back buffer? Also the error is onCode:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Analog_Clock { public partial class Form1 : Form { public Form1() { InitializeComponent(); } const int HourLength = 100; const int MinuteLength = 150; const int SecondLength = 200; bool MyDisposed = false; void MyPaint() { Graphics maing = Graphics.FromHwnd(SafeGetHandle()); Bitmap backbuffer = new Bitmap(this.Width, this.Height); Graphics g = Graphics.FromImage(backbuffer); float centerx = this.Width / 2; float centery = this.Height / 2; g.Clear(Color.Black); //Draw the hour hand int hour = DateTime.Now.Hour % 12; g.DrawLine(new Pen(Color.Red, 3), centerx, centery, centerx + (float)(HourLength * Degrees.Cos((hour * 30) - 90)), centery + (float)(HourLength * Degrees.Sin((hour * 30) - 90))); //Draw the minute hand int minute = DateTime.Now.Minute; g.DrawLine(new Pen(Color.Red, 2), centerx, centery, centerx + (float)(MinuteLength * Degrees.Cos((minute * 6) - 90)), centery + (float)(MinuteLength * Degrees.Sin((minute * 6) - 90))); //Draw the second hand int second = DateTime.Now.Second; g.DrawLine(new Pen(Color.Red, 1), centerx, centery, centerx + (float)(SecondLength * Degrees.Cos((second * 6) - 90)), centery + (float)(SecondLength * Degrees.Sin((second * 6) - 90))); //Draw the millisecond hand for fun int milli = DateTime.Now.Millisecond; g.DrawLine(new Pen(Color.Red, 1), centerx, centery, centerx + (float)(20 * Degrees.Cos((milli * .36) - 90)), centery + (float)(20 * Degrees.Sin((milli * .36) - 90))); maing.DrawImageUnscaled(backbuffer, 0, 0); } public IntPtr SafeGetHandle() { if (this.InvokeRequired) return (IntPtr)this.Invoke(new SafeGetHandleD(SafeGetHandle)); return this.Handle; } public delegate IntPtr SafeGetHandleD(); private void Form1_Load(object sender, EventArgs e) { System.Timers.Timer t = new System.Timers.Timer(10); t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); t.Start(); } void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { MyPaint(); } private void Form1_Paint(object sender, PaintEventArgs e) { MyPaint(); } } }
return (IntPtr)this.Invoke(new SafeGetHandleD(SafeGetHandle));
when I close the app.
I tried adding ifs about this.IsDisposed. But it is false when it gets called.




Reply With Quote