Digital Clock with Date

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class DigitalClock: Form
{
     public static void Main()
     {
          Application.Run(new DigitalClock());
     }
     public DigitalClock()
     {
          ResizeRedraw = true;
          Timer timer    = new Timer();
          timer.Tick    += new EventHandler(TimerOnTick);
          timer.Interval = 1000;
          timer.Start();
     }
     private void TimerOnTick(object obj, EventArgs ea)
     {
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx    = pea.Graphics;
          DateTime dt      = DateTime.Now;
          string   strTime = dt.ToString("d") + "
" + dt.ToString("T");
          SizeF    sizef   = grfx.MeasureString(strTime, Font);
          float    fScale  = Math.Min(ClientSize.Width  / sizef.Width,
                                      ClientSize.Height / sizef.Height);
          Font     font    = new Font(Font.FontFamily,
                                      fScale * Font.SizeInPoints);
   
          StringFormat strfmt = new StringFormat();
          strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
   
          grfx.DrawString(strTime, font, new SolidBrush(ForeColor), 
                          ClientRectangle, strfmt);
     }
}