下面的做法简单些: TURLLabel = class(TLabel) Procedure WndProc(var message: TMessage); override; end ;................procedure TURLLabel.WndProc(var message: TMessage); begin //窗口过程 if (Message.Msg=CM_MOUSELEAVE) then begin Font.Color := ClRed ; Font.Style := Font.Style - [fsUnderLine] ; end ; if (Message.Msg = CM_MOUSEENTER) then begin Font.Color := clBlue ; Font.Style := Font.Style + [fsUnderLine] ; end ; inherited WndProc(Message);end;
嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻 嘻嘻嘻伴水逍遥剑嘻嘻嘻嘻 嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻procedure TForm1.FormCreate(Sender: TObject); begin with TButton.Create(Self) do begin Parent := Self; TabStop := False; Top := -100; Action := TAction.Create(Self); Action.OnUpdate := ActionUpdate; end; end;procedure TForm1.ActionUpdate(Sender: TObject); var vHandle: THandle; I, J: Integer; begin vHandle := WindowFromPoint(Mouse.CursorPos); for I := 0 to ComponentCount - 1 do if (Components[I] is TWinControl) and (TWinControl(Components[I]).Handle = vHandle) then with TWinControl(Components[I]) do for J := 0 to ControlCount - 1 do if Controls[J] is TLabel then if PtInRect(TLabel(Controls[J]).BoundsRect, ScreenToClient(Mouse.CursorPos)) then TLabel(Controls[J]).Font.Color := clRed else TLabel(Controls[J]).Font.Color := clWindowText; for J := 0 to ControlCount - 1 do if Controls[J] is TLabel then if PtInRect(TLabel(Controls[J]).BoundsRect, ScreenToClient(Mouse.CursorPos)) then TLabel(Controls[J]).Font.Color := clRed else TLabel(Controls[J]).Font.Color := clWindowText; end;
TLabelEx = class(TLabel)
private
FOnMouseEnter: TNotifyEvent;
FOnMouseLeave: TNotifyEvent;
protected;
procedure CMMouseEnter(var Message: TMessage); message CM_MouseEnter;
procedure CMMouseLeave(var Message: TMessage); message CM_MouseLeave;
public
constructor Create(AOwner: TComponent); override;
end;......
.....
,....
,....
....procedure CMMouseEnter(var Message: TMessage);
begin
if Assigned(FOnMouseEnter) then
FOnMouseEnter(self);
end;procedure CMMouseLeave(var Message: TMessage);
begin
if Assigned(FOnMouseLeave) then
FOnMouseLeave(self);
end;
TURLLabel = class(TLabel)
Procedure WndProc(var message: TMessage); override;
end ;................procedure TURLLabel.WndProc(var message: TMessage);
begin //窗口过程
if (Message.Msg=CM_MOUSELEAVE) then
begin
Font.Color := ClRed ;
Font.Style := Font.Style - [fsUnderLine] ;
end ;
if (Message.Msg = CM_MOUSEENTER) then
begin
Font.Color := clBlue ;
Font.Style := Font.Style + [fsUnderLine] ;
end ;
inherited WndProc(Message);end;
嘻嘻嘻伴水逍遥剑嘻嘻嘻嘻
嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻procedure TForm1.FormCreate(Sender: TObject);
begin
with TButton.Create(Self) do begin
Parent := Self;
TabStop := False;
Top := -100;
Action := TAction.Create(Self);
Action.OnUpdate := ActionUpdate;
end;
end;procedure TForm1.ActionUpdate(Sender: TObject);
var
vHandle: THandle;
I, J: Integer;
begin
vHandle := WindowFromPoint(Mouse.CursorPos);
for I := 0 to ComponentCount - 1 do
if (Components[I] is TWinControl) and
(TWinControl(Components[I]).Handle = vHandle) then
with TWinControl(Components[I]) do
for J := 0 to ControlCount - 1 do
if Controls[J] is TLabel then
if PtInRect(TLabel(Controls[J]).BoundsRect,
ScreenToClient(Mouse.CursorPos)) then
TLabel(Controls[J]).Font.Color := clRed
else TLabel(Controls[J]).Font.Color := clWindowText;
for J := 0 to ControlCount - 1 do
if Controls[J] is TLabel then
if PtInRect(TLabel(Controls[J]).BoundsRect,
ScreenToClient(Mouse.CursorPos)) then
TLabel(Controls[J]).Font.Color := clRed
else TLabel(Controls[J]).Font.Color := clWindowText;
end;