Solved

Need handle of form under mouse

Posted on 2004-09-28
5
198 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 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
drawing animated level bar based on numbers 3 120
Firemonkey android show image from resource ? 1 70
Dev express lookupcombo 3 64
Twebbrowser add css to the header 3 52
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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