delphivcldelphi-xe3

Identify Component under mouse cursor does not work with TImage Control


I am using the following Procedure to identify the controls under my mouse in Delphi XE3. All works well for vcl.contols. However when the mouse is over a TImage, no control name is returned.

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;

Is there a simple way to access the names of a TImage - am I missing something really simple?


Solution

  • FindVCLWindow finds descendants of TWinControl. Since TImage is not windowed control and it does not inherit from TWinControl, FindVCLWindow will not be able to find it. Just like it will not be able to find any other control that does not have TWinControl class among its ancestors.

    However, there is similar function FindDragTarget that will return any VCL control, including non-window ones.

    This function is also declared in Vcl.Controls, just like FindVCLWindow

    function FindDragTarget(const Pos: TPoint; AllowDisabled: Boolean): TControl;
    

    It has extra argument - AllowDisabled which controls whether it will return disabled controls or not.

    You should rewrite your method as following - note that ctrl must be redeclared as 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;