unit YDONGScrollLabel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,extctrls;
type
Directiontype=(FromLeftToRight,FromRightToLeft);
TYDONGScrollLabel = class(TLabel)
private
{ Private declarations }
fflag:boolean;
fhold:integer;
FDirection:directiontype;
FTimer:Ttimer;
FScroll:boolean;
Finterval:integer;
Foldcaption:string;
FLastScroll:string;
procedure ftimertimer(sender:tobject);
function Autoscroll:string;
protected
{ Protected declarations }
public
{ Public declarations }
constructor create(aowner:tcomponent); override;
procedure BeginScroll;
Procedure EndScroll;
published
{ Published declarations }
property Direction:directiontype read fdirection write fdirection;
property Interval:integer read finterval write finterval;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('ydong', [TYDONGScrollLabel]);
end;
constructor tydongscrolllabel.create(aowner:tcomponent);
begin
inherited create(aowner);
finterval:=200;
fscroll:=false;
fdirection:=fromlefttoright;
fflag:=false;
flastscroll:='';
end;
procedure tydongscrolllabel.BeginScroll;
var
oldwidth,i:integer;
s:string;
begin
foldcaption:=caption;
fflag:=false;
if fscroll then exit
else
begin
ftimer:=ttimer.Create(self);
ftimer.Enabled:=false;
ftimer.OnTimer:=ftimertimer;
ftimer.Interval:=finterval;
if not autosize then
begin
oldwidth:=width;
s:=caption;
autosize:=true;
update;
fhold:=(length(caption)*oldwidth) div width;
if fhold>length(caption) then
begin
setlength(s,fhold);
for i:=length(caption)+1 to fhold do
s:=' ';
caption:=s;
end
else
begin
fflag:=true;
caption:=foldcaption;
end;
autosize:=false;
width:=oldwidth;
update;
end;
ftimer.Enabled:=true;
fscroll:=true;
flastscroll:=caption;
end;
end;
procedure tydongscrolllabel.EndScroll;
begin
if not fscroll then exit
else
begin
ftimer.Enabled:=false;
ftimer.free;
fscroll:=false;
end;
end;
function tydongscrolllabel.Autoscroll:string;
var
ch1,ch2:char;
i:integer;
size:integer;
s:string;
begin
s:=flastscroll;
// s:=caption;
size:=length(s);
if fdirection=fromrighttoleft then
begin
ch1:=s[size];
if ord(s[1])>161 then
begin
ch2:=s[size-1];
s[size-1]:=s[1];
s[size]:=s[2];
for i:=1 to size-4 do
s:=s[I+2];
s[size-2]:=ch1;
s[size-3]:=ch2;
end
else
begin
s[size]:=s[1];
for i:=1 to size-2 do
s:=s[i+1];
s[size-1]:=ch1;
end;
end
else
begin
ch1:=s[1];
if ord(s[size])<=161 then
begin
s[1]:=s[size];
for i:=size downto 3 do
s:=s[i-1];
s[2]:=ch1;
end
else
begin
ch2:=s[2];
s[1]:=s[size-1];
s[2]:=s[size];
for i:=size downto 5 do
s:=s[i-2];
s[3]:=ch1;
s[4]:=ch2;
end;
end;
flastscroll:=s;
result:=s;
end;
procedure tydongscrolllabel.ftimertimer(sender:tobject);
var
s:string;
begin
if fscroll then
begin
if not fflag then caption:=autoscroll
else
begin
s:=autoscroll;
s:=copy(s,1,fhold);
caption:=s;
end;
end;
end;
end.