Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am aware that I can use CBT functionality in the Windows API to achieve this, however, I thought it would be worth asking whether anyone has worked on something similar or if there is any plug-and-play library that can handle this.
Basically, my program is a Windows Forms program and I need to keep track of events such as when users activate or deactivate the main window of different programs on Windows. For example, if I have Word, Excel and Internet Explorer windows opened on the desktop; I want my application to receive notifications when the user switches from one program to another. For example when user switches from Word to Excel, I will receive one event (Deactivate) for the Word window and another event (Activate) for the Excel window.

Kindly advise if you know of any simpler way than Win32 CBT API to do this or if there is a library somewhere that implements this functionality already.

Many thanks

Terence
Posted

If you need to catch all such events globally, you need to use Windows Hooks.

Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx[^].

Note, that C# with P/Invoke would be enough to set up a hook using raw Windows API referenced above, but to install the hook to be system-global (this is what you probably want), this is not. According to Microsoft documentation, to install a global hook, you need to do in in a native DLL. You can load such DLL from your .NET application and communicate with its code using P/Invoke.

If you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

This CodeProject article can be also helpful: Essential P/Invoke[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 9-Feb-12 10:22am    
5'ed!
Sergey Alexandrovich Kryukov 9-Feb-12 12:13pm    
Thank you, Espen.
--SA
There are many resources on CP for using Global Hooks: whether any of them meet your exact need here, or can be adapted to your exact need: I don't know.

Be sure and read, re GlobalHooks in Win 7: "Change to Global Keyboard and Mouse Hooks behavior in Windows 7 by Dave Kreskowiak"[^].

Suggest you start your research with the frequently revised "classic" article, "Processing Global Mouse and Keyboard Hooks in C# by George Mamaladze | 1 Sep 2011" [^], and then search further on CP for "global hook."
 
Share this answer
 
v2
Comments
Espen Harlinn 9-Feb-12 10:23am    
5'ed!
There might be a library that wraps the functionality you're using, but what you're using is likely to be it otherwise. You can use p/invoke to call windows API functions in C#, but the underlying API is going to be written in C, that's some code that's been in Windows for a long time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900