在鼠标光标下识别组件不适用于TImage Control

Wob*_*Bob 3 delphi vcl delphi-xe3

我使用以下过程在Delphi XE3中识别鼠标下的控件.一切都很好vcl.contols.但是,当鼠标悬停在a上时TImage,不会返回控件名称.

procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: oolean);    
var
  ctrl : TWinControl;
begin    
  ctrl := FindVCLWindow(Mouse.CursorPos);     
  if ctrl <> nil then begin    
    Label2.caption := ctrl.Name;    
    //do something if mouse is over TLabeledEdit    
    if ctrl is TLabeledEdit the begin    
      Caption := TLabeledEdit(ctrl).Text;    
    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法来访问a的名字TImage- 我错过了一些非常简单的东西吗?

Dal*_*kar 6

FindVCLWindow找到后代TWinControl.由于TImage不是窗口控件而且它不继承TWinControl,FindVCLWindow因此无法找到它.就像它无法TWinControl在其祖先中找到任何其他没有类的控件一样.

但是,有类似的功能FindDragTarget将返回任何VCL控件,包括非窗口控件.

此函数也在声明中Vcl.Controls,就像FindVCLWindow

function FindDragTarget(const Pos: TPoint; AllowDisabled: Boolean): TControl;
Run Code Online (Sandbox Code Playgroud)

它有额外的参数 - AllowDisabled控制它是否会返回禁用的控件.

您应该重写您的方法如下 - 请注意ctrl必须重新声明为TControl

procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
var
  ctrl : TControl;
begin
  ctrl := FindDragTarget(Mouse.CursorPos, true);
  if ctrl <> nil then
    begin
      Label2.caption := ctrl.Name;
      ...
    end;
end;
Run Code Online (Sandbox Code Playgroud)