1. Home
  2. Computing & Technology
  3. Delphi Programming

Get the Control Under the Mouse in a Delphi application
How to know what Control is Under the Mouse in a Delphi application

By , About.com Guide

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. Delphi tips navigator:
» Controling the Number of a Delphi application Instances on a Terminal Server
« Graphical Progress Bar for Delphi applications - Partial / Continuous Move

Explore Delphi Programming
About.com Special Features

Reader's Choice Award Winners

What are the best instant messengers, apps, editors and more? You told us, for our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TMouse
  6. Get the Control Under the Mouse in a Delphi application

©2010 About.com, a part of The New York Times Company.

All rights reserved.