如何找出哪个是当前在系统上运行的活动窗口 [英] How to find out which is the active current window running on a system

查看:155
本文介绍了如何找出哪个是当前在系统上运行的活动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要使用Visual Studio 2005用C#编写一个控制台/窗口服务应用程序,它将在系统上运行24X7.要求是
1)应该每5分钟触发一次事件,以找出哪个是当前活动窗口(具有键盘焦点)
在系统上运行
2)将其写入文本文件-在特定时间段内哪个应用程序处于活动窗口
3)此文本文件数据应加密.
请帮帮我.
问候,
Manushi

Hi,
I need to write a console/Window Service application in C# using Visual Studio 2005 which will run 24X7 on a system.Requirements are
1)An event should fire every 5 minutes to findout out which is current active window(which have keyboard focus)
running on a system
2)write it into text file -for a particular time which application was active window
3)This text file data should be encrypted.
Kindly help me out.
Regards,
Manushi

推荐答案

您最可能需要的窗口不是前景窗口.它在大多数情况下都可以使用,但是当应用程序运行对话框时可能无法获得预期的效果.

要执行下一步,您应该使用P/Invoke.
Windows非常顽皮,您所看到的可能不是您想要的.

因此,您可以执行以下操作:调用Windows API GetForegroundWindow,获取一些HWND作为返回值.将此HWND值用作API GetLastActivePopup的参数.最有可能的是,它将返回与其参数相同的HWND,但是在某些情况下,它将返回前景窗口所拥有的其他Windows HWND.这是真正的最重要.

最后一步取决于您的应用程序.在上一步中,您获得了两个窗口句柄;它们可以相同也可以不同.您可能需要它们中的一个,也可能只需要其中之一,这取决于您要对它们进行什么处理.


后续评论OP确认实际上是最后一个活动弹出窗口"是必需的.
活动窗口"是指具有键盘焦点或具有非弹出子项的弹出窗口,其中一个非子项具有键盘焦点".通过奇怪的Windows设计,它并不总是前景窗口.微软…:-)

请参阅以获取更多信息: http://msdn.microsoft.com/zh-我们/library/ff468919(v=VS.85).aspx [
Most probably the window you need is not the foreground window. It works in most cases, but can fail to get desired effect when the application runs a dialog.

To do the next step, you should use P/Invoke.
Windows is very naughty, what you thing is obvious may be not what you want.

So here is what you do: call Windows API GetForegroundWindow, get some HWND as a return value. Use this HWND value as a parameter of the API GetLastActivePopup. Most likely, it will return the same HWND as its parameter, but in certain cases, it will return some other HWND of the windows owned by your foreground windows. This is what''s really on top.

The last step depends on your application. From the previous step you got two window handles; they can be the same or not. You may need both of them or just one of them, depending on what you want to do with them.


A follow-up comment OP confirm the actually the "last active popup" is what required.
"Active window" means "a popup window holding keyboard focus or having non-popup children, one of them holding keyboard focus". By weird Windows design, it is not always a foreground window. Microsoft… :-)

See for more information: http://msdn.microsoft.com/en-us/library/ff468919(v=VS.85).aspx[^].

—SA


不清楚您的要求.但是要获取活动窗口,我们使用API​​调用GetForegroundWindow.此处的参考 http://msdn.microsoft.com/en-us /library/ms633505%28v=vs.85%29.aspx [
Not clear about your requirements. But to get the active windows we use the API call GetForegroundWindow. The reference here http://msdn.microsoft.com/en-us/library/ms633505%28v=vs.85%29.aspx[^]

Google help how to call this in your c#


这篇关于如何找出哪个是当前在系统上运行的活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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