show information with Rolling / moving messages delphi xe7

267 views Asked by At

Good day sir/ma i want to create a status bar with a rolling information like Os version current User Name Date and time

    unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    tmr2: TTimer;
    stsbr: TStatusBar;
    procedure tmr2Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

  procedure TForm1.tmr2Timer(Sender: TObject);
begin
  if tmr2.Interval = 3000 then begin
    stsbr.Panels[1].Text:= FormatDateTime('dddd' + ', ' + 'dd/mm/yyyy',date) + ', ' + TimeToStr(Time);
    tmr2.Interval := 3001;
  end else if tmr2.Interval = 3001 then begin
    tmr2.Interval := 3002;
    stsbr.Panels[1].Text:= 'PC Owner: '+GetUsersName+ ' - '+ GetLocalPCName;
  end else if tmr2.Interval = 3002  then begin
    tmr2.Interval := 3003;
    stsbr.Panels[1].Text:= GetOSVersion;
  end else if tmr2.Interval = 3003 then begin
    tmr2.Interval := 3000;
    stsbr.Panels[1].Text:= GetCPUName;

  end;

  procedure Form.FormCreate(Sender: TObject);
  begin
  tmr2Timer(Sender);
  end;
end

. that my full code

what i wanted to Achieve was a moving Information on a status bar Please Help if u can thanks..

1

There are 1 answers

2
Dalija Prasnikar On BEST ANSWER

You should not use Timer.Interval as lookout value to determine which data you should show in status bar. Use separate variable to do that. It will make your code cleaner.

unit Unit1;

interface

uses
  Winapi.Windows, System.SysUtils, System.Classes, System.Win.Registry, 
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.ComCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    tmr2: TTimer;
    stsbr: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure tmr2Timer(Sender: TObject);
  private
    status: integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetUsersName: string;
var
  Buf: array [0 .. MAX_PATH] of Char;
  BufSize: longword;
begin
  Buf[0] := #$00;
  BufSize := MAX_PATH;
  if Winapi.Windows.GetUserName(Buf, BufSize) then Result := Buf
  else Result := '';
end;

function GetLocalPCName: string;
var
  Buf: array [0 .. MAX_COMPUTERNAME_LENGTH] of Char;
  BufSize: longword;
begin
  Buf[0] := #$00;
  BufSize := MAX_COMPUTERNAME_LENGTH;
  if Winapi.Windows.GetComputerName(Buf, BufSize) then Result := Buf
  else Result := '';
end;

function GetOSVersion: string;
begin
  Result := TOSVersion.ToString;
end;

function GetCPUName: string;
var
  Reg: TRegistry;
begin
  Result := '';
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKeyReadOnly('\Hardware\Description\System\CentralProcessor\0') then
      begin
        Result := Reg.ReadString('ProcessorNameString');
        Reg.CloseKey;
      end;
  finally
    Reg.Free;
  end;
end;

procedure TForm1.tmr2Timer(Sender: TObject);
begin
  case status of
    0 : stsbr.Panels[1].Text:= FormatDateTime('dddd' + ', ' + 'dd/mm/yyyy',date) + ', ' + TimeToStr(Time);
    1 : stsbr.Panels[1].Text:= 'PC Owner: ' + GetUsersName + ' - ' + GetLocalPCName;
    2 : stsbr.Panels[1].Text:= GetOSVersion;
    else stsbr.Panels[1].Text:= GetCPUName;
  end;
  inc(status);
  if status > 3 then status := 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  status := 0;
  // this property can also be set through IDE form designer
  tmr2.Enabled := true;
  // show initial status data
  tmr2Timer(Sender);
end;

end.