我想在我的 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;