用户停用和激活不同程序的主窗口时发生的事件 [英] Events when user deactivates and activates the main windows of different programs

查看:80
本文介绍了用户停用和激活不同程序的主窗口时发生的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我知道我可以在Windows API中使用CBT功能来实现此目的,但是,我认为值得询问是否有人在进行类似的工作,或者是否有任何即插即用库可以处理此问题. br/> 基本上,我的程序是Windows Forms程序,我需要跟踪事件,例如用户何时激活或停用Windows上不同程序的主窗口.例如,如果我在桌面上打开了Word,Excel和Internet Explorer窗口;我希望我的应用程序在用户从一个程序切换到另一个程序时收到通知.例如,当用户从Word切换到Excel时,我将在Word窗口中收到一个事件(停用"),而在Excel窗口中收到另一个事件(激活").

请告知您是否知道比Win32 CBT API更简单的方法来执行此操作,或者是否已经有某个库实现了此功能.

非常感谢

Terence

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

推荐答案

如果需要全局捕获所有此类事件,则需要使用Windows Hooks.

请参阅:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms632589%28v=vs.85%29.aspx [ http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms644990%28v=vs.85%29.aspx [ http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ].

此CodeProject文章也可能会有所帮助: Essential P/Invoke [
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


CP上有许多使用全局挂钩的资源:它们中的任何一个是否满足您的确切需求,或者是否可以适应您的确切需求:我不知道.

确保并阅读并重新赢取Win 7中的GlobalHooks:由Dave Kreskowiak更改Windows 7中的全局键盘和鼠标钩行为" [
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."


可能有一个包装了您正在使用的功能的库,但是您正在使用的可能是其他方式.您可以使用p/invoke在C#中调用Windows API函数,但是底层API将用C编写,这是Windows中已经存在很长时间的一些代码.
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.


这篇关于用户停用和激活不同程序的主窗口时发生的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆