问题
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...
}
}
}
回答1:
There could be a few things which could cause this behavior, here are two separate things I encountered just now:
When I tried to use vim.exe and very first time an exception occurred that Type Library is not registered so I registered and after that VIM.EXE was loaded successfully. This could be a behavior with your application.
When I tried to Load Eclipse and there was no exception but Eclipse.exe was loaded outside the WPF window. Looking in the Spy++ I found WM_ACTIVATEAPP message which caused the Windows to open outside WPF windows which is describe here why:
http://support.microsoft.com/kb/89563
So depend on what kind of application you are trying to open in your WPF, application not each and every application will open as there are application specific certain restrictions.
回答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 is your sln name;
来源:https://stackoverflow.com/questions/10777296/how-to-launch-external-exe-application-in-wpf-window