获取窗口实例的窗口句柄 [英] Get Window instance from Window Handle

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

问题描述

我能够得到用下面的code运行的应用程序窗口句柄。

I am able get a Window handle from running applications using the following code.

foreach (ProcessModule module in process.Modules)
{
  if (module.ModuleName.Contains("PresentationFramework.dll") || module.ModuleName.Contains("PresentationFramework.ni.dll"))
  {
    IntPtr window = process.MainWindowHandle;
  }
}

不过,我想从这个处理程序获取窗口实例。这可能吗?

But I want to get the Window instance from this handler. Is it possible?

有快捷的想法吗?

推荐答案

在一个WPF应用程序(或的WinForms)有两个'对象'(即包含信息存储块),以一个窗口:

In a WPF application (or WinForms) there are two 'objects' (that is blocks of memory containing information) to a 'window':

  1. 在系统窗口对象。
  2. 在被管理对象包裹的系统对象。

进入系统窗口对象是通过窗口句柄(typeof运算HWND在非托管code,IntPtr的托管code)提供。给定一个窗口句柄,你已经获得的,您可以使用窗口API方法操纵该窗口 。您可以使用的P / Invoke这一点。

Access to the system window object is provided through the window handle (typeof HWND in unmanaged code, IntPtr in managed code). Given a window handle, which you already obtained, you can manipulate that window using the Window API methods. You can use p/invoke for this.

访问管理对象,它驻留在过程中(在托管过程中的情况下,或应用程序域)堆是被禁止的。这种存储器是与其它处理受保护的(1)。

Access to the managed object, which resides in the heap of the process (or AppDomain in the case of a managed process) is forbidden. This memory is 'protected' from other processes(1).

这对象可以处理(或应用程序域)之间共享的唯一方法是通过编组这两个过程的一部分合作努力。甚至访问另一个进程中一个窗口时,这适用于很多的Win32 API的方法。并非所有的访问是可能没有自定义编组。

The only way that objects can be shared between processes (or AppDomains) is through marshalling which is a cooperative effort on the part of both processes. This applies even to many of the Win32 API methods when accessing a window in another process. Not all access is possible without custom marshalling.

请注意,不像的WinForms,WPF不(正常)使用系统窗口控制。如果你的目的是操纵可视树在另一个WPF程序/域,你只是出于运气,除非这一过程提供了某种形式的自动化接口。

Note that unlike WinForms, WPF does not (normally) use system windows for controls. If your aim is to manipulate the visual tree in another WPF process/domain, you're simply out of luck unless that process provides some sort of Automation interface.

(1)虽然可以读取另一个进程的原始内存,在托管堆中的对象移动的目标。人们可以从来没有找到他们,即使你能以某种方式中止该进程的垃圾收集线程。

(1) While it is possible to read the raw memory of another process, objects on a managed heap are moving targets. One could never even find them, even if you could somehow suspend the garbage collecting thread of that process.

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

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