-1

I want to build a citrix like win-form means i have a win-form inside which i want to run Cmd prompt. I want a win-form to have 3 - 4 tab and each tab having cmd prompt inside which some external jobs will run and show the status.

The CMD prompt should run inside the win-form only i don't want to use Process.Start() because it will open outside win-form.

I am Looking for some help or link because not able to find something on google. Or if anyone can suggest someother way

4

1 回答 1

2

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);                
          }));    
        };
}

enter image description here

于 2013-09-07T18:21:48.497 回答