从C#启动另一个应用程序

时间:2010-04-22 08:48:53

标签: c# winforms

我正在尝试从C#应用程序启动另一个应用程序,有没有办法在我的应用程序的mainform中显示这个应用程序?

谢谢,

4 个答案:

答案 0 :(得分:6)

您可以使用Process.Start(...)启动其他应用程序:

Process.Start(@"C:\Path\OtherApp.exe");

要在表单中嵌入应用程序,请查看表单中的CodeProject article that demos a technique for hosting other application's windows

答案 1 :(得分:1)

您可以尝试通过重新定位来完成此操作。请参阅我在MSDN上的帖子,我在其中为WPF描述了这个帖子:Composite "shell" application

WinForms的技术本身也是一样的。在您的应用中有一个主机区域。将顶级窗口的目标应用程序样式更改为WS_CHILD。调用SetParent(),将目标窗口的父级更改为主机区域。

请注意,在Win32中,只有顶级窗口才有菜单。因此,更改为WS_CHILD会删除菜单。

答案 2 :(得分:1)

你可以这样做:

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

    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [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 void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

    [DllImport("user32.dll")]
    static extern IntPtr SetActiveWindow(IntPtr hWnd);


    private const int GWL_STYLE = (-16);
    private const int WS_VISIBLE = 0x10000000;
    private const int WS_MAXIMIZE = 0x01000000;

    private void Form1_Load(object sender, EventArgs e)
    {
        this.SuspendLayout();

        Process notepad = new Process();
        ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
        psi.WindowStyle = ProcessWindowStyle.Normal;
        notepad.StartInfo = psi;

        notepad.Start();

        this.ResumeLayout();

        notepad.WaitForInputIdle(3000);

        IntPtr old = SetParent(notepad.MainWindowHandle, this.Handle);

        SetWindowLong(notepad.MainWindowHandle, GWL_STYLE, WS_VISIBLE + WS_MAXIMIZE);
        MoveWindow(notepad.MainWindowHandle, 100, 100, 400, 400, true);

        SetActiveWindow(notepad.MainWindowHandle);
        SwitchToThisWindow(notepad.MainWindowHandle, true);  }

通过这种方式,您可以在表单中使用记事本应用程序;)

答案 3 :(得分:0)

一般情况下,几乎不可能在您的内部显示任何类型的第三方应用程序。 如果目标应用程序支持控制台界面,我会为此应用程序创建自己的界面,将GUI命令转换为目标应用程序的控制台命令。