tags:

views:

251

answers:

2

I want to trap WM_SETFOCUS messages on all the current windows. Whats the best way to do that.

I think SetWidnwosHookEx can be used to for this purpose. Can it be used with WH_CALLWNDPROC or WH_MSGFILTER ?

Also its mentioned that hook procedure must be in separate dll. Is it required. Can I not have hook procedure in same dll which installs hook.

+1  A: 

Yes you can have the hook proc in the same DLL that installs the hook. The requirement you are talking about has to do with when you are setting windows hooks in another process, I believe.

I'm not familiar with the WH_CALLWNDPROC hook type but I believe you can use the WH_CBT which is described as being useful for "computer based training" applications but seems to have some pretty useful functionality as well. One of the events it is notified about is:

HCBT_SETFOCUS
A window is about to receive the keyboard focus.

Josh Einstein
+1  A: 

A hook proc must be in a dll, but it doesn't have to be a different dll than the one you use to install the hook. The reason that the documentation mentions "separate dll" is because they assume that the code to install the hook will be in an exe not a dll.

A WH_CALLWNDPROC hook should see WM_SETFOCUS messages, I would not expect a WH_MSGFILTER hook to see them, but I don't know for sure.

You should be aware that if you intend to eat WM_SETFOCUS messages, this will NOT prevent focus from being set to the window.

Whatever your problem is, a windows hook is rarely the best solution. If you would be more specific about what you are trying to do, we could probably be more helpful.

John Knoeller
Although I've never used it personally, the CBTProc documentation states that focus can be stopped for the HCBT_SETFOCUS nCode. I agree though that it seems kinda heavy-handed and it's not the way things like the Tablet PC input panel work.
Josh Einstein
Yeah, the CBT hooks are sort of a special case. They are for 'Computer based training' so on the theory that you want to allow the computer to pretend to be the user at times, and to monitor what the user does - they basically put the whole system into a sort of single threaded mode and then permit the hook to seriously mess with things.
John Knoeller