views:

432

answers:

3

Hey, I'm using Windows Hook, I installed the mouse hook, system-wide and its working perfectly. Now there is a problem, I need to the get window handle on which the mouse was clicked.. How do I do that? Does the Mouse hook event passes us that information?

A: 

Use the WindowFromPoint function to get the window under a given location.

SLaks
But what if the user changed the window quickly? I may get the wrong handle right?
hab
If you call it inside the mouse hook, I don't think so.
SLaks
The mouse hook event also gives us the MOUSEHOOKSTRUCT, but its hwnd value is null, but its NULL, do you know why?
hab
I don't know; Google it.
SLaks
did you install a low level mouse hook, or a simple mouse hook ?
Adrien Plisson
I did SetWindowsHookEx, which one is that?
hab
@manzoor: it's the same call for the two types of hook, only the value of the idHook parameter change. what i want to know is whether you used WH_MOUSE_LL or WH_MOUSE ?
Adrien Plisson
I used WH_MOUSE_LL
hab
See my second answer.
SLaks
@SLaks: thank you.
Adrien Plisson
A: 

Assuming you set a WH_MOUSE hook, your MouseProc receives a pointer to a MOUSEHOOKSTRUCT struct. Since the hwnd member is NULL, you could try using WindowFromPoint with the pt member of the struct. The pt member is the coordinate at the time the message was created.

sean e
+1  A: 

Since you're using WH_MOUSE_LL, you're making a low-level mouse hook, which actually receives a pointer to a MSLLHOOKSTRUCT that doesn't have an hwnd member.

You need to set a normal mouse hook using WH_MOUSE; you'll then get a pointer to the MOUSEHOOKSTRUCT that you're expecting..

SLaks
Is it working now?
SLaks