2013-03-13 83 views
3

我對使用winform應用程序執行以下操作感興趣,我打算在Visual Studio 2010 IDE的c#文件夾中打開一個.exe文件。在WinForm項目選項卡中運行exe文件

我目前能夠通過點擊按鈕在使用下面的代碼所需的選項卡打開程序:

 string str = @"-INSERT FILEPATH HERE-";//took file path out as i have a few exes i'm wanting to add. 
     Process process = new Process(); 
     process.StartInfo.FileName = str; 
     process.Start(); 

現在我怎樣才能使它所以這個可執行文件打開的選項卡或選項卡,在我的winform?我願意接受任何案例的建議。

解決:

using System.Runtime.InteropServices; 
    using System.Threading; 
    [DllImport("user32.dll", SetLastError = true)] 
    private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); 
    [DllImport("user32.dll", SetLastError = true)] 
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

    public GUI() 
    { 
     // Initialize form fieds 
     InitializeComponent(); 
     openProgram() 
    } 
    private void openProgram() 
    { 

     process.StartInfo.FileName = "-filepathhere-"; 

     process.Start(); 

     IntPtr ptr = IntPtr.Zero; 
     while ((ptr = process.MainWindowHandle) == IntPtr.Zero) ; 
     SetParent(process.MainWindowHandle, trackerPanel.Handle); 
     MoveWindow(process.MainWindowHandle, 0, 0, this.Width - 90, this.Height, true); 

    } 
+0

你的意思是在'winforms'使用'TabControl'? – spajce 2013-03-13 18:56:48

+0

是的,我希望它作爲選項卡在選項卡控件中。 – PhoenixLament 2013-03-13 23:53:52

+0

所以你試圖打開TabControl中的記事本,你可以嘗試爲該任務使用'richTextBox'。 – spajce 2013-03-13 23:57:07

回答

3

你可以使用的setparent API來設置可執行文件的窗口的父。將面板添加到TabControl中,並使用下面的代碼將可執行窗口的父項分配給面板的父項。

[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

private void button2_Click(object sender, EventArgs e) 
{ 
    var process = new Process(); 
    process.StartInfo.FileName = "notepad.exe"; 
    process.Start(); 
    SetParent(process.MainWindowHandle, panel1.Handle); 
} 

要從面板刪除窗口,使用相同的代碼,但設置父句柄IntPtr.Zero

SetParent(process.MainWindowHandle, IntPtr.Zero); 
+2

提供的代碼仍然會打開記事本作爲新窗口。仍然試圖在tabcontrol的面板中獲得這個。 – PhoenixLament 2013-03-13 23:53:21

相關問題