Solved

Need handle of form under mouse

Posted on 2004-09-28
5
194 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 354
Printing problem 2 110
Error E2158 compiling with Delphi XE10 Seattle 2 124
Tvertscrollbox like a whatsapp layout 5 50
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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