【发布时间】:2021-02-09 16:20:29
【问题描述】:
在我的 WPF 中,我有一个包含 WindowsFormsHost 的网格,它将承载一个 .exe。 我的 .xaml 代码是:
<Window x:Class="PracticeWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PracticeWPF"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="1300">
<Grid x:Name="grid">
<Button x:Name="BtnRun" Content="Run"
HorizontalAlignment="Left" Margin="10,10,0,0"
VerticalAlignment="Top" Click="BtnRun_Click"/>
<WindowsFormsHost x:Name="formshost" Margin="0,39,0,0"
Height="720" Width="1280"/>
</Grid>
</Window>
还有我的cs:
namespace PracticeWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[DllImport("user32.dll")]
public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private void BtnRun_Click(object sender, EventArgs e)
{
Process proc = Process.Start("notepad.exe");
proc.WaitForInputIdle();
while (proc.MainWindowHandle == IntPtr.Zero)
{
Thread.Sleep(100);
proc.Refresh();
}
SetParent(proc.MainWindowHandle, formshost.Handle);
}
}
}
当我使用 WinForm 时一切正常,但当我在 WPF 中使用 WindowsFormsHosting 时,.exe 不可见。 任何帮助将不胜感激。 谢谢。
【问题讨论】:
标签: c# wpf exe windowsformshost