如何在 .NET 8 上的 WPF 中设置 SetParent?

问题描述 投票:0回答:1

我想在我的 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 视图中。

c# wpf .net-8.0 setparent
1个回答
0
投票

进程的主窗口句柄可能无法立即可用。你应该阻止直到它准备好:

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;
© www.soinside.com 2019 - 2024. All rights reserved.