Search code examples
vb.netformspanelmdichildmdiparent

Move a child out of panel


I use the code down below to start a cmd.exe window and move it into panel1 on my form. I added button1 to it and I like to use that button to move the child out of panel1, back "to the desktop". Does anybody know how to do that? I did some Googling and I can find a lot of examples on how to move a child into a panel or how to move it from one panel to another, but not how to move it out of a panel... Thanks for any help in advance! Kind regards, Eric

Imports System.Runtime.InteropServices
Public Class Form1
    Private WithEvents proc As New Process
    Public Const SW_SHOWMAXIMIZED As UInt32 = 3
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Int32) As Int32
    Private Declare Function DwmGetWindowAttribute Lib "dwmapi" (ByVal hwnd As IntPtr, ByVal dwAttribute As Integer, ByRef pvAttribute As RECT, ByVal cbAttribute As Integer) As Integer
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
    Public Structure RECT
        Public left, top, right, bottom As Integer
    End Structure
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        proc.EnableRaisingEvents = True
        proc.StartInfo.FileName = "cmd"
        proc.Start()
    End Sub
    Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        proc.WaitForExit(200)
        If SetParent(proc.MainWindowHandle, Panel1.Handle) <> IntPtr.Zero Then
            SetWindowPos(proc.MainWindowHandle, IntPtr.Zero, 0, 0, Width, Height, 0)
            ShowWindow(proc.MainWindowHandle, SW_SHOWMAXIMIZED)
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Code to move the child out of panel1
    End Sub
End Class

Solution

  • Thank you @john for your help, I have been able to get it working now. Kind regards, Eric

    Imports System.Runtime.InteropServices
    Public Class Form1
        Private WithEvents proc As Process
        Public Const SW_SHOWMAXIMIZED As UInt32 = 3
        Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
        Private Declare Function DwmGetWindowAttribute Lib "dwmapi" (ByVal hwnd As IntPtr, ByVal dwAttribute As Integer, ByRef pvAttribute As RECT, ByVal cbAttribute As Integer) As Integer
        Private Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
        Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
        Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
        Public Structure RECT
            Public left, top, right, bottom As Integer
        End Structure
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            proc.EnableRaisingEvents = True
            proc.StartInfo.FileName = "cmd"
            proc.Start()
        End Sub
        Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Shown
            proc.WaitForExit(200)
            If SetParent(proc.MainWindowHandle, Panel1.Handle) <> IntPtr.Zero Then
                SetWindowPos(proc.MainWindowHandle, IntPtr.Zero, 0, 0, Width, Height, 0)
                ShowWindow(proc.MainWindowHandle, SW_SHOWMAXIMIZED)
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SetParent(proc.MainWindowHandle, IntPtr.Zero)
        End Sub
    End Class