【问题标题】:How to Launch External .exe application in WPF Window如何在 WPF 窗口中启动外部 .exe 应用程序
【发布时间】:2012-05-27 21:06:18
【问题描述】:

请帮助我,如何在 WPF 窗口中启动外部 .exe 应用程序。

下面的代码,我可以在 WPF 窗口中打开 Notepad.exe 和 WinWord.exe 应用程序,但不能打开其他应用程序。当我尝试打开其他 .exe 应用程序时,它会在单独的窗口中打开。

public partial class Window1 : Window
{
    public IntPtr MainWindowHandle { get; set; }


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


    //[DllImport("user32.dll", SetLastError = true)]
    //private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    public Window1()
    {
        InitializeComponent();

        try
        {
            //External exe inside WPF Window 
            System.Windows.Forms.Panel _pnlSched = new System.Windows.Forms.Panel();

            WindowsFormsHost windowsFormsHost1 = new WindowsFormsHost();

            windowsFormsHost1.Child = _pnlSched;

            _Grid.Children.Add(windowsFormsHost1);

            //_Grid.Children.Add(_pnlSched);

            ProcessStartInfo psi = new ProcessStartInfo(@"C:\Program Files\Atwin\Atwin2k2.exe");

            psi.WindowStyle = ProcessWindowStyle.Normal;

            Process PR = Process.Start(psi);

            PR.WaitForInputIdle(); // true if the associated process has reached an idle state.

            System.Threading.Thread.Sleep(3000);

            IntPtr hwd = PR.MainWindowHandle;
            SetParent(PR.MainWindowHandle, _pnlSched.Handle);  // loading exe to the wpf window.

        }
        catch (Exception ex)
        { 
            //Nothing...
        }
      }



}

【问题讨论】:

    标签: wpf winforms windowsformshost windowsformsintegration


    【解决方案1】:

    可能有几件事会导致这种行为,以下是我刚刚遇到的两件事:

    当我尝试使用 vim.exe 并且第一次发生未注册类型库的异常时,所以我注册并成功加载了 VIM.EXE。这可能是您的应用程序的一种行为。

    当我尝试加载 Eclipse 并且没有异常但 Eclipse.exe 在 WPF 窗口之外加载时。查看 Spy++ 我发现 WM_ACTIVATEAPP 消息导致 Windows 在 WPF 窗口之外打开,这里描述了原因:

    http://support.microsoft.com/kb/89563

    因此,取决于您尝试在 WPF 中打开的应用程序类型,并非每个应用程序都会打开,因为存在特定于应用程序的某些限制。

    【讨论】:

    • 感谢您的回复...我试图在 WPF 窗口中打开的应用程序是在大型机中开发的。
    【解决方案2】:

    string strReportPath = System.IO.Directory.GetCurrentDirectory();

            if (strReportPath.Substring(strReportPath.Length - 9) == "bin\\Debug")
            {
                strReportPath = strReportPath.Substring(0, strReportPath.Length - 10);
            }
    
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(strReportPath + @"\bin\Debug\drag.exe");
            p.Start();
    

    //drag 是你的 sln 名称;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-29
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多