hey guys,
i wrote an analog clock in c#, it works but after a few seconds it gets really unresponsive. i think this has something to do with the timer_tick method of the clock class. i posted the code below, sorry for the german comments, i will translate them if you want.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
namespace Stoppuhr
{
class Program
{
private static Form frm;
private static Clock clock;
public Program (Size size)
{
initComponents (size);
frm.Size = size;
clock.Location = new Point ( new Size ( frm.ClientRectangle.Size.Width / 6, frm.ClientRectangle.Size.Height / 6 ) );
frm.Controls.Add ( clock );
frm.BackColor = Color.Gray;
frm.TransparencyKey = Color.Gray;
frm.FormBorderStyle = FormBorderStyle.None;
frm.ShowDialog ();
}
public static void Main ( String [] args )
{
Program uhr = new Program ( new Size ( 400, 400 ) );
}
private static void initComponents (Size size)
{
frm = new Form ();
clock = new Clock (size);
}
}
class Clock : Panel
{
private static Zeiger sekunde;
private static Zeiger minute;
private static Zeiger stunde;
private static Timer tmr;
private static Ziffernblatt blatt;
private static int counter;
private static PaintEventArgs pea;
public Clock ( Size size )
{
this.Size = size;
initComponents ();
initZeiger ();
this.SetStyle ( ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true );
tmr = new Timer ();
tmr.Interval = 1000;
tmr.Start ();
}
private void initComponents ()
{
blatt = new Ziffernblatt ( this.Size );
sekunde = new Zeiger ( new Pen ( Color.Black, 2 ) );
minute = new Zeiger ( new Pen ( Color.Red, 4 ) );
stunde = new Zeiger ( new Pen ( Color.Blue, 5 ) );
counter = 0;
}
private void initZeiger ()
{
sekunde.Laenge = 140;
sekunde.Size = this.Size;
minute.Laenge = 140;
minute.Size = this.Size;
stunde.Laenge = 100;
stunde.Size = this.Size;
sekunde.Winkel = DateTime.Now.Second * 6;
minute.Winkel = DateTime.Now.Minute * 6;
stunde.Winkel = ( DateTime.Now.Hour % 12 ) * 30;
}
protected override void OnPaint ( PaintEventArgs e )
{
base.OnPaint ( e );
pea = e;
blatt.paint (e);
sekunde.paint ( e );
minute.paint ( e );
stunde.paint ( e );
tmr.Tick += new EventHandler ( tmr_Tick );
}
private void tmr_Tick ( object sender, EventArgs e )
{
sekunde.Winkel = DateTime.Now.Second * 6;
minute.Winkel = DateTime.Now.Minute * 6;
stunde.Winkel = ( DateTime.Now.Hour % 12 ) * 30;
this.Refresh ();
}
}
class Ziffernblatt : Panel
{
public Ziffernblatt ( Size size )
{
this.BackColor = Color.Transparent;
this.Size = new Size ( size.Width - size.Width / 3, size.Height - size.Height / 3 );
}
public void paint ( PaintEventArgs e )
{
this.OnPaint ( e );
}
protected override void OnPaint ( PaintEventArgs e )
{
this.Dispose ();
base.OnPaint ( e );
e.Graphics.TranslateTransform ( this.Size.Width / 2, this.Size.Height / 2 );
Brush brush = Brushes.Black;
Rectangle rect = new Rectangle ( -5, -5, 11, 11 );
e.Graphics.FillRectangle ( brush, rect );
for ( int x = 1; x <= 60; x++ )
{
Pen pen = new Pen ( Color.Black, 2 );
e.Graphics.DrawLine ( pen, 0, -this.Height / 2, 0, -( this.Height / 2 - this.Height / 20 ) );
e.Graphics.RotateTransform ( 6 );
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if ( ( x % 5 ) == 0 )
{
e.Graphics.DrawLine ( pen, 0, -this.Height / 2, 0, -( this.Height / 2 - this.Height / 10 ) );
}
if ( ( x % 15 ) == 0 )
{
pen.Width = 5;
e.Graphics.DrawLine ( pen, 0, -this.Height / 2, 0, -( this.Height / 2 - this.Height / 10 ) );
e.Graphics.DrawString ( "II", new Font ( "Arial", 11 ), brush, -5, -( this.Height / 2 - this.Height / 9 ) );
}
}
}
}
class Zeiger : Panel
{
Pen pen;
int laenge;
int breite;
int winkel;
public Zeiger ( Pen pen )
{
laenge = new int ();
breite = new int ();
this.pen = pen;
}
public int Laenge
{
get { return laenge; }
set { laenge = value; }
}
public int Winkel
{
get { return winkel; }
set { winkel = value; }
}
public void paint ( PaintEventArgs e )
{
this.OnPaint ( e );
}
protected override void OnPaint ( PaintEventArgs e )
{
this.Dispose ();
Graphics g = e.Graphics;
g.ResetTransform ();
g.TranslateTransform ( this.Width / 3, this.Height / 3 );
g.RotateTransform ( winkel );
g.DrawLine ( pen, 0, 0, 0, -this.Laenge );
}
}
}
as you can see, after i initialize a form i populate it with a clock-object which in turn holds 3 Zeiger(clock hand) objects and a ziffernblatt(number wheel) object. these 4 objects are then put into the clock-panel. i implemented a counter in the timer_tick method and noticed that it accumulated a huge value after a while. this is why i think the fault lies in said method.
i hope you can point me in the right direction here...