Doo Dee
Doo Dee

Reputation: 75

How to return the handle of a window when we click on it, without any DLL injection?

For one of my projects, I need to create a function that will return a handle to a window when the user click on it (any window displayed on screen, and anywhere inside that window). I know it is possible to use a global hook, but I think there must be a more simple way of doing that, without using any DLL injection.

In fact, I could intercept the left mouse click or intercept when a window is activated. Can I use one of those 2 solutions without any DLL injection?

Upvotes: 1

Views: 407

Answers (2)

Rob Kennedy
Rob Kennedy

Reputation: 163247

Call SetCapture. When you do that, all subsequent mouse events will go to your own window. When you get a click event, call ReleaseCapture, and then WindowFromPoint to find out what window resides at the point where you got the click event. The coordinates you get in the click event will be relative to the window you passed to SetCapture, to remember to convert them to screen coordinates first. Use ClientToScreen.

Upvotes: 2

jdigital
jdigital

Reputation: 12266

You could use a LowLevelMouseProc hook to intercept the click, and then use WindowFromPoint to determine the window. (I haven't actually tried this.)

Upvotes: 2

Related Questions