MODULE Countdown; (* ========================================================================= Countdown Timer Example Author : Chris Burrows Created: Jun 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 Digits = RECORD label: Wfm.Label; upDown: Wfm.NumericUpDown END; TimerForm = POINTER TO RECORD (Wfm.Form) components: Cpm.Container; zeroTime: Sys.DateTime; time: Sys.DateTime; timeDisplay: Wfm.Label; mins, secs: Digits; button: Wfm.Button; timer: Wfm.Timer END; VAR frm: TimerForm; (* ==================================================================== *) PROCEDURE (frm: TimerForm) Dispose*(disposing: BOOLEAN); BEGIN IF disposing THEN IF frm.components # NIL THEN frm.components.Dispose() END; frm.Dispose^(disposing) END END Dispose; (* ==================================================================== *) PROCEDURE FormatTime(time: Sys.DateTime): Sys.String; BEGIN RETURN time.ToString('mm:ss') END FormatTime; (* ==================================================================== *) PROCEDURE (frm: TimerForm) SetButtonLabel(), NEW; BEGIN IF frm.timer.get_Enabled() THEN frm.button.set_Text("&Stop") ELSE frm.button.set_Text("&Start") END END SetButtonLabel; (* ==================================================================== *) PROCEDURE (frm: TimerForm) btnStartClick(sender: Sys.Object; e: Sys.EventArgs), NEW; BEGIN IF frm.timer.get_Enabled() THEN frm.timer.Stop() ELSE frm.timer.Start(); frm.time := Sys.DateTime.init(0); frm.time := frm.time.AddSeconds(Sys.Convert.ToDouble(frm.secs.upDown.get_Value())); frm.time := frm.time.AddMinutes(Sys.Convert.ToDouble(frm.mins.upDown.get_Value())); frm.timeDisplay.set_Text(FormatTime(frm.time)) END; frm.SetButtonLabel() END btnStartClick; (* ==================================================================== *) PROCEDURE (frm: TimerForm) OnTimerTick(sender: Sys.Object; e: Sys.EventArgs), NEW; BEGIN IF Sys.DateTime.Compare(frm.zeroTime, frm.time) # 0 THEN frm.time := frm.time.AddSeconds(-1); frm.timeDisplay.set_Text(FormatTime(frm.time)) END; IF Sys.DateTime.Compare(frm.zeroTime, frm.time) = 0 THEN frm.timer.Stop(); frm.SetButtonLabel() END END OnTimerTick; (* ==================================================================== *) PROCEDURE (frm: TimerForm) OnActivatedAction(sender: Sys.Object; e: Sys.EventArgs), NEW; VAR b: BOOLEAN; BEGIN frm.mins.upDown.Select(0, 99); b := frm.mins.upDown.Focus() END OnActivatedAction; (* ==================================================================== *) PROCEDURE (frm: TimerForm) NewLabel(VAR lbl: Wfm.Label; s: Sys.String; col, row: INTEGER), NEW; BEGIN NEW(lbl); lbl.set_Location(Drw.Point.init(col, row)); lbl.set_Text(s); lbl.set_AutoSize(TRUE); frm.get_Controls().Add(lbl) END NewLabel; (* ==================================================================== *) PROCEDURE (frm: TimerForm) NewNumericUpDown(VAR ud: Wfm.NumericUpDown; max, width, col, row: INTEGER), NEW; BEGIN NEW(ud); ud.set_Location(Drw.Point.init(col, row)); ud.set_Size(Drw.Size.init(width, 20)); ud.set_Maximum(Sys.Convert.ToDecimal(max)); frm.get_Controls().Add(ud) END NewNumericUpDown; (* ==================================================================== *) PROCEDURE (frm: TimerForm) InitializeComponent(), NEW; CONST labelWidth = 40; updownWidth = 40; rowHeight = 16; margin = 20; col1 = 20; col2 = 80; VAR frmSize: Drw.Size; i, col, row: INTEGER; formWidth: INTEGER; BEGIN NEW(frm.components); NEW(frm.button); NEW(frm.timer); frm.timer.set_Interval(1000); frm.zeroTime := Sys.DateTime.init(0); frm.SuspendLayout(); frm.set_DoubleBuffered(TRUE); row := 0; NEW(frm.timeDisplay); frm.timeDisplay.set_Location(Drw.Point.init(0, 0)); frm.timeDisplay.set_Dock(Wfm.DockStyle.Top); frm.timeDisplay.set_BackColor(Drw.Color.get_Black()); frm.timeDisplay.set_ForeColor(Drw.Color.get_Lime()); frm.timeDisplay.set_Font(Drw.Font.init("Microsoft Sans Serif", 20, Drw.FontStyle.Regular, Drw.GraphicsUnit.Point, 0)); frm.timeDisplay.set_TextAlign(Drw.ContentAlignment.MiddleCenter); frm.timeDisplay.set_Height(40); frm.timeDisplay.set_Text(FormatTime(frm.zeroTime)); frm.get_Controls().Add(frm.timeDisplay); row := 3 * rowHeight; frm.NewLabel(frm.mins.label, "Mins", col1, row); frm.NewLabel(frm.secs.label, "Secs", col2, row); INC(row, 1 * rowHeight); frm.NewNumericUpDown(frm.mins.upDown, 59, updownWidth, col1+3, row); frm.NewNumericUpDown(frm.secs.upDown, 59, updownWidth, col2+3, row); formWidth := col2 + updownWidth + margin; INC(row, 2 * rowHeight); frm.button.set_FlatStyle(Wfm.FlatStyle.System); col := (formWidth - frm.button.get_Width()) DIV 2; frm.button.set_Location(Drw.Point.init(col, row)); frm.SetButtonLabel(); frm.get_Controls().Add(frm.button); REGISTER(frm.button.Click, frm.btnStartClick); INC(row, 2 * rowHeight); frm.set_StartPosition(Wfm.FormStartPosition.CenterScreen); frm.set_FormBorderStyle(Wfm.FormBorderStyle.FixedToolWindow); frm.set_Text("Countdown Timer"); frm.set_AutoScaleBaseSize(Drw.Size.init(5, 13)); frm.set_ClientSize(Drw.Size.init(formWidth, row)); REGISTER(frm.Activated, frm.OnActivatedAction); REGISTER(frm.timer.Tick, frm.OnTimerTick); frm.ResumeLayout(FALSE) END InitializeComponent; BEGIN Wfm.Application.EnableVisualStyles(); Wfm.Application.SetCompatibleTextRenderingDefault(FALSE); NEW(frm); frm.InitializeComponent(); Wfm.Application.Run(frm) END Countdown.