QUOTE
I'm having some ideas for my next project and I gotta strike while the iron is hot. Is it possible to have a program open inside your WindowsForm? To be clear, I mean literally a completely different program. Imagine a create a WinForm with two buttons, I click button1 and software X appears inside that WinForms as a child.
Using MDI Form perhaps?
Just wanna know before I start pouring energy into it and then find out it can't be done.
There is actually a pretty easy way to do this.
First, you need to add a panel to your form. This panel will be used to "host" the application.
Next, you need to the "System.Runtime.InteropServices" and the "System.Diagnostics" namespace to your namespaces:
csharp
using System.Diagnostics;
using System.Runtime.InteropServices;
Now, we need setup our WinAPI functions:
csharp
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndNewParent);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, Int32 wParam, Int32 lParam);
Now, inside the button click event, start the process, and set it's parent to the panel. In this example, I will be using notepad:
csharp
// Create a new process
Process proc;
// Start the process
proc = Process.Start("notepad.exe");
proc.WaitForInputIdle();
// Set the panel control as the application's parent
SetParent(proc.MainWindowHandle, this.panel1.Handle);
// Maximize application
SendMessage(proc.MainWindowHandle, 274, 61488, 0);
Hope this helps!
**EDIT** In "SendMessage()", 274 and 61488 are commands used to maximize the application. 274 represetns the "WM_SYSCOMMAND" message, which is the message that is sent to a window when the user chooses a command from the window menu. 61488 represents the "SC_MAXIMIZE" wParam, or the type of system command to use. So, by calling "SendMessage()" with these parameters, you are telling the application to maximize itself. **EDIT**
This post has been edited by lesPaul456: 10 Jun, 2009 - 09:49 AM