views:

40

answers:

1

I am making an taskbar and I need to know when the active window is changed. I don't want to use a timer. I'm thinking that maybe there is a WndProc message I can intercept.

I'm using C#

+1  A: 

That requires a hook set by SetWindowsHookEx, WH_SHELL. The callback gets notifications like HSHELL_WINDOWACTIVATED and HSHELL_WINDOWCREATED so that you can know what's happening with the top-level windows. Check out the SDK article for "ShellProc" for the full list of notifications you can get. Just about all of them are relevant to implementing your own taskbar.

That was the good news. The bad news is that WH_SHELL is a global hook. It requires a DLL that can be injected into another process. Problem is, you can't write that DLL in managed code. Injecting managed code into an unmanaged process is not possible.

Back to good news again, somebody has solved that problem and has created an unmanaged DLL that is injectable and can interop with managed code. The project is here. No idea how good it is. I suspect it might not be quite UAC proof.

Hans Passant
It also doesn't work on 64-bit Windows. This requires two injection DLLs and two injector apps. Otherwise you'll only get events from applications of the same bitness as your injector/injection DLL.
Tergiver
Yup, that would be one complication.
Hans Passant