如何改變tlabel的顏色(100分)

  • 主题发起人 luckystar
  • 开始时间
L

luckystar

Unregistered / Unconfirmed
GUEST, unregistred user!
我想做一個label,當鼠標落到上面時﹐它就變顏色﹐我開始用label的ONMOUSEMOVE

事件﹐效果不好﹐鼠標移動的一快了﹐就反應不過來了﹐我于是想從tgraphiccontrol

類繼承重寫一個label,我知道有一個cm_mouseenter和cm_mouseleave(大概是)消息﹐

我在controls.pas中找到了它們的聲明﹐是delphi自定義的消息﹕cm_mouseenter=

cm_base+19,但是僅此而已﹐我想知道到哪里去找這些消息的幫助﹐以及它們是怎樣

實現的?另外﹐我看到tlabel是從tcustomlabel繼承來的﹐但我看過其源碼﹐為什么

沒有onmousemove方法的實現﹐只是有聲明了一個FOnMouseMove,不知是什么?

謝謝
 
onmousemove中加延时,sleep(xx) 防止反映不及
 
程序反应不过来的时候运行程序的人也不必需要看到这种效果!
你还可以定义一个全局变量来判断是否已经在这个标签上了,如果是就不需要运行相应的
代码!还可以用SetCaptureControl(Control: TControl)的方法来进行鼠标捕获!
 
to 張劍波﹕我的程式本來就反應不過來﹐加個sleep不是更加反應不過來了?
to hhzh426:我已經扑火到了鼠標﹐在tlabel中的onmousemove中有個x,y參數﹐是相對于
label左上角的坐標﹐我是判斷是否鼠標進入比label小一點的一個矩形內﹐如
果是﹐則是一種顏色﹐否則是另一種
to all :于是﹐我想從tgraphiccontrol繼承一個類出來﹐該類可以捕獲cm_mouseenter和
cm_mouseleave消息﹐做相應的動作﹐我現在的問題是﹐我到哪去找這些vcl自定義
消息的幫助?
 
还有WM_MOUSELEAVE事件.
 
我以前写了一个,现在贴出代码如下:

unit JHotLabel;

interface

uses
Messages, Controls, Classes, Graphics,
StdCtrls;

type
TJHotLabel = class(TLabel)
private
{ Private declarations }
FHotColor: TColor;
FHotStyle: TFontStyles;
FNormalColor: TColor;
FNormalStyle: TFontStyles;
procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(Owner: TComponent); override;
published
{ Published declarations }
property HotColor: TColor read FHotColor write FHotColor;
property HotStyle: TFontStyles read FHotStyle write FHotStyle;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('JComponents', [TJHotLabel]);
end;

procedure TJHotLabel.CMMouseEnter(var Msg: TMessage);
begin
FNormalColor := Font.Color;
FNormalStyle := Font.Style;
Font.Color := FHotColor;
Font.Style := FHotStyle;
end;

procedure TJHotLabel.CMMouseLeave(var Msg: TMessage);
begin
Font.Color := FNormalColor;
Font.Style := FNormalStyle;
end;

constructor TJHotLabel.Create(Owner: TComponent);
begin
FHotColor := clRed;
FHotStyle := [fsUnderline];
inherited Create(Owner);
Cursor := crHandPoint;
end;

end.
 
onmousemove:
if x>5 and x<label1.clientwidth-5 and y>5 and y<label1.clientheight-5 then
label1.font.color:=$00ff0000
else
label1.font.color:=$00000000;
这时,当鼠标移动得快一些的时候就会造成颜色不能改变的事情发生。
假设label1是直接放在form1上的,可以使用如下方法:
form1的onmousemove:
label1.font.color:=$00000000;
 
to chenlili:沒有wm_mouseleave這各消息?
我想知道這幾個vcl消息是怎樣實現的?
 
多人接受答案了。
 
来个简单一点的吧
type
mylabel=class(tlabel)
protected
procedure mouseenter(var msg:tmessage)
message cm_mouseenter;
procedure mouseleave(var msg:tmessage)
message cm_mouseleave;
end;
...
procedure mylabel.mouseenter(var msg:tmessage)
begin
color:=clblue;
end;

procedure mylabel.mouseleave(var msg:tmessage)
begin
color:=clred;
end;
 
顶部