0

Please help me, how to Launch External .exe application in the WPF Window.

Below code, I am able to open Notepad.exe and WinWord.exe applications in the WPF window but not other applications.. when i try to open other .exe applications it is opening in separate window.

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...
        }
      }



}
4

2 回答 2

0

可能有一些事情可能导致这种行为,这是我刚才遇到的两件不同的事情:

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

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

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

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

于 2012-05-29T03:26:05.013 回答
0

字符串 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 名称;

于 2014-04-02T07:50:07.197 回答