[RESOLVED] [2.0] Analog clock?
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.
Code:
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();
}
}
}
How should I do the back buffer? Also the error is on
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.
Re: [RESOLVED] [2.0] Analog clock?
Ya, I changed it to a thread now although now I get lower errors.
Code:
System.Threading.Thread t = new System.Threading.Thread(t_Elapsed);
t.Start();
void t_Elapsed()
{
using (Graphics g = this.CreateGraphics())
{
while (!this.IsDisposed)
{
MyPaint(g);
}
}
}
With the timer I was only get 60fps max. With the thread I get 400+.
although when closing I got this
"A generic error occurred in GDI+."
on
maing.DrawImageUnscaled(backbuffer, Point.Empty);
Re: [RESOLVED] [2.0] Analog clock?
Why would you need 400+ refreshes per second? A clock is only going to change it's display once every second so I would think that even 60 refreshes per second is excessive.
Re: [RESOLVED] [2.0] Analog clock?
I was just testing the framerate of GDI+.