Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need handle of form under mouse

Posted on 2004-09-28
5
Medium Priority
?
205 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

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

597 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