表单上 SetParent() 的托管方法

发布于 2024-10-17 15:10:12 字数 559 浏览 8 评论 0原文

如何将窗体显示为不在我的程序中的窗口的子窗口?

我有一个应该是父级的窗口句柄,但我在表单上没有看到任何 SetParent() 的托管方法。有吗?似乎 form.Show() 方法只接受实现 IWin32Window 的托管对象。

如果没有托管方法,那么声明 API 以最大程度地兼容未来系统的首选方法是什么?像这样?:

<DllImport("user32.dll")> _
Private Shared Function SetParent(hWndChild As IntPtr, hWndNewParent As IntPtr) As IntPtr
End Function

是否可以构建一个实现 IWin32Window 并以某种方式包装窗口的类?这样做会很方便,但我不熟悉 IWin32Window:

frmMyForm.Show(New NativeWindowWrapper(12345)) 'Where 12345 is the hWnd of the window I want to wrap

How can I show a form as a child of a window that isn't in my program?

I have a window handle to what should be the parent, but I don't see any managed method for SetParent() on a form. Is there one? It also seems that the form.Show() method only accepts managed objects implementing IWin32Window.

If there isn't a managed method, what is the preferred method for declaring the API for maximum compatibility with future systems? Like this?:

<DllImport("user32.dll")> _
Private Shared Function SetParent(hWndChild As IntPtr, hWndNewParent As IntPtr) As IntPtr
End Function

Is it possible to build a class that implements IWin32Window and somehow wraps up a window? It would be handy do something like this, but I am not familiar with IWin32Window:

frmMyForm.Show(New NativeWindowWrapper(12345)) 'Where 12345 is the hWnd of the window I want to wrap

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2024-10-24 15:10:12

哇哦,我刚刚找到了 IWin32Window 的文档,发现它只有一个属性...Handle。是的,那么我当然可以轻松地制作这个 NativeWindowWrapper 类...

我还没有测试它,但我确信它会工作得很好...

Public Class NativeWindowWrapper
    Implements IWin32Window

    Private _Handle As IntPtr

    Public ReadOnly Property Handle As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
        Get
            Return _Handle
        End Get
    End Property

    Public Sub New(ByVal Handle As IntPtr)
        Me._Handle = Handle
    End Sub
End Class

Oh wow, I just found the documentation on IWin32Window, and see that it is only one property... Handle. Yes, then of course I can easily make this NativeWindowWrapper class...

I haven't tested it yet, but I am sure it will work just fine...

Public Class NativeWindowWrapper
    Implements IWin32Window

    Private _Handle As IntPtr

    Public ReadOnly Property Handle As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
        Get
            Return _Handle
        End Get
    End Property

    Public Sub New(ByVal Handle As IntPtr)
        Me._Handle = Handle
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文