All pastes #1790944 Raw Edit

Stuff

public text v1 · immutable
#1790944 ·published 2010-02-09 04:13 UTC
rendered paste body
'Noble was here, Don't remove this line!
'Declaring some constants to use with the SendMessage API

          Private Const WM_SYSCOMMAND As Integer = 274

          Private Const SC_MAXIMIZE As Integer = 61488


          'This is the API that does all the hard work

          <Runtime.InteropServices.DllImport("user32.dll")> _

          Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer

          End Function

          'This is the API used to maximize the window

          <Runtime.InteropServices.DllImport("user32.dll")> _
 
          Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
  
          End Function

 
          'This is the process that is currently set as a child to the form
 
          Dim parentedProcess As Process

  
          Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  
              '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

              If Not parentedProcess Is Nothing Then
  
                  SetParent(parentedProcess.MainWindowHandle, Nothing)
 
              End If

          End Sub

          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
              SetProcessParent("notepad")
 
          End Sub


          Private Sub SetProcessParent(ByVal processName As String)
 
              'Retrieve an array of running processes with the given name
  
              Dim processes() As Process = Process.GetProcessesByName(processName)
  
              If processes.Length = 0 Then
  
                  MessageBox.Show("No processes by that name found")
  
              Else

                  '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.

                  If Not parentedProcess Is Nothing Then

                      SetParent(parentedProcess.MainWindowHandle, Nothing)
 
                  End If
 
                  'This forms new child will be the process on index 0 of the array
 
                  parentedProcess = processes(0)
  
                  SetParent(parentedProcess.MainWindowHandle, Me.Handle)
  
                  'SetParent(parentedprocess.MainWindowHandle,Me.Panel1.Handle) Try adding a panel to your form and use this line instead of the above line.
  

                  'Now lets maximize the window of the process

                  SendMessage(parentedProcess.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
              End If
          End Sub