相关Win32api的学习
SetParent
[DllImport("user32.dll ", EntryPoint = "SetParent")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); //将外部窗体嵌入程序
HWND SetParent(
[in] HWND hWndChild,
[in, optional] HWND hWndNewParent
);


作用:检索其类名和窗口名称与指定字符串匹配的顶级窗口的句柄。此函数不搜索子窗口。此函数不执行区分大小写的搜索。若要从指定的子窗口开始搜索子窗口,请使用 FindWindowEx 函数。





public static class WindowManager
{
public static IntPtr intPtr; //第三方应用窗口的句柄
/// <summary>
/// 调整第三方应用窗体大小
/// </summary>
public static void ResizeWindow()
{
ShowWindow(intPtr, 0); //先将窗口隐藏
ShowWindow(intPtr, 3); //再将窗口最大化,可以让第三方窗口自适应容器的大小
}
/// <summary>
/// 循环查找第三方窗体
/// </summary>
/// <returns></returns>
public static bool FindWindow(string formName)
{
for (int i = 0; i < 100; i++)
{
//按照窗口标题查找Python窗口
IntPtr vHandle = FindWindow(null, formName);
if (vHandle == IntPtr.Zero)
{
Thread.Sleep(100); //每100ms查找一次,直到找到,最多查找10s
continue;
}
else //找到返回True
{
intPtr = vHandle;
return true;
}
}
intPtr = IntPtr.Zero;
return false;
}
/// <summary>
/// 将第三方窗体嵌入到容器内
/// </summary>
/// <param name="hWndNewParent">父容器句柄</param>
/// <param name="windowName">窗体名</param>
public static void SetParent(IntPtr hWndNewParent, string windowName)
{
ShowWindow(intPtr, 0); //先将窗体隐藏,防止出现闪烁
SetParent(intPtr, hWndNewParent); //将第三方窗体嵌入父容器
Thread.Sleep(100); //略加延时
ShowWindow(intPtr, 3); //让第三方窗体在容器中最大化显示
RemoveWindowTitle(intPtr); // 去除窗体标题
}
/// <summary>
/// 去除窗体标题
/// </summary>
/// <param name="vHandle">窗口句柄</param>
public static void RemoveWindowTitle(IntPtr vHandle)
{
long style = GetWindowLong(vHandle, -16);
style &= ~0x00C00000;
SetWindowLong(vHandle, -16, style);
}
#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
}
[DllImport("user32.dll ", EntryPoint = "SetParent")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
这段代码是在C#中使用平台调用(Platform Invoke,或P/Invoke)来调用Windows的user32.dll中的一个函数,名为SetParent。这是一种在.NET中调用本地方法(通常是C或C++编写的)的技术。[DllImport("user32.dll ", EntryPoint = "SetParent")]
:这是一个属性,它告诉.NET运行时你要调用的DLL的名称(在这里是"user32.dll")和函数的入口点(在这里是"SetParent")。private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent)
:这是函数的声明。它告诉.NET运行时函数的签名。在这个例子中,函数名为SetParent,它接受两个IntPtr类型的参数(hWndChild和hWndNewParent),并返回一个IntPtr类型的值。在C#中,extern
关键字用于声明一个方法,该方法在外部实现,通常是在一个DLL中。在该静态类中定义了一个类型为IntPtr
的静态成员intPtr表示第三方应用窗口的句柄。在C#中,IntPtr是一个特殊的数据类型,用于表示指针或句柄。它的大小会根据当前操作系统的位数而变化,32位系统下为4字节,64位系统下为8字节。IntPtr主要用于在托管代码和非托管代码之间传递指针或句柄,以及处理不确定性大小的内存操作。它通常用于与操作系统API进行交互、处理内存分配和操作句柄等场景。IntPtr类型提供了一种安全的方式来处理指针,因为它是托管代码中的数据类型,受到.NET运行时的管理和保护。通过IntPtr,可以在托管代码中安全地表示非托管资源的地址或句柄,而无需担心内存泄漏或其他不安全的操作。使用IntPtr类型时,需要谨慎处理,并遵循.NET平台的内存管理规则,以确保代码的稳定性和安全性。通常情况下,IntPtr主要用于与非托管代码进行交互,处理平台特定的资源或操作系统API,同时尽量避免直接使用指针操作,以减少内存管理和安全性方面的问题。这个静态类还有ResizeWindow
、FindWindow
、SetParent
、RemoveWindowTitle
方法,等后面用到了再做解释。


在这里就会遇到一个问题就是如何确定窗体的标题是什么?

得到了关于这个窗体的一些信息,其中红框部分就是窗体标题,如下所示:找到窗体标题之后,看看WindowManager.FindWindow
方法:

再看看 WindowManager.SetParent
方法:

但是我们发现嵌入的效果不是很好,而且无法随着窗体的变化而变化,需要再做下修改:


总结
以上就是在Winform中嵌入第三方窗体的一次实践,希望对你有所帮助。如果你觉得这篇文章对你有帮助,不妨点个赞支持一下!你的支持是我继续分享知识的动力。如果有任何疑问或需要进一步的帮助,欢迎随时留言。也可以加入微信公众号[DotNet技术匠] 社区,与其他热爱技术的同行一起交流心得,共同成长!作者:mingupupup
出处:cnblogs.com/mingupupu/p/18058334
该文章在 2024/12/26 16:05:42 编辑过