[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:
'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
Re: [2005] Displaying another softwares window inside your form (or another container)
Hey,
You mentioned in another post this code, i have decided this is the better code as i understand this better.
I want to know how to make it so the process that is running can detect a key-press such as F12 or something like that, I have made the form so it has no border and is full screen then i get a process which is full screen in there when i push the key i want my form underneath to be able to do something, how can i do this?
Any help would be great :)
Thanks,
Lee.
Re: [2005] Displaying an "external" window inside your form (or another container)
Hey bro, just a quick question.
I havn't tried this but lets say you have a powerpoint slideshow in your form. Will your click on the form register on the powerpoint presentation and move the slide forward?
Re: [2005] Displaying an "external" window inside your form (or another container)
Yeah, Im almost certain that it should, since the window receives windows messages, no matter if your application is its parent or not.