Home > Mouse > Get the control Under Mouse in a Delphi application

Get the control Under Mouse in a Delphi application

Reference : http://delphi.about.com/od/delphitips2008/qt/find-vcl-window.htm

 

If, in your Delphi application you need to know what control is “under” the mouse while the mouse is being moved over the application, you can use RTL’s FindVCLWindow function.

FindVCLWindow

The VCLWindow function can locate the windowed control under a certain point – for example under the mouse pointer. You can use FindVCLWindow to identify the windowed control that is under the mouse from another control that has captured the mouse. The Pos parameter specifies the location that must be over the returned windowed control. If there is no windowed control under the Pos parameter, FindVCLWindow returns nil.For an example have a TApplicationEvents component on the main form of the application and handle its OnIdle event as:

//Handles OnIdle event of the ApplicationEvents1 control the MainForm
procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean) ;
var
  ctrl : TWinControl;
begin
  ctrl := FindVCLWindow(Mouse.CursorPos) ;

  if ctrl <> nil then
  begin
    Caption := ctrl.Name;

    //do something if mouse is over TLabeledEdit
    if ctrl is TLabeledEdit then
    begin
      Caption := TLabeledEdit(ctrl).Text;
    end;
  end;
end;

Note: Mouse.CursorPos returns the coordinate of the mouse pointer related to the screen.

Categories: Mouse Tags: ,
  1. Valerio Oliveira
    February 22, 2010 at 3:42 am

    Excelent post!
    Simple and direct..

    Regards!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: