Delphi 右滚动文本

Delphi 右滚动文本,delphi,Delphi,我有一个DelphiXe2项目来显示滚动文本(更好的“字幕文本”)。 在我的项目中,我有Timer1,Timer2,Button1,Button2,Label1和Label2。 我的目标是在按钮1之后的标签1上显示一些左滚动文本。单击使用Timer1在按钮1之后的标签2上显示一些右滚动文本。单击使用Timer2 我定义了以下代码: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, Sy

我有一个DelphiXe2项目来显示滚动文本(更好的“字幕文本”)。 在我的项目中,我有
Timer1
Timer2
Button1
Button2
Label1
Label2
。 我的目标是在
按钮1之后的
标签1
上显示一些左滚动文本。单击
使用
Timer1
按钮1之后的
标签2
上显示一些右滚动文本。单击
使用
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.    

我的问题是
左滚动
是使用
Timer1
进行的,但是
右滚动
不是使用
Timer2
进行的
Timer2Timer
中的
for
循环应该向下运行,而不是向上运行:

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;
但我建议不要使用可写常量,也不要使用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;

您是否尝试过一些调试,例如,使用调试器或在
Timer2Timer
中的
for
循环中放置
ShowMessage('cat')
?[N.b.:你不必使用“猫”——任何哺乳动物都会这么做。]你为什么要使用可写类型常量,这种自相矛盾且早已被弃用的“功能”?同样在你的“功能”中,我认为这是故意的。如果右滚动文本意味着复制最后一个字符和前面的所有字符;那么,左滚动不意味着复制最后一个字符和第一个字符吗?你应该能自己找到答案。你不需要那个空间