for i:=0 to lable1.width do begin lable2.left:=lable2.left - 1 ;//左移一象素 if i:=lable1.width then begin lable2.left:=lable1.width; i:=0; end;end;
////////////////////////////////// //CopyRght(C)Stanely 2002-8-20//// //[email protected]/////////// //////////////////////////////////unit ScrollingLabel_San;interface uses windows,sysutils,classes,extctrls,stdctrls,controls;type TScrollingLabel_San=class(TLabel) private protected Timer:TTimer; CurrentIndex:integer; TotalString:string; ShowAll:boolean; Spaces:integer; procedure ClearText; function AddSpaces(AStr:string;Count:integer):string; procedure OnTimer(Sender:TObject); function GetLastSpace:integer; function GetTextWidth:integer; public procedure SetString(AStr:string); function GetString:string; function IsGoing:boolean; function GetInterval:integer; procedure SetInterval(new:integer); procedure Start; procedure Pause; procedure Go; constructor Create(AOwner:TWinControl;AString:string;RefreshInterval:integer;StartNow:boolean=true); destructor Destroy;override; end; implementation{ TScrollingLabel_San }function TScrollingLabel_San.AddSpaces(AStr: string; Count: integer): string; var i:integer; begin result:=astr; for i:=1 to count do begin result:=result+' '; end; end;procedure TScrollingLabel_San.ClearText; begin canvas.FillRect(canvas.ClipRect); end;constructor TScrollingLabel_San.Create(AOwner: TWinControl;AString:string; RefreshInterval: integer; StartNow: boolean); begin inherited create(aowner); self.Alignment:=taRightJustify; //parent:=aowner; autosize:=false; width:=30; height:=18; timer:=ttimer.Create(self); timer.Interval:=refreshinterval; timer.OnTimer:=self.ontimer; timer.Enabled:=startnow;
totalstring:=astring; currentindex:=1;end;destructor TScrollingLabel_San.Destroy; begin timer.Free; inherited; end;function TScrollingLabel_San.GetInterval: integer; begin result:=timer.Interval; end;function TScrollingLabel_San.GetLastSpace: integer; var j,i:integer; begin j:=0; for i:=length(caption) downto 1 do begin if caption[i]<>' ' then break; inc(j); end; result:=j; end;function TScrollingLabel_San.GetString: string; begin result:=totalstring; end;function TScrollingLabel_San.GetTextWidth: integer; begin result:=canvas.TextWidth(copy(totalstring,currentindex,length(totalstring))); end;procedure TScrollingLabel_San.Go; begin timer.Enabled:=true; end;function TScrollingLabel_San.IsGoing: boolean; begin result:=timer.Enabled; end;procedure TScrollingLabel_San.OnTimer(Sender: TObject); var c:string; begin if currentindex>length(totalstring) then begin c:=addspaces('',self.GetLastSpace); if canvas.TextWidth(c)<clientwidth then caption:=caption+' ' else currentindex:=1; end else begin caption:=copy(totalstring,1,currentindex); inc(currentindex); end; end;procedure TScrollingLabel_San.Pause; begin timer.Enabled:=false; end;procedure TScrollingLabel_San.SetInterval(new: integer); begin timer.Interval:=new; end;procedure TScrollingLabel_San.SetString(AStr: string); begin totalstring:=astr; end;procedure TScrollingLabel_San.Start; begin caption:=''; currentindex:=1; timer.Enabled:=true; end;end. //使用很简单, public部分就那几个函数。
用 Timer 控件不断改变 Label 控件的 Left 值,这样 Label 看起来就像是在移动了。事实上,动画都是基于这个思想的!
www.51delphi.com下
如果不用控件呢?
to: AV_15(C.C.Q.) (
用 Timer 控件不断改变 Label 控件的 Left 值的方法我当然知道,只是这种方法不能把在左边移出去的文字显示到右边区;
to myhfit()
只有文字
begin
lable2.left:=lable2.left - 1 ;//左移一象素
if i:=lable1.width then
begin
lable2.left:=lable1.width;
i:=0;
end;end;
//CopyRght(C)Stanely 2002-8-20////
//[email protected]///////////
//////////////////////////////////unit ScrollingLabel_San;interface
uses
windows,sysutils,classes,extctrls,stdctrls,controls;type
TScrollingLabel_San=class(TLabel)
private
protected
Timer:TTimer;
CurrentIndex:integer;
TotalString:string;
ShowAll:boolean;
Spaces:integer;
procedure ClearText;
function AddSpaces(AStr:string;Count:integer):string;
procedure OnTimer(Sender:TObject);
function GetLastSpace:integer;
function GetTextWidth:integer;
public
procedure SetString(AStr:string);
function GetString:string;
function IsGoing:boolean;
function GetInterval:integer;
procedure SetInterval(new:integer);
procedure Start;
procedure Pause;
procedure Go;
constructor Create(AOwner:TWinControl;AString:string;RefreshInterval:integer;StartNow:boolean=true);
destructor Destroy;override;
end;
implementation{ TScrollingLabel_San }function TScrollingLabel_San.AddSpaces(AStr: string;
Count: integer): string;
var
i:integer;
begin
result:=astr;
for i:=1 to count do
begin
result:=result+' ';
end;
end;procedure TScrollingLabel_San.ClearText;
begin
canvas.FillRect(canvas.ClipRect);
end;constructor TScrollingLabel_San.Create(AOwner: TWinControl;AString:string; RefreshInterval: integer; StartNow: boolean);
begin
inherited create(aowner);
self.Alignment:=taRightJustify;
//parent:=aowner;
autosize:=false;
width:=30;
height:=18;
timer:=ttimer.Create(self);
timer.Interval:=refreshinterval;
timer.OnTimer:=self.ontimer;
timer.Enabled:=startnow;
totalstring:=astring; currentindex:=1;end;destructor TScrollingLabel_San.Destroy;
begin
timer.Free;
inherited;
end;function TScrollingLabel_San.GetInterval: integer;
begin
result:=timer.Interval;
end;function TScrollingLabel_San.GetLastSpace: integer;
var
j,i:integer;
begin
j:=0;
for i:=length(caption) downto 1 do
begin
if caption[i]<>' ' then break;
inc(j);
end;
result:=j;
end;function TScrollingLabel_San.GetString: string;
begin
result:=totalstring;
end;function TScrollingLabel_San.GetTextWidth: integer;
begin
result:=canvas.TextWidth(copy(totalstring,currentindex,length(totalstring)));
end;procedure TScrollingLabel_San.Go;
begin
timer.Enabled:=true;
end;function TScrollingLabel_San.IsGoing: boolean;
begin
result:=timer.Enabled;
end;procedure TScrollingLabel_San.OnTimer(Sender: TObject);
var
c:string;
begin
if currentindex>length(totalstring) then
begin
c:=addspaces('',self.GetLastSpace);
if canvas.TextWidth(c)<clientwidth then
caption:=caption+' '
else
currentindex:=1;
end
else
begin
caption:=copy(totalstring,1,currentindex);
inc(currentindex);
end;
end;procedure TScrollingLabel_San.Pause;
begin
timer.Enabled:=false;
end;procedure TScrollingLabel_San.SetInterval(new: integer);
begin
timer.Interval:=new;
end;procedure TScrollingLabel_San.SetString(AStr: string);
begin
totalstring:=astr;
end;procedure TScrollingLabel_San.Start;
begin
caption:='';
currentindex:=1;
timer.Enabled:=true;
end;end.
//使用很简单, public部分就那几个函数。