如何在WPF中实现软件内嵌效果

embedded/2025/2/12 23:57:28/

1.创建Process进程,设置运行程序文件路径

Process proc = new Process();
proc.StartInfo.FileName = @"C:\Users\hdevelop.exe";
proc.Start();
proc.WaitForInputIdle();

2.根据创建的进程获取窗口句柄

IntPtr hWnd = proc.MainWindowHandle;

3.开启线程,当获取的句柄不为空过的时候,将获取到的窗口与自己创建的窗体进行绑定

if (hWnd != IntPtr.Zero)
{SetWindow.intPtr = hWnd;//这里是WPF的写法,Winform把this.Dispatcher.Invoke改为this.Invoke即可this.Dispatcher.Invoke(new Action(() =>{SetWindow.SetParent(tempPanel.Handle, "TempWindow", this);  //设置父容器}));
}
else
{System.Windows.MessageBox.Show("未能查找到窗体");
}

嵌入容器的方法以及需要调用的API为:

/// <summary>
/// 将第三方窗体嵌入到容器内
/// </summary>
/// <param name="hWndNewParent">父容器句柄</param>
/// <param name="windowName">窗体名</param>
public static void SetParent(IntPtr hWndNewParent, string windowName, MainWindow window)
{ShowWindow(intPtr, 0);                 //先将窗体隐藏,防止出现闪烁SetParent(intPtr, hWndNewParent);      //将第三方窗体嵌入父容器                    Thread.Sleep(100);                      //略加延时ShowWindow(intPtr, 3);                 //让第三方窗体在容器中最大化显示RemoveWindowTitle(intPtr);// 去除窗体标题
}#region API 需要using System.Runtime.InteropServices;[DllImport("user32.dll ", EntryPoint = "SetParent")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);   //将外部窗体嵌入程序[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);      //按照窗体类名或窗体标题查找窗体[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);                  //设置窗体属性[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern long GetWindowLong(IntPtr hWnd, int nIndex);#endregion/// <summary>
/// 去除窗体标题
/// </summary>
/// <param name="vHandle">窗口句柄</param>
public static void RemoveWindowTitle(IntPtr vHandle)
{long style = GetWindowLong(vHandle, -16);style &= ~0x00C00000;SetWindowLong(vHandle, -16, style);
}

以上就已经将外部窗体嵌入至现有窗体,如果需要调整内嵌尺寸则可以调用方法

 /// <summary>/// 调整第三方应用窗体大小,intPtr为获取到的程序窗口句柄/// </summary>public static void ResizeWindow(){ShowWindow(intPtr, 0);  //先将窗口隐藏ShowWindow(intPtr, 3);  //再将窗口最大化,可以让第三方窗口自适应容器的大小}


http://www.ppmy.cn/embedded/161719.html

相关文章

antd-react日期组件disabledDate不可选择的日期 (置灰)属性

需求&#xff1a;原定结项时间表单里回显为2025-02-06&#xff0c;延期时间的选择范围要设置从2025-02-07开始选择包括2.7号的; 2.7号之前的置灰&#xff0c;不可选择 PC端部分代码&#xff1a; // react的函数组件写法 const disabledDate function (current) {console.log(c…

CSS 小技巧 —— CSS 实现 Tooltip 功能-鼠标 hover 之后出现弹层

CSS 小技巧 —— CSS 实现 Tooltip 功能-鼠标 hover 之后出现弹层 1. 两个元素实现 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><title>纯 CSS 实现 Tooltip 功能-鼠标 hover 之后出现弹层</titl…

npm运行Vue项目报错 error:0308010c:digital envelope routines::unsupported

大家好&#xff0c;我是 程序员码递夫。 问题 VSCode 运行Vue项目&#xff0c;提示错误&#xff1a; building 2/2 modules 0 activeError: error:0308010c:digital envelope routines::unsupported 解决方法 原因是 npm 高版本(大于17)&#xff0c;对ssl的处理做了改进&…

Windows命令行学习(和Linux命令比对记忆

本文侧重于记忆Windows简单命令&#xff0c;所以相关Linux命令演示较少&#xff0c;学习Linux命令详见Linux系统专栏&#xff1b;本文示例版本为Windows 10与Linux 7。 目录 一、文件与目录操作 1、列出当前目录下文件及子目录信息 Windows&#xff1a;dir Linux&#xff1…

SpringBoot单机模式,能否支持一万用户请求并发?

Spring Boot 单机模式能否支持一万用户请求并发&#xff0c;取决于多个因素&#xff1a; 硬件配置&#xff1a;CPU、内存、磁盘I/O和网络带宽是关键。高性能硬件能显著提升并发处理能力。 应用复杂度&#xff1a;业务逻辑复杂度和数据库操作频率会影响性能。复杂的业务逻辑和高…

Docker使用指南与Dockerfile文件详解:从入门到实战

Docker使用指南与Dockerfile文件详解:从入门到实战 文章目录 **Docker使用指南与Dockerfile文件详解:从入门到实战****引言****第一部分:Docker 核心概念速览****1. Docker 基础架构****2. Docker 核心命令****第二部分:Dockerfile 文件深度解析****1. Dockerfile 是什么?…

<论文>DeepSeek-R1:通过强化学习激励大语言模型的推理能力(深度思考)

一、摘要 本文跟大家来一起阅读DeepSeek团队发表于2025年1月的一篇论文《DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning | Papers With Code》&#xff0c;新鲜的DeepSeek-R1推理模型&#xff0c;作者规模属实庞大。如果你正在使用Deep…

运用 LangChain 编排任务处理流水线,实现多轮对话场景

在LangChain中&#xff0c;任务处理流水线和多轮对话的场景可以通过LangChain的链式结构&#xff08;Chain&#xff09;和会话记忆&#xff08;Conversation Memory&#xff09;来实现。LangChain框架旨在简化与大语言模型&#xff08;如 GPT-3&#xff09;进行交互的流程&…