【发布时间】:2020-07-22 17:54:50
【问题描述】:
我正在尝试在WPF应用程序中运行一个应用程序,这是我搜索答案后的代码。
[DllImport("user32.dll", EntryPoint = "SetParent", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
private static extern long ShowWindow(IntPtr hWnd, int nCmdShow);
public void ShowWindow(){
System.Windows.Forms.PictureBox childp = new System.Windows.Forms.PictureBox()
{
Height = 1000,
Width = 1000
};
winform.Child = childp;
ProcessStartInfo psi = new ProcessStartInfo("C:\\Windows\\system32\\mspaint.exe")
{
WindowStyle = ProcessWindowStyle.Minimized
};
Process PR = Process.Start(psi);
// true if the associated process has reached an idle state:
PR.WaitForInputIdle();
// loading exe to the wpf window:
SetParent(PR.MainWindowHandle, childp.Handle);
ShowWindow(PR.MainWindowHandle, 3);
}
xaml 代码是:
<TabControl>
<TabItem Header="localhost">
<StackPanel>
<wfi:WindowsFormsHost x:Name="winform"/>
<Button Command = "{Binding ShowWindowCommand}">Show Window<Button/>
</StackPanel>
</TabItem>
<TabItem Header="File1">
</TabItem>
</TabControl>
但是这段代码没有正确运行,mspaint.exe跑出了WPF应用程序,函数ShowWindow()被命令ShowWindowCommand调用并无用,但如果被Click调用并更改@987654326 @ to Thread.sleep ,它有效,但是为什么呢?
【问题讨论】: