?
Solved

Need handle of form under mouse

Posted on 2004-09-28
5
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

770 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