【问题标题】:show information with Rolling / moving messages delphi xe7使用滚动/移动消息显示信息 delphi xe7
【发布时间】:2015-06-08 06:02:21
【问题描述】:

先生/妈妈好 我想创建一个带有滚动信息的状态栏,例如 操作系统版本 当前用户名 日期和时间

    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

我的完整代码

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

【问题讨论】:

    标签: delphi delphi-xe7


    【解决方案1】:

    您不应使用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.
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多