1

我有一个 Delphi XE2 项目来显示滚动文本(更好的“选取框文本”)。在我的项目中,我有Timer1, Timer2, Button1,和. 我的目标是在使用后显示一些左滚动文本,在使用后显示一些右滚动文本。Button2Label1Label2Label1Button1.ClickTimer1Label2Button1.ClickTimer2

我定义了以下代码:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Timer1: TTimer;
    Timer2: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled := true;
  Timer2.Enabled := true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Timer1.Enabled := false;
  Timer2.Enabled := false;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.Interval := 100;
  Timer2.Interval := 100;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
const
{$WRITEABLECONST ON}
  ScrollingText : string = 'This is left scrolling text ';
{$WRITEABLECONST OFF}
var
  ScrollPosition: Integer;
begin
  Label1.Caption := ScrollingText;
  for ScrollPosition := 1 to (Length(ScrollingText) - 1) do
    begin
      ScrollingText[ScrollPosition] := Label1.Caption[ScrollPosition + 1];
      ScrollingText[Length(ScrollingText)] := Label1.Caption[1];
     end;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
const
{$WRITEABLECONST ON}
  ScrollingText : string = 'This is right scrolling text ';
{$WRITEABLECONST OFF}
var
  ScrollPosition: Integer;
begin
  Label2.Caption := ScrollingText;
  for ScrollPosition := (Length(ScrollingText) - 1) to 1 do
    begin
      ScrollingText[ScrollPosition] := Label2.Caption[ScrollPosition - 1];
      ScrollingText[Length(ScrollingText)] := Label2.Caption[1];
    end;
end;

end.    

我的问题是Left Scrolling正在使用Timer1Right Scrolling没有使用Timer2.

4

1 回答 1

3

for循环应该向下而Timer2Timer不是向上运行:

procedure TForm1.Timer2Timer(Sender: TObject);
const
{$WRITEABLECONST ON}
  ScrollingText : string = 'This is right scrolling text ';
{$WRITEABLECONST OFF}
var
  ScrollPosition: Integer;
begin
  Label2.Caption := ScrollingText;
  for ScrollPosition := (Length(ScrollingText) - 1) downto 2 do
    begin
      ScrollingText[ScrollPosition] := Label2.Caption[ScrollPosition - 1];
      ScrollingText[1] := Label2.Caption[Length(ScrollingText) - 1];
    end;
end;

但我建议不要使用 writeable const 也不要使用 for 循环:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ...
  Label2.Caption := 'This is right scrolling text ';
end;

procedure TForm1.Timer2Timer(Sender: TObject);
var
  S: String;
begin
  S := Label2.Caption;
  S := S[Length(S)] + Copy(S, 1, Length(S) - 1);
  Label2.Caption := S;
end;
于 2013-08-17T09:48:21.123 回答