您的位置:首页 > 编程语言 > Delphi

Delphi下TLabel鼠标MouseEnter、MouseLeave更改颜色失灵

2015-12-01 17:27 513 查看
在Delphi 7下,如果想在鼠标MouseEnter、MouseLeave的时候改变TLabel自身的颜色,很多人可能会采用 Label.Color := clRed;这样的方式来实现,我当初也是一样,结果无论鼠标怎么移动Tlabel都不会改变自身的颜色,但是通过Object Inspecter却可以设置TLabel的颜色,但是在它的MouseEnter、MouseLeave直接给Color属性赋值,TLabel的颜色却无法跟随鼠标的进入和移出而更改自身的颜色,经过跟踪发现TLabel的color属性原来根本就没有Color属性,而这个Color属性却是来自TControl的。

我先看一看三幅图:



这是最初的效果



这是鼠标进入的效果



这是鼠标移出的效果

下面给出代码:
procedure TForm1.Label1MouseEnter(Sender: TObject);
begin
//Label1.Canvas.Pen.Color := $0080BFFF;
with Label3 do
begin
if Color <> $0080BFFF then
begin
Color := $0080BFFF;
ParentColor := False;
Perform(CM_COLORCHANGED, 0, 0);
end;
end;
end;

procedure TForm1.Label1MouseLeave(Sender: TObject);
begin
with Label3 do
begin
if Color <> clWindow then
begin
Color := clWindow;
ParentColor := False;
Perform(CM_COLORCHANGED, 0, 0);
end;
end;
end;
http://www.lsworks.net/article/44.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: