我有一個Delphi XE2項目顯示滾動文本(更好的「選框文本」)。 在我的項目中,我有Timer1
,Timer2
,Button1
,Button2
,Label1
和Label2
。 我的目標是在Label1
之後顯示一些左滾動文本Button1.Click
使用Timer1
和一些右滾動文本在Label2
之後Button1.Click
使用Timer2
。右滾動文本
我定義了下面的代碼:
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.
我的問題是沒有發生使用Timer2
是,Left Scrolling
使用Timer1
但Right Scrolling
發生。
你嘗試一些調試,例如,使用調試器或在'Timer2Timer'的'for'循環中放置'ShowMessage('cat')'? [N.b .:你不必使用「貓」 - 任何哺乳動物都會這樣做。] –
爲什麼你使用可寫類型常量,這是oxymoronic和早已棄用的「功能」? –