views:

39

answers:

2

Hello!

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?

+2  A: 

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

jdigital
Thank you very much, theorically it might do the job!I'll try and let you know ;)See you!
Doo Dee
So, I tried your solution and it works like a charm, and without any DLL!Thank you very much!
Doo Dee
+2  A: 

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.

Rob Kennedy
It's a little bit more complicated way of doing it, but that's an interesting technique, thank you very much :)
Doo Dee
More complicated than what? A mouse hook? For a mouse hook to intercept messages bound for another process, the hook procedure needs to be in a DLL. SetCapture works without needing a separate module.
Rob Kennedy
@DooDee: if you want "modal" behavior (where the user is selecting a window) then try Rob's solution; if this is non-modal (and you want the click to perform its normal function), then try the hook I mentioned above.
jdigital
@Rob:The mouse hook jdigital2 suggested works without any DLL.The hook procedure is actually in my main(), and it works :p
Doo Dee