如何使用Timer控件更改字体颜色 [英] How can you change the font color using Timer control
问题描述
我正在尝试使用 Timer04
更改 Label01字体颜色
。我的要求是将字体颜色的十进制值从 0 开始增加到 16777215 。当字体颜色达到 16777215 时,字体颜色将再次降低为 0 。这将是一个连续的循环。如果基本三种颜色(* RGB )的增量和减量比例相同,则只有从黑色到白色以及从白色到黑色的增量和减量的比例相同。
因此,我定义了以下代码:
I am trying to change Label01 Font Color
using Timer04
. My requirement is to increase the decimal value of font color starting from 0 to 16777215. When the font colour will reach 16777215 then the same will be decreased to 0 again. It will be a continuous loop. If the proportions of increment and decrement for the basic three colors (*RGB) are the same then only it will be from Black to White and White to Black.
So I defined the following codes:
procedure TMainForm.Timer04Timer(Sender: TObject);
var
RedColor, GreenColor, BlueColor: integer;
begin
RedColor := 1;
GreenColor := 2;
BlueColor := 3;
if (RedColor >= 1) and (RedColor <= 255) then RedColor := RedColor + 5;
if (GreenColor >= 1) and (RedColor <= 255) then GreenColor := GreenColor + 5;
if (BlueColor >= 1) and (BlueColor <= 255) then BlueColor := BlueColor + 5;
Label01.Font.Color := RedColor + GreenColor + BlueColor;
end;
..
..
..
..
..
procedure TMainForm.FormCreate(Sender: TObject);
begin
Timer04.Enabled := true;
end;
但事实并非如此。
推荐答案
但是请在过程外部放置一些值来保存您的值-因为局部变量不存在(并且其中的值会丢失)在过程退出之后(否则将无法进行递归和多线程)
But take some external place to hold your value outside the procedure - because local variables do not exist (and the value in them is lost) after procedure exit (otherwise recursion and multithreading would be impossible)
type
TMainForm = class(TForm)
....
private
Luminosity: byte;
Direction: shortint;
end;
// Those variables exist in the form itself, outside of
// the procedure, thus can be used to hold the values you need.
procedure TMainForm.Timer04Timer(Sender: TObject);
begin
Label01.Font.Color := RGB(Luminosity, Luminosity, Luminosity);
if ((Luminosity = 0) and (Direction < 0)) or
((Luminosity = 255) and (Direction > 0))
then Direction := - Direction // go back
else Luminosity := Luminosity + Direction; // go forth
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Luminosity := 0;
Direction := +1;
Timer04.Enabled := true;
end;
变量是表单本身的成员,因此它们存在于过程之外,因此可以使用在过程退出后保留值。
The variables are members of the form itself, so they exist outside of the procedure, thus can be used to keep values after the procedure exits.
PS。在颜色摆动范围的末端,上面有一个稍微明显的延迟(通过更改符号而不是来更改颜色,它跳过了一个计数)。如果我在我的项目中这样做,那么我会添加额外的延迟(通过额外的计数器或通过调整计时器属性),以便用户确实看到该颜色停留了一段时间(并给了他一些时间来相对舒适地阅读文本) )。这不是任务所必需的,但是它将使IMHO用户体验更好。
PS. There above is a slightly noticeable delay at the ends of color swinging range (it skips one "count" by changing the sign instead of changing the color). If i'd did it for my projects i'd added yet extra delay (via extra counter or via tweaking timer properties) so user would really see that color stucking for a while (and giving him some time to read the text with relative comfort). That s not required by task, but it would make IMHO user experience better.
type
TMainForm = class(TForm)
....
private
var
Luminosity, Latch: byte;
Direction: shortint;
const
LatchInit = 5;
end;
// Those variables exist in the form itself, outside of
// the procedure, thus can be used to hold the values you need.
procedure TMainForm.TimerLabelColorTimer(Sender: TObject);
begin
if Latch > 0 then begin
Dec(Latch);
exit;
end;
LabelSwinging.Font.Color := RGB(Luminosity, Luminosity, Luminosity);
if ((Luminosity = 0) and (Direction < 0)) or
((Luminosity = 255) and (Direction > 0))
then begin
Direction := - Direction; // go back
Latch := LatchInit; // give user eyes time to relax
end else
Luminosity := Luminosity + Direction; // go forth
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Luminosity := 0; // optional: Delphi objects anyway do zero their internal
Latch := 0; // variables before entering the constructor
Direction := +1; // and that is required
end;
procedure TMainForm.FormShow(Sender: TObject);
begin
TimerLabelColor.Enabled := true;
end;
procedure TMainForm.FormHide(Sender: TObject);
begin
TimerLabelColor.Enabled := false;
end;
启用计时器在 OnCreate $ c中没有位置$ c>处理程序有两个原因:
Enabling the timer has no place for it in the OnCreate
handler for two reasons:
- 在
OnCreate
中需要的内容只需通过更改IDE的对象检查器中的属性将其放入DFM中即可。 - 更重要的是-在不可见的表单上更改标签颜色几乎没有意义,因此创建表单有点为时过早,无法启动计时器序列。
- What you want in
OnCreate
you can just put into DFM by changing properties in IDE's Object Inspector - More importantly - there is little sense to change label color on invisible form, so the creation of the form is somewhat too early to start the timer sequence.
这篇关于如何使用Timer控件更改字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!