Finishing the Client for an Internet Access Control Application - The Timer Window
(Page 4 of 4 )
The timer will keep the user informed of how much time she has left for the session. It will automatically put itself in the bottom right corner of the screen. When there is only one minute left, the user will be given a warning saying that they should save their work and the window will turn red. Below is a listing of the code that makes it all happen:
Listing 2:
unit utimer;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm2 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
StaticText1: TStaticText;
Image1: TImage;
StaticText2: TStaticText;
Label2: TLabel;
Label3: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
times:integer;
end;
var
Form2: TForm2;
implementation
uses uclient;
{$R *.dfm}
procedure TForm2.Timer1Timer(Sender: TObject);
var
BlockInput : function(Block: BOOL): BOOL; stdcall;
begin
times:=times-1;
label1.caption:=inttostr(times);
if times = 1 then
begin
form2.Color:=clred;
label3.caption:='You have one minute left,'+#13#10+'please save your work.';
end;
if times = 0 then
begin
//users time is up, disable the timer:
timer1.Enabled:=false;
//write to server, inform that times up and free this workstation
form1.tc.Socket.WriteLn('timesup:'+form1.edname.text+'@plchldr');
//form1.tc.Socket.WriteLn('timesup:'+form1.GetComputerName+'@plchldr');
//activate the screenblock or lock the workstation
form1.visible:=true;
form1.setoff:=true;
//close the timer form
form2.close;
end;
end;
procedure TForm2.FormShow(Sender: TObject);
begin
if times > 0 then
begin
form2.Color:=clblack;
label1.Caption:=inttostr(times);
label2.Caption:=inttostr(times);
timer1.enabled:=true;
end;
end;
end.
Most of the code in here is responsible for updating the user interface. The workhorse is the timer event, which we will discuss.
When a session is started, the main application that we discussed before sends the time that is allocated to the timer form in a variable called "times." The timer component that I'm using deducts one from the times variable. For example, if the times variable contains fifteen, then every minute that passes will deduct one from the variable until it reaches zero, at which time the session will be ended. Let's walk through the code that does all of this.
Code that sets session time, deducts one for every minute that passes, and updates the appropriate visual accordingly:
times:=times-1;
label1.caption:=inttostr(times);
When the times variable reaches the number one the warning to save the work is shown:
if times = 1 then
begin
form2.Color:=clred;
label3.caption:='You have one minute left,'+#13#10+'please save your work.';
end;
When the session time is used up, a message is sent to the server informing it of the change:
if times = 0 then
begin
//users time is up, disable the timer:
timer1.Enabled:=false;
//write to server, inform that times up and free this workstation
//this is the debug version of the communication, comment it out when implementing the application
form1.tc.Socket.WriteLn('timesup:'+form1.edname.text+'@plchldr');
{uncomment the line below if you are implementing this program}
//form1.tc.Socket.WriteLn('timesup:'+form1.GetComputerName+'@plchldr');
The workstation is then lock, i.e. keyboard and mouse access is blocked:
//activate the screen block or lock the workstation
form1.visible:=true;
form1.setoff:=true;
The timer form is closed:
//close the timer form
form2.close;
end;
Below is a screen shot of the server with three workstations connected to it.
The three client applications that you see are debug versions. The full client version will usually be located on a separate computer and will cover the entire screen.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |