I want to trap WM_SETFOCUS messages on all the current windows. What's the best way to do that?
I think SetWindowsHookEx can be used for this purpose. Can it be used with WH_C开发者_Go百科ALLWNDPROC or WH_MSGFILTER?
Also, its mentioned that hook procedure must be in separate DLL. Is it required. Can I not have hook procedure in the same DLL which installs the hook.
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.
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.
精彩评论