如何在.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.