我想在我的 WPF 应用程序中设置 SetParent, 但当我尝试这样做时。不行,我在.net 8上试试 我要设置的程序是calc.exe。
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Process process = Process.Start("calc.exe");
// Find the handle of the external window
IntPtr externalWindowHandle = process.MainWindowHandle;
if (externalWindowHandle != null)
{
// Reparent the external window into the WPF application
SetParent(externalWindowHandle, new WindowInteropHelper(this).Handle);
}
else
{
MessageBox.Show("External window not found.");
}
}
我期待 calc.exe 的视图出现在我的 wpf 视图中
进程的主窗口句柄可能无法立即可用。你应该阻止直到它准备好:
Process process = Process.Start("calc.exe");
// Wait until the process finishes creating the window handle
process.WaitForInputIdle();
// Find the handle of the external window
IntPtr externalWindowHandle = process.MainWindowHandle;