Как запустить приложение.exe в окне WPF
Помогите, пожалуйста, как запустить приложение External .exe в окне WPF.
Ниже кода я могу открыть приложения Notepad.exe и WinWord.exe в окне WPF, но не другие приложения. Когда я пытаюсь открыть другие приложения.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...
}
}
}
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();
// перетащить ваше имя sln;
Там может быть несколько вещей, которые могут вызвать такое поведение, вот две отдельные вещи, с которыми я столкнулся только сейчас:
Когда я пытался использовать vim.exe и в первый раз произошло исключение, что библиотека типов не зарегистрирована, поэтому я зарегистрировался, и после этого VIM.EXE был успешно загружен. Это может быть поведение с вашим приложением.
Когда я попытался загрузить Eclipse, исключений не было, но Eclipse.exe был загружен за пределами окна WPF. Заглядывая в Spy++, я обнаружил сообщение WM_ACTIVATEAPP, которое заставило Windows открыться за пределами окон WPF, и здесь описывается, почему:
http://support.microsoft.com/kb/89563
Поэтому зависит от того, какое приложение вы пытаетесь открыть в своем WPF, не каждое приложение будет открываться, поскольку существуют определенные ограничения для конкретного приложения.