Solved

Need handle of form under mouse

Posted on 2004-09-28
5
186 Views
Last Modified: 2010-04-05
Hi folks,

Can anyone tell me how to get the form handle for the form under the mouse cursor??  This has to work for MDI Child forms as well.

Cheers!

Stu.
0
Comment
Question by:SJohnson
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12177172
you may try the api

WindowFromPoint-api will return the handle of the control or window

and after this use the GetParent-api multiple (in case you got a control handle)

just from head,
sorry no time for a sample

meikl ;-)
0
 
LVL 1

Author Comment

by:SJohnson
ID: 12177203
Hi Meikl,

I've tried that, but once the mouse leaves the bounds of my app, I no longer get mouse co-ordinates being sent back to the MouseMove event.

Any ideas on how I can do that?

Thanks mate.

Stu.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12177366
>I no longer get mouse co-ordinates being sent back to the MouseMove event.
yep, you may use a timer and the getcursorpos-api to retrieve the current mousepos

try to code a sample from head

var
  p : TPoint;
  h, hh : THandle;
begin
  p := getCursorPos;
  h := WindowFromPoint(p);
  hh := getParent(h);
  if hh <> 0 then
  begin
   while hh <> 0 do
   begin
     h := hh;
     hh := getParent(h);
   end;
  end;
  //now the window should be in h
  //you may additional use the IsWindow-api
end;

well, don't know if this works above

meikl ;-)
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 12180943
Stu,

If you use SetCapture, then you will get the mouse move event for the window handle that is passed in to SetCapture. Below is a very simple example that captures the mouse when the form's mousedown is fired, and releases the capture when the mouse button is released. It also displays the window handle being tracked while the mouse is captured.

Hope this helps,
Russell
 
----

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1            =  class(TForm)
     procedure      FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
     procedure      FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
     procedure      FormCreate(Sender: TObject);
     procedure      FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
     // Private declarations
     FTracking:     Boolean;
  protected
     // Protected declarations
     procedure      SetTracking(Value: Boolean);
  public
     // Public declarations
     property       Tracking: Boolean read FTracking write SetTracking;
  end;

var
  Form1:            TForm1;

implementation
{$R *.DFM}

procedure TForm1.SetTracking(Value: Boolean);
begin
  // Check state
  if (FTracking <> Value) then
  begin
     // Update state
     FTracking:=Value;
     // Handle mouse capture
     case FTracking of
        False :  ReleaseCapture;
        True  :  SetCapture(Handle);
     end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Set default tracking state
  FTracking:=False;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  // Start tracking
  Tracking:=True;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  // Stop tracking
  Tracking:=False;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var  hwndTrack:     Integer;
begin
  // Determine if we are tracking
  if Tracking then
  begin
     // Get window under mouse point
     hwndTrack:=WindowFromPoint(Mouse.CursorPos);
     // Display in the window title
     Caption:=Format('Tracking window %d', [hwndTrack]);
  end;
end;

end.
0
 
LVL 1

Author Comment

by:SJohnson
ID: 12185573
Hi Russell,

Awesome!  Thank you very much!  I'll give it a go now.

Stu.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now