20

如何将从窗体/控件的 Handle 属性获取的句柄转换为 IWin32Window^ ?

4

3 回答 3

28

Control.FromHandle

(这将为您提供实现 IWin32Window 接口的 Control 对象。)

例如。

IntPtr myWindowHandle = IntPtr(someVal);
IWin32Window^ w = Control::FromHandle(myWindowHandle);

请注意,这依赖于“从表单/控件的 Handle 属性获取”的句柄。您不能将此技术与任意 Win32 窗口句柄一起使用。

于 2010-03-20T03:59:34.133 回答
27

.NET 框架直接支持一种更简单的方法,无需创建您自己的自定义类。您可以将其与任意Window 句柄一起使用。

给定IntPtr类型的ptrWindowHandle

using System.Windows.Forms;

NativeWindow nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(ptrWindowHandle);

System.Windows.Forms.NativeWindow实现了IWin32Window接口。

于 2012-06-08T23:50:47.213 回答
7

这似乎正是您所要求的。我自己从来没有做过,但它似乎相对简单:

从 Win32 句柄创建 IWin32Window

祝你好运!

于 2010-03-20T03:54:54.917 回答