I know which using the GetForegroundWindow function i can get the current active window handle, but now i want to detect when the active window (of any application) changes (become active). the first solution which come to my mind is
- store the current value (handle) returned by the GetForegroundWindow function.
- using a timer component check again the value returned by GetForegroundWindow and compare with the old value.
I’m wondering if exist a better way to do this maybe using a windows message or something else.
Use
SetWindowsHookExto install a CBT hook. MSDN describes the callback function you’ll provide:The calls you’ll be interested in are the ones where the first parameter is
HCBT_Activate. ThewParamparameter will tell you the window handle.