用滚动/移动消息delphi xe7显示信息

时间:2015-06-08 06:02:22

标签: delphi delphi-xe7

好日子先生/马 我想创建一个状态栏,其中包含滚动信息 Os版 当前用户名 日期和时间

    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

我的完整代码

我想要实现的是状态栏上的移动信息 如果可以,请帮助 感谢..

1 个答案:

答案 0 :(得分:4)

您不应使用Timer.Interval作为了望值来确定您应在状态栏中显示哪些数据。使用单独的变量来做到这一点。它会使你的代码更清晰。

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.