function ControlFromPoint(mParent: TWinControl; mPoint: TPoint; mExcludeContols: array of TControl): TControl; var I: Integer; J: Integer; B: Boolean; begin Result := nil; if not Assigned(mParent) then Exit; for I := mParent.ControlCount - 1 downto 0 do begin B := False; for J := Low(mExcludeContols) to High(mExcludeContols) do if mParent.Controls[I] = mExcludeContols[J] then begin B := True; Break; end; if B then Continue; if PtInRect(mParent.Controls[I].BoundsRect, mPoint) then begin if not mParent.Controls[I].Visible then Continue; if mParent.Controls[I] is TWinControl then Result := ControlFromPoint(TWinControl(mParent.Controls[I]), Point(mPoint.X - mParent.Controls[I].Left, mPoint.Y - mParent.Controls[I].Top), mExcludeContols); if not Assigned(Result) then Result := mParent.Controls[I]; Break; end; end; end; { ControlFromPoint }procedure TForm1.Timer1Timer(Sender: TObject); var vControl: TControl; begin vControl := ControlFromPoint(Self, ScreenToClient(Mouse.CursorPos), []); (* vControl := ControlAtPos(ScreenToClient(Mouse.CursorPos), True, True); //系统方法TWinControl.ControlAtPos()但不支持子控件 *) if Assigned(vControl) then Caption := vControl.Name else Caption := '<nil>'; end;
mExcludeContols: array of TControl): TControl;
var
I: Integer;
J: Integer;
B: Boolean;
begin
Result := nil;
if not Assigned(mParent) then Exit;
for I := mParent.ControlCount - 1 downto 0 do begin
B := False;
for J := Low(mExcludeContols) to High(mExcludeContols) do
if mParent.Controls[I] = mExcludeContols[J] then begin
B := True;
Break;
end;
if B then Continue; if PtInRect(mParent.Controls[I].BoundsRect, mPoint) then begin
if not mParent.Controls[I].Visible then Continue;
if mParent.Controls[I] is TWinControl then
Result := ControlFromPoint(TWinControl(mParent.Controls[I]),
Point(mPoint.X - mParent.Controls[I].Left,
mPoint.Y - mParent.Controls[I].Top), mExcludeContols);
if not Assigned(Result) then Result := mParent.Controls[I];
Break;
end;
end;
end; { ControlFromPoint }procedure TForm1.Timer1Timer(Sender: TObject);
var
vControl: TControl;
begin
vControl := ControlFromPoint(Self, ScreenToClient(Mouse.CursorPos), []);
(*
vControl := ControlAtPos(ScreenToClient(Mouse.CursorPos), True, True);
//系统方法TWinControl.ControlAtPos()但不支持子控件
*)
if Assigned(vControl) then
Caption := vControl.Name
else Caption := '<nil>';
end;