当前位置: 首页 > news >正文

WPF内嵌其他进程的窗口

WPF内嵌其他进程窗口的常见方法有 HwndHost + SetParentWindowsFormsHost + WinForms Panel + SetParent

推荐使用自定义HwndHost

两者的对比区别
在这里插入图片描述
示例代码

public class MyWndHost : HwndHost
{const int WS_CHILD = 0x40000000;const int WS_VISIBLE = 0x10000000;const int WS_CLIPCHILDREN = 0x02000000;const int WS_CLIPSIBLINGS = 0x04000000;[DllImport("user32.dll")]internal static extern IntPtr CreateWindowEx(int exStyle, string className, string windowName, int style, int x, int y, int width, int height, IntPtr hwndParent, IntPtr hMenu, IntPtr hInstance, IntPtr pvParam);[DllImport("user32.dll")]internal static extern bool DestroyWindow(IntPtr hwnd);public static readonly DependencyProperty HandleProperty = DependencyProperty.Register("Handle", typeof(IntPtr), typeof(MyWndHost), new PropertyMetadata(IntPtr.Zero));public new IntPtr Handle{get => (IntPtr)GetValue(HandleProperty);set => SetValue(HandleProperty, value);}protected override HandleRef BuildWindowCore(HandleRef hwndParent){Handle = CreateWindowEx(0, "static", "",WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,0, 0,(int)Width, (int)Height,hwndParent.Handle,IntPtr.Zero,IntPtr.Zero,IntPtr.Zero);return new HandleRef(this, Handle);}protected override void DestroyWindowCore(HandleRef hwnd){DestroyWindow(hwnd.Handle);}
}
<Grid><!--WindowsFormsHost--><Grid Margin="12,0,0,0" x:Name="web2Layout" Background="Black"><wfi:WindowsFormsHost Name="windowsFormsHost" ><wf:Panel x:Name="hostPanel"/></wfi:WindowsFormsHost></Grid><!--自定义HwndHost--><ui:MyWndHost x:Name="MyWnd"/>
</Grid>
 public partial class WebView : System.Windows.Controls.UserControl{private IntPtr m_hWndChild = IntPtr.Zero;private Process _electronProcess;[DllImport("user32.dll", SetLastError = true)]private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);[DllImport("user32.dll")]private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);[DllImport("user32.dll")]private static extern uint GetWindowLong(IntPtr hWnd, int nIndex);[DllImport("user32.dll")]private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);private const int GWL_STYLE = -16;private const uint WS_CHILD = 0x40000000;private const uint WS_BORDER = 0x00800000;public WebView(){InitializeComponent();Loaded += Web2Container_Loaded;SizeChanged += Web2Container_SizeChanged;}private void Web2Container_Loaded(object sender, RoutedEventArgs e){StartElectronApp();}private void StartElectronApp(){var path = AppDomain.CurrentDomain.BaseDirectory + @"Exteral\MyElectronApp.exe";_electronProcess = new Process{StartInfo ={FileName = path,UseShellExecute = false}};_electronProcess.Start();Task.Run(() =>{while (_electronProcess.MainWindowHandle == IntPtr.Zero && !_electronProcess.HasExited){Thread.Sleep(100);_electronProcess.Refresh();}if (!_electronProcess.HasExited) ToSetWndParent(_electronProcess.MainWindowHandle);});}private async void ToSetWndParent(IntPtr wnd){await Dispatcher.InvokeAsync(() =>{if (this.FindName("MyWnd") is MyWndHost wfh && wfh.Handle != IntPtr.Zero){m_hWndChild = wnd;SetParent(m_hWndChild, wfh.Handle);uint style = GetWindowLong(m_hWndChild, GWL_STYLE);style = (style & ~WS_BORDER) | WS_CHILD;SetWindowLong(m_hWndChild, GWL_STYLE, style);AdjustChildWindowSize();}});}private void Web2Container_SizeChanged(object sender, SizeChangedEventArgs e){AdjustChildWindowSize();}private void AdjustChildWindowSize(){if (m_hWndChild != IntPtr.Zero){var dpiScale = GetDpiFromVisual(this);MoveWindow(m_hWndChild,0,0,(int)(ActualWidth * dpiScale),(int)(ActualHeight * dpiScale),true);}}private double GetDpiFromVisual(System.Windows.Media.Visual visual){var source = PresentationSource.FromVisual(visual);double dpiX = 96.0;if (source?.CompositionTarget != null){dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;}return dpiX / 96.0;}}

效果示例
在这里插入图片描述

相关文章:

  • 深入解析WPF中的3D图形编程:材质与光照
  • PostgreSQL技术大讲堂 - 第89讲:重讲数据库完全恢复
  • zst-2001 历年真题 设计模式
  • 227. 基本计算器 II
  • Java 原生实现代码沙箱(OJ判题系统第1期)——设计思路、实现步骤、代码实现
  • 线段树:数据结构中的超级英雄
  • 检查当前 Docker 使用的 默认运行时(default runtime)方法
  • LeetCode-双指针-盛最多水的容器
  • 部署Superset BI(四)连接sql server数据库
  • MSF 生成不同的木马 msfvenom 框架命令
  • uniapp跨平台开发HarmonyOS NEXT应用初体验
  • 纯净IP,跨境账号稳定的底层逻辑
  • git做commit信息时的校验
  • hz2新建Keyword页面
  • Spring 必会之微服务篇(1)
  • python实现点餐系统
  • gitlab相关面试题及答案
  • 学习笔记:黑马程序员JavaWeb开发教程(2025.3.31)
  • 用Python监控金价并实现自动提醒!附完整源码
  • 软件测试——用例篇(2)
  • 上海消防全面推行“检查码”,会同相关部门推行“综合查一次”
  • 竞彩湃|霍芬海姆看到保级曙光,AC米兰专注于意大利杯
  • 安徽亳州涡阳县司法局党组书记刘兴连落马
  • 体坛联播|曼联热刺会师欧联杯决赛,多哈世乒赛首日赛程出炉
  • 中华人民共和国和俄罗斯联邦在纪念中国人民抗日战争、苏联伟大卫国战争胜利和联合国成立80周年之际关于进一步深化中俄新时代全面战略协作伙伴关系的联合声明
  • 阿森纳被打得毫无脾气,回天无力的阿尔特塔只剩嘴硬