创建 Label 时对它们的 MouseMove 事件先赋值
TYourControl = class(TCustomControl)
...
private
procedure SubMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
public
constructor Create(AOwner: TComponent); override;
end;
implememtation
...
constructor TYourControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLabel1 := TLabel.Create(Self);
FLabel1.Parent := Self;
FLabel1.OnMouseMove := SubMouseMove;
...
FLabel2 := TLabel.Create(Self);
FLabel2.Parent := Self;
FLabel2.OnMouseMove := SubMouseMove;
...
end;
procedure TYourControl.SubMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
Point: TPoint;
begin
with TControl(Sender).Parent do
if Assign(OnMouseMove) then
begin
Point.x := X;
Point.y := Y;
ScreenToClient(TControl(Sender).ClientToScreen(Point));
OnMouseMove(Sender, Shift, Point.x, Point.y);
end;
end;
...
然后可以在 YourControl 的 MouseMove 事件中照教父说的那样去区分Label。