Managed method for SetParent() on form

3.2k views Asked by At

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
1

There are 1 answers

0
Brad On BEST ANSWER

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