Lazarus 在光标下找到控件

Lazarus find control under cursor

我正在使用这篇文章中的以下代码。 Code from Checked Answer

我需要从多个 TLabel 之一获取鼠标光标下的控件 (Label.Caption),当标签位于 Main From 时它工作正常。我将标签放在主窗体的面板上,现在只能找到面板。我只想让它在面板上的许多标签中的 select 几个标签上工作。

我尝试将标签的 Z 顺序更改为 "Bring To Front",但没有任何区别,仍然得到面板。既然标签在面板上,我如何才能再次找到光标下的标签?

Lazarus 似乎没有 FindVCLWindow 或 ObjectAtPoint。

procedure TForm1.Button1Click(Sender: TObject);
var
  ctrl : TControl;
  point : TPoint;
begin
  point := Mouse.CursorPos; // Mouse pos at screen
  Dec(point.X, Left); // Adjust for window.
  Dec(point.Y, Top);
  Dec(point.Y, GetSystemMetrics(SM_CYCAPTION)); // Adjust to client area.

  ctrl := ControlAtPos(point, True, True, True);

  // I added the following
  tStr:=ctrl.Name; // DEBUG: This now shows "Panel2"
  aStr:=(ctrl as TLabel).Caption; // This used to work

end;

尝试:

procedure TForm1.Button1Click(Sender: TObject);
var
    ctrl: TControl;
    pt: TPoint;
begin
    pt := ScreenToClient(Mouse.CursorPos);
    ctrl := ControlAtPos(pt, [capfRecursive, capfAllowWinControls]);
    if Assigned(ctrl) then
        Caption := ctrl.Name
    else
        Caption := Format('%d, %d', [pt.x, pt.y]);
end;