QT写的exe嵌入到wpf中
1.cs代码
public partial class MainWindow : Window
{
//声明调用user32.dll中的函数
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
//定义变量
private IntPtr prsmwh;//外部EXE文件运行句柄
private Process app;//外部exe文件对象
public MainWindow()
{
this.InitializeComponent();
}
private void flash_Click(object sender, System.Windows.RoutedEventArgs e)
{
//获取当前窗口句柄
IntPtr handle = new WindowInteropHelper(this).Handle;
string path=System.Environment.CurrentDirectory + @"\positioning\CTHME.exe";
app = Process.Start(path);
prsmwh = app.MainWindowHandle;
while ( prsmwh ==IntPtr.Zero)
{
prsmwh = app.MainWindowHandle;
}
//设置父窗口
SetParent(prsmwh,handle);
ShowWindowAsync(prsmwh,3);//子窗口最大化
}
private void Window_Closed(object sender, System.EventArgs e)
{
if (app.CloseMainWindow()){
app.Kill();
app.Close();
}
}
}
}
2.xaml代码
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfEXE.MainWindow"
x:Name="Window"
Title="test"
Width="640" Height="480" WindowStartupLocation="CenterScreen" Closed="Window_Closed" WindowState="Maximized">
<Grid x:Name="LayoutRoot">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Blue" Offset="0.313"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Button x:Name="flash" Content="测试" Height="25" VerticalAlignment="Top" Cursor="Hand" FontSize="14.667" Margin="0,78,0,0" Click="flash_Click" HorizontalAlignment="Left" Width="104"/>
</Grid>
</Window>