Mouse Digger – Show control under cursor

I worked in a large project that had lots of frames and lots of dynamically created controls. When the software was running was difficult to figure out which control is which and were is created.
So, I wrote this tiny piece of code that tells you which control is under the mouse. I was showing the Digger form only if the program was compiled in Debug mode, so it was not available to the customer but only to the developers.

 

The code is very very simple. It all resumes to a single recursive function called ShowParentTree.
We start calling the ShowParentTree from Digg which is called when the application goes idle:

procedure TfrmDigger.ApplicationEventsIdle(Sender: TObject; var Done: Boolean);
begin
   Digg;
end;

The Digg function looks like this. The magic is done by FindVCLWindow:

procedure TfrmDigger.Digg;
VAR Ctrl : TWinControl;
begin
 Ctrl := FindVCLWindow(Mouse.CursorPos); { It will not “see” disabled controls }

 if Ctrl <> NIL then
 begin
  VAR s:= ctrl.Name+ ‘ (‘+ ctrl.ClassName + ‘)’;
  Memo.Text:= s+ #13#10+ ShowParentTree(ctrl, 1);

 Caption := s;
 if ctrl is TLabeledEdit then
 Caption := Caption + ‘ Text: ‘+TLabeledEdit(ctrl).Text;
end;
end;

Once we got the control under the mouse, ShowParentTree digs down the parent of that control, and the parent’s parent and so on, with a recursive call to itself:

function ShowParentTree(Control: TControl; Depth: Integer): string; { Recursive }
VAR Ctrl: TControl;
begin
  Ctrl:= Control.Parent;
  if Ctrl = NIL
  then Result:= ”
  else
  begin
     Result:= System.StringOfChar(‘ ‘, Depth);
     Inc(Depth);
     Result:= Result+ ‘ ‘+ Ctrl.Name + ‘ (‘+ Ctrl.ClassName+ ‘)’+ #13#10+ ShowParentTree(Ctrl, Depth); { Recursive }
  end;
end;

We leave the recursive call once we reached deep down to the form.

_

A warning:  Disabled controls cannot be found/investigated. But replacing FindVCLWindow with FindDragTarget will fix that.

 

Full source code:

https://github.com/GabrielOnDelphi/Mouse-Digger

Leave a Comment

Scroll to Top