In fact you can use SetParent
win32 function to set parent of your Cmd window
to your TabPage
. We can get the Handle
of the Cmd Window
by the property MainWindowHandle
of the Process
starting the cmd.exe
. Here is the code you can try, however there is a little nasty problem that when you focus on the Cmd window
, your form will look like lost focus:
[DllImport("user32")]
private static extern int SetParent(IntPtr hwndChild, IntPtr hwndParent);
[DllImport("user32")]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32")]
private static extern int SetWindowLong(IntPtr hwnd, int nIndex, long newLong);
[DllImport("user32")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndAfter, int x, int y, int cx, int cy, int flags);
public Form1() {
InitializeComponent();
//Start cmd
Process proc = Process.Start("cmd.exe");
//HandleCreated event handler
HandleCreated += (s,e) => {
BeginInvoke((Action)(()=>
{
System.Threading.Thread.Sleep(100);//wait for a while to make sure the MainWindowHandle is created
int oldLong = GetWindowLong(proc.MainWindowHandle, -16);//GWL_STYLE = -16
SetWindowLong(proc.MainWindowHandle, -16, oldLong & ~0xc00000);//WS_CAPTION = 0xC00000
SetWindowPos(proc.MainWindowHandle, IntPtr.Zero, 0, 0, tabControl1.TabPages[0].Width, tabControl1.TabPages[0].Height + 40, 0x40);//SWP_SHOWWINDOw = 0X40
SetParent(proc.MainWindowHandle, tabControl1.TabPages[0].Handle);
}));
};
}