Tools used : .NetFramework Beta 1, Editplus.
Introduction
This example displays current system time continuously.
Classes and methods used in this example are
Graphics Class: Graphics Class encapsulates a GDI+ drawing surface. The FromHWND method is used to create a new instance of the Graphics class from a window handle. Clear method fills the entire drawing surface with the specified RGB color by using Color class. DrawString method draws a string using Font and SolidBrush classes. The DrawString method of graphics class displays the current time using DateTime.Now property.
Timer Class : Implements the windows-based timer that raises an event at user-defined intervals. Interval property indicates the time interval to raise the Tick Event. Enabled property indicates whether to raises the Tick Event, if it is false Tick Event does not raised. Tick Event of Timer class raises for every one second and displays the system time.
Source Code
/* Author Mokhtar B
Date 21st June, 2001
Company Adsoft Solutions Pvt. Ltd
Application Type Windows Forms
*/
using System;
using System.WinForms;
using System.Drawing;
public class TimeTicker:Form
{
private Graphics g;
private Font DispFont;
private string CDate;
private SolidBrush MyBrush;
// Constructor
public TimeTicker()
{
InitializeComponents();
}
//InitializeComponents method
public void InitializeComponents()
{
DispFont = new Font("Arial",35,FontStyle.Bold);
MyBrush = new SolidBrush(Color.Red);
CDate = DateTime.Now.ToString();
g = Graphics.FromHWND(this.Handle);
//Instantiating Timer Class
Timer aTimer = new Timer();
aTimer.Interval = 1000; // 1000 milliseconds
aTimer.Enabled = true;
aTimer.Tick += new EventHandler(OnTimer);
//Setting Form Properties
this.Size = new Size(275, 150);
this.Text = "Time Ticker";
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BorderStyle = FormBorderStyle.FixedSingle;
this.StartPosition=FormStartPosition.CenterScreen;
//Setting Form Icon through Icon Class
this.Icon = new Icon("clock.ico");
}
protected override void OnPaint(PaintEventArgs e)
{
g.DrawString(CDate.Substring(11) , DispFont,MyBrush,30,30);
}
//Timer Event
protected void OnTimer(object source, EventArgs e)
{
CDate = DateTime.Now.ToString();
g.Clear(Color.FromARGB(216,208,200));
g.DrawString(CDate.Substring(11) , DispFont,MyBrush,30,30);
}
public static void Main()
{
Application.Run(new TimeTicker());
}
}
Compilation
csc /t:winexe /r:System.dll /r:System.WinForms.dll /r:System.Drawing.dll /r:Microsoft.Win32.Interop.dll TimeTicker.cs