tags:

views:

343

answers:

1

I am developing an application that sits in the system tray and can perform actions on the active window. But when the icon in the system tray is clicked, GetForegroundWindow() returns the taskbar. I need to get the window that was active before the taskbar was.

I've tried enumerating the desktop window with EnumWindows and GetWindow, but this is often turning up desktop gadgets and other top items that where not active last. Is it even possible, or the information completely lost when the window is deactivated?

+2  A: 

I think the only way to get that info is by installing a system wide hook (SetWindowsHookEx) on WH_CALLWNDPROC and capturing all WM_ACTIVATEAPP. This will even enable you to track the full history of which window was active when.

Andreas Magnusson
This works really well. However, I have an issue about some applications being unaffected because they are 64-bit (or 32-bit if I compile for 64-bit). Any ideas?
Nick Whaley
I can hook with both a 32-bit and a 64-bit DLL and use some sort of interprocess communication between the two but it is looking very messy.
Nick Whaley
Unfortunately I don't think there's a clean way of doing it, so two DLLs and some IPC is ASFAIK the only way to go.
Andreas Magnusson
Alternative to WH_CALLWNDPROC, use WH_CBT instead, and look for HCBT_ACTIVATE
Remy Lebeau - TeamB