Results 1 to 11 of 11

Thread: [RESOLVED] [2.0] Analog clock?

Threaded View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Resolved [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.
    Last edited by high6; Dec 14th, 2008 at 10:34 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width