MODULE SecondsTimer; (* ========================================================================= Seconds Timer Example Author : Chris Burrows Created: Nov 2007 (c) 2007-2008 CFB Software http://www.cfbsoftware.com/cpide ========================================================================= *) IMPORT Sys := "[mscorlib]System", Cpm := "[System]System.ComponentModel", Wfm := "[System.Windows.Forms]System.Windows.Forms", Drw := "[System.Drawing]System.Drawing", WinMain; TYPE TimerForm* = POINTER TO RECORD (Wfm.Form) components: Cpm.Container; time: Sys.DateTime; timeDisplay: Wfm.Label; button: Wfm.Button; timer: Wfm.Timer END; VAR form*: TimerForm; (* ==================================================================== *) PROCEDURE (form: TimerForm) Dispose*(disposing: BOOLEAN); BEGIN IF disposing THEN IF form.components # NIL THEN form.components.Dispose() END; form.Dispose^(disposing) END END Dispose; (* ==================================================================== *) PROCEDURE (form: TimerForm) DisplayTime(), NEW; BEGIN form.timeDisplay.set_Text(form.time.ToString('mm:ss')) END DisplayTime; (* ==================================================================== *) PROCEDURE (form: TimerForm) ResetTime(), NEW; BEGIN form.time := Sys.DateTime.init(0); form.DisplayTime() END ResetTime; (* ==================================================================== *) PROCEDURE (form: TimerForm) EnableTimer(enabled: BOOLEAN), NEW; BEGIN form.timer.set_Enabled(enabled); IF enabled THEN form.button.set_Text("&Stop") ELSE form.button.set_Text("&Start") END END EnableTimer; (* ==================================================================== *) PROCEDURE (form: TimerForm) ButtonClick(sender: Sys.Object; e: Sys.EventArgs), NEW; BEGIN IF form.timer.get_Enabled() THEN form.EnableTimer(FALSE) ELSE form.ResetTime(); form.EnableTimer(TRUE) END END ButtonClick; (* ==================================================================== *) PROCEDURE (form: TimerForm) OnTimerTick(sender: Sys.Object; e: Sys.EventArgs), NEW; BEGIN form.time := form.time.AddSeconds(1); form.DisplayTime() END OnTimerTick; (* ==================================================================== *) PROCEDURE (form: TimerForm) InitializeComponent(), NEW; CONST displayHeight = 50; formHeight = 100; formWidth = 140; VAR column, row: INTEGER; BEGIN NEW(form.components); NEW(form.button); NEW(form.timer); form.SuspendLayout(); form.set_DoubleBuffered(TRUE); form.set_StartPosition(Wfm.FormStartPosition.CenterScreen); form.set_FormBorderStyle(Wfm.FormBorderStyle.FixedToolWindow); form.set_Text("Timer"); form.set_AutoScaleBaseSize(Drw.Size.init(5, 13)); form.set_ClientSize(Drw.Size.init(formWidth, formHeight)); NEW(form.timeDisplay); form.timeDisplay.set_Location(Drw.Point.init(0, 0)); form.timeDisplay.set_Dock(Wfm.DockStyle.Top); form.timeDisplay.set_BackColor(Drw.Color.get_Black()); form.timeDisplay.set_ForeColor(Drw.Color.get_Lime()); form.timeDisplay.set_Font(Drw.Font.init("Microsoft Sans Serif", 20, Drw.FontStyle.Regular, Drw.GraphicsUnit.Point, 0)); form.timeDisplay.set_TextAlign(Drw.ContentAlignment.MiddleCenter); form.timeDisplay.set_Height(displayHeight); form.get_Controls().Add(form.timeDisplay); (* Centre the button horizontally *) column := (formWidth - form.button.get_Width()) DIV 2; (* Centre the button vertically in the remaining space below the time display *) (* dh + ((fh - dh - bh) DIV 2) ==> (dh + fh - bh) DIV 2 *) row := (displayHeight + formHeight - form.button.get_Height()) DIV 2; form.button.set_Location(Drw.Point.init(column, row)); form.button.set_FlatStyle(Wfm.FlatStyle.System); form.get_Controls().Add(form.button); REGISTER(form.button.Click, form.ButtonClick); form.timer.set_Interval(1000); form.ResetTime(); form.EnableTimer(FALSE); REGISTER(form.timer.Tick, form.OnTimerTick); form.ResumeLayout(FALSE) END InitializeComponent; BEGIN Wfm.Application.EnableVisualStyles(); Wfm.Application.SetCompatibleTextRenderingDefault(FALSE); NEW(form); form.InitializeComponent(); Wfm.Application.Run(form) END SecondsTimer.