English 中文(简体)
如何在 WPF 窗口中启动外部.exe 应用程序
原标题:How to Launch External .exe application in WPF Window

请帮助我,如何在 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 FilesAtwinAtwin2k2.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...
        }
      }



}
问题回答

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

当我试图使用 vim.exe 并且第一次出现一个例外, 即类型库没有注册, 所以我注册了, 并在 VIM. EXE 成功装入后成功。 这可能会是您应用程序的行为 。

当我试图装入日光片时,没有例外,但是在 WPF 窗口外装入了 Eclipse.exe 。 在 Spy++ 中, 我找到了 WM_ ACTIVATEAPP 信息, 导致 Windows 在 WPF 窗口外打开了 WPF 窗口, 此处描述原因 :

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

取决于您在 WPF 中试图打开的应用程序类型, 应用不是每个应用程序, 每一个应用程序都会打开, 因为有特定的应用限制 。

字符串报告帕特 = System.IO. indenty. GetCentry ();

        if (strReportPath.Substring(strReportPath.Length - 9) == "bin\Debug")
        {
            strReportPath = strReportPath.Substring(0, strReportPath.Length - 10);
        }

        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(strReportPath + @"inDebugdrag.exe");
        p.Start();

折叠是您的 Sln 名称;





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签