Results 1 to 4 of 4

Thread: [2005] Displaying an "external" window inside your form (or another container)

Threaded View

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    [2005] Displaying an "external" window inside your form (or another container)

    This code sets the parent of another window handle to any container on your form (or the form itself). You can launch a new process and stick it inside your form, or you can do it with an existing process.

    This example shows how to take an existing notepad process and parent set it as a child to the form.
    Im also using the SendMessage API to maximize the process' window once its on the form.

    NOTE: If the form closes with the process main window set as a child to your form, the process will terminate along with your form. Do not remove the code in the FormClosing event handler.
    VB.Net Code:
    1. 'Declaring some constants to use with the SendMessage API
    2.     Private Const WM_SYSCOMMAND As Integer = 274
    3.     Private Const SC_MAXIMIZE As Integer = 61488
    4.  
    5.     'This is the API that does all the hard work
    6.     <Runtime.InteropServices.DllImport("user32.dll")> _
    7.     Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    8.     End Function
    9.  
    10.     'This is the API used to maximize the window
    11.     <Runtime.InteropServices.DllImport("user32.dll")> _
    12.     Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    13.     End Function
    14.  
    15.     'This is the process that is currently set as a child to the form
    16.     Dim parentedProcess As Process
    17.  
    18.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    19.         'This is important! If you have a child process on your form, it will terminate along with your form if you do not set its parent to Nothing
    20.         If Not parentedProcess Is Nothing Then
    21.             SetParent(parentedProcess.MainWindowHandle, Nothing)
    22.         End If
    23.     End Sub
    24.  
    25.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    26.         SetProcessParent("notepad")
    27.     End Sub
    28.  
    29.     Private Sub SetProcessParent(ByVal processName As String)
    30.         'Retrieve an array of running processes with the given name
    31.         Dim processes() As Process = Process.GetProcessesByName(processName)
    32.         If processes.Length = 0 Then
    33.             MessageBox.Show("No processes by that name found")
    34.         Else
    35.             'If there already is a process set as a child to our form, we set its parent to Nothing, to make it go back to its normal state.
    36.             If Not parentedProcess Is Nothing Then
    37.                 SetParent(parentedProcess.MainWindowHandle, Nothing)
    38.             End If
    39.             'This forms new child will be the process on index 0 of the array
    40.             parentedProcess = processes(0)
    41.             SetParent(parentedProcess.MainWindowHandle, Me.Handle)
    42.             'SetParent(parentedprocess.MainWindowHandle,Me.Panel1.Handle) Try adding a panel to your form and use this line instead of the above line.
    43.  
    44.             'Now lets maximize the window of the process
    45.             SendMessage(parentedProcess.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    46.         End If
    47.     End Sub
    Last edited by Atheist; Jun 10th, 2008 at 04:22 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width