00 Votes

Delphi/Lazarus: Get Handle of Window under Mouse Cursor

Question by Guest | 2015-12-22 at 21:20

I would like to specify the window that is currently under the mouse cursor using Delphi or Lazarus. The whole thing should work under the operating system Windows and it should work systemwide, not only in my own application. I need the Handle of that window.

Is this possible in some way? And if yes, can someone tell me how to do it?

ReplyPositiveNegative
Avatar
0Best Answer2 Votes

First of all, you have to determine the current mouse position. This can be done using the function GetCursorPos(). After that, you can use the function WindowFromPoint() to specify the window at this point. Here is an example:

var
  ACursorPos: TPoint;
  AHandle: HWND;
begin

  if GetCursorPos(ACursorPos) then begin

     AHandle := WindowFromPoint(ACursorPos);

     if AHandle <> 0 then begin
        SendMessage(AHandle, ..., ..., ...);
     end;
  end;  

end;

Because both functions may fail, you should implement conditions like I have done in the example. Only if you are sure that the Handle is not 0, that means, a window was found, you should go on and use the Handle. In the example above, we are sending a message to the corresponding window.
2015-12-24 at 10:09

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.