如何获得窗口的句柄? [英] How can I get the handle of window?

查看:87
本文介绍了如何获得窗口的句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用关闭时,如何获取将成为前景窗口的窗口的句柄。这样做,我需要我的应用程序表单可见。例如,我有一个TopMost属性的表单是true.I点击任何其他程序,我的应用程序仍然是前景窗口。这意味着如果我关闭我的应用程序,其他程序将是前台窗口。如何在不关闭我的应用程序的情况下获取此其他程序窗口句柄?

How can I get the handle of the window that will be foreground window when my app is closed.While doing this, I need my application form is visible. For example I have a form with TopMost property is true.I clicked any other program and my app is still foreground window.This means if I closed my app,the other program will be foreground window. How can I get this other program window handle without closing my app?

推荐答案

请参阅我对该问题的评论。也许MSDN文档不是很清楚这个主题,但属性 TopMost 的具体目的是显示桌面上所有其他表单的顶部形式,无论它们是否激活:

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost%28v=vs.110%29.aspx [ ^ ] 。



这种效果很少使用,因为它会分散其他应用程序的注意力,所以它仅用于某些非常特殊的应用程序的一些非常重要的关键需求。如果你想要别的东西,你不应该使用 TopMost 。但是,如果将此属性设置为false将无法解决您尝试解决的问题,则应解释所需的行为。你总是需要解释你为什么想要的东西。







如何获得一个处理?



如果你有一个表单实例,那就是 Form.Handle

http://msdn.microsoft.com/en-us/library /system.windows.forms.control.handle.aspx [ ^ ]。



在所有其他情况下,您可以获得某个流程的实例(您的当前 System.Diagnostics.Process.GetCurrentProcess 或任何其他一个)并使用 System.Diagnostics.Process获取相应应用程序的主窗口的句柄.MainWindowHandle

http://msdn.microsoft.com/en-us/library/vstu dio / system.diagnostics.process.mainwindowhandle [ ^ ]。



注意可以抛出的异常是没有句柄。因此,您通常必须在try-catch块下调用此属性。



-SA
Please see my comment to the question. Maybe the MSDN documentation is not quite clear about the subject but the specific purpose of the property TopMost is to show the form of top of all other forms on the desktop regardless of their activation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost%28v=vs.110%29.aspx[^].

This effect is used quite rarely, because it can distract from other applications, so it is used only in some very important critical need of some very special applications. If you want something else, you should not use TopMost. But then, if setting this property to false won't solve the problem you are trying to solve, you should explain the behavior your want. And you always need to explain why you want something.



How to get a handle?

If you have a form instance, that's Form.Handle:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.handle.aspx[^].

In all other cases, you can get an instance of some process (your current System.Diagnostics.Process.GetCurrentProcess or any other one) and get the handle of a main window of the corresponding application using System.Diagnostics.Process.MainWindowHandle:
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.mainwindowhandle[^].

Pay attention for the exception which can be thrown is there is not handle. Therefore, you generally have to call this property under a try-catch block.

—SA


这篇关于如何获得窗口的句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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