In Depth Banner
Skip Navigation Links

Select your preferred language

The code for the BigClock class

using System;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

using xRay.Toolkit.Win32Defs;

using xRay.Toolkit.Utilities.Win32;

 

 

namespace WellFormed

{

  /// <summary>

  /// Summary description for BigClock.

  /// </summary>

  public class BigClock : NativeWindow

  {

 

    Timer t;

 

    int _currentTick;

 

    int _alpha=0;

 

    public BigClock()

    {

    }

 

    void DrawHand(Graphics g, float angle, float size)

    {

      Matrix mx=new Matrix(1,0,0,1,200,200);

      mx.Rotate(angle,MatrixOrder.Prepend);

      g.Transform=mx;

      Point[] poly=new Point[]{

                    new Point(0,-(int)size),

                    new Point(5,0),

                    new Point(0,10),

                    new Point(-5,0),

                    new Point(0,-(int)size)};

 

 

      g.FillPolygon(Brushes.Teal,poly);

      g.DrawPolygon(Pens.Black,poly);

 

    }

 

    void UpdateClock(Graphics g)

    {

      TimeSpan ts=DateTime.Now.TimeOfDay;

      g.Clear(Color.White);

      for(float f=0; f<360; f+=6)

      {

        Matrix mx=new Matrix(1,0,0,1,200,200);

        mx.Rotate(f,MatrixOrder.Prepend);

        g.Transform=mx;

 

        int size= f%30 == 0 ? 10 : 5;

        

        g.FillEllipse(Brushes.Teal,-size,-(180+size),2*size,2*size);

        g.DrawEllipse(Pens.Black,-size,-(180+size),2*size,2*size);

      }

      Pen p=new Pen(Color.Black,10);

      float angle=(float)(ts.TotalMinutes*6f);

      DrawHand(g,angle,160);

      angle=(float)(ts.TotalHours*30f);

      DrawHand(g,angle,100);

      angle=(float)(ts.TotalSeconds*6f);

      DrawHand(g,angle,160);

    }

 

 

    private void TickHandler(object sender, EventArgs e)

    {

 

      COLORREF cr=new COLORREF();

      cr.Color=Color.White;

      _currentTick++;

      if(_alpha<255 && _currentTick<60)

      {

        //clock fades in;

        _alpha+=32;

        if(_alpha>255)

          _alpha=255;

        Win32Methods.SetLayeredWindowAttributes(this.Handle,cr,(byte)_alpha,(int)LWADefs.LWA_COLORKEY | (int)LWADefs.LWA_ALPHA);

      }

      else if(_currentTick>60)

      {

        //clock fades out

        _alpha-=32;

        if(_alpha<0)

          _alpha=0;

        Win32Methods.SetLayeredWindowAttributes(this.Handle,cr,(byte)_alpha,(int)LWADefs.LWA_COLORKEY | (int)LWADefs.LWA_ALPHA);

        if(_alpha==0)

          this.Hide();

      }

      Win32Methods.RedrawWindow(this.Handle,IntPtr.Zero,IntPtr.Zero,(int)RDWDefs.RDW_INVALIDATE | (int)RDWDefs.RDW_NOERASE | (int)RDWDefs.RDW_INTERNALPAINT);

    }

 

    public void Create()

    {

      CreateParams cp=new CreateParams();

      cp.Style= (int)WSDefs.WS_POPUP;

      cp.ClassStyle=(int)CSDefs.CS_OWNDC;

      cp.ExStyle |= (int)WSDefs.WS_EX_LAYERED | (int)WSDefs.WS_EX_TOPMOST;

      cp.ClassName="tooltips_class32";

      cp.X=(SystemInformation.VirtualScreen.Width/2)-200;

      cp.Y=(SystemInformation.VirtualScreen.Height/2)-200;

      cp.Width=400;

      cp.Height=400;

      cp.Parent=IntPtr.Zero;

      CreateHandle(cp);

    }

 

    protected override void WndProc(ref Message m)

    {

      switch(m.Msg)

      {

        case (int)WMDefs.WM_PAINT:

          Paint();

          m.Result=IntPtr.Zero;

          break;

        case (int)WMDefs.WM_MOUSEMOVE:

          if(_currentTick<59)

            _currentTick=59;

          m.Result=IntPtr.Zero;

          break;

        default:

          base.WndProc (ref m);

          break;

      }

    }

 

    void Paint()

    {

      PAINTSTRUCT ps=new PAINTSTRUCT();

      Win32Methods.BeginPaint(this.Handle,ref ps);

 

      Graphics g=Graphics.FromHdc(ps.hdc);

      g.SmoothingMode=SmoothingMode.AntiAlias;

 

      UpdateClock(g);

      Win32Methods.EndPaint(this.Handle,ref ps);

    }

 

    public void Show()

    {

      Win32Methods.ShowWindow(this.Handle,(short)SWDefs.SW_SHOWNOACTIVATE);

      t=new Timer();

      t.Tick+=new EventHandler(this.TickHandler);

      t.Enabled=true;

    }

 

    public void Hide()

    {

      Win32Methods.ShowWindow(this.Handle, (short)SWDefs.SW_HIDE);

      t.Dispose();

      this.DestroyHandle();

    }

 

 

  }

}

 

Return to the article

Copyright © Bob Powell 2003-2009. All rights reserved