我使用以下过程在Delphi XE3中识别鼠标下的控件。一切都适用于vcl.contols
。但是,当鼠标位于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;
有没有一种简单的方法来访问TImage
的名字 - 我错过了一些非常简单的东西吗?
答案 0 :(得分:6)
FindVCLWindow
找到TWinControl
的后代。由于TImage
不是窗口化控件,并且它不会从TWinControl
继承,因此FindVCLWindow
将无法找到它。就像它无法在其祖先中找到任何其他没有TWinControl
类的控件一样。
但是,有类似的函数FindDragTarget
将返回任何VCL控件,包括非窗口控件。
此功能也在Vcl.Controls
中声明,就像FindVCLWindow
function FindDragTarget(const Pos: TPoint; AllowDisabled: Boolean): TControl;
它有额外的参数 - 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;