WPF 使用SetParent嵌套窗口
有点类似与Winform的MDI窗口。
使用函数为SetParent和MoveWindow(经常配合)。
[DllImport("user32.dll", SetLastError = true)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("user32.dll", EntryPoint = "SetParent")] public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
给个小例子,例如嵌套TIM的聊天窗口
其中window1 就是新建的窗口 里面什么都没有写,默认
public partial class MainWindow : Window { [DllImport("user32.dll", SetLastError = true)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("user32.dll", EntryPoint = "SetParent")] public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32", EntryPoint = "GetDesktopWindow")] public static extern IntPtr IntPtrGetDesktopWindow(); public MainWindow() { InitializeComponent(); } int baseSize =700; bool IsOpen = false; int Num = 1; Window1 window; private void MainWindow_Closed(object sender, EventArgs e) { // var childhwnd = FindWindow("TXGuiFoundation", "唐大人的 iPhone"); var d = SetParent(childhwnd, IntPtrGetDesktopWindow()); MoveWindow(childhwnd, 0, 0, baseSize + Num+10, baseSize + Num+10, true); } IntPtr childhwnd; private void MainWindow_Loaded(object sender, RoutedEventArgs e) { IntPtr hwnd = new WindowInteropHelper(window).Handle; childhwnd = FindWindow("TXGuiFoundation", "唐大人的 iPhone"); //(IntPtr)Convert.ToInt32("000307BA", 16); var d = SetParent(childhwnd, hwnd); MoveWindow(childhwnd, 0, 0, 600, 600, true); } private void Button_Click(object sender, RoutedEventArgs e) { if (!IsOpen) { window = new Window1(); window.Loaded += MainWindow_Loaded; window.Closed += MainWindow_Closed; window.Show(); IsOpen = true; Num++; return; } Num++; window.Close(); IsOpen = false; } }
截图
SetParent的问题:
1 使用API后,子窗口在父窗口中不显示但是可以显示鼠标的拖拉动作(窗口调整)
2 使用API后,子窗口在父窗口中不再具有焦点,也不可以点击,看起来和图片差不多
3 使用API后,子窗口在原桌面留下空白且可以在空白操控父窗口内的子窗口而在父窗口内则是不可以操作子窗口(针对firefox浏览器[官方发行版])
相对来说嵌套的问题不少,可以说有很多问题。 本文中的代码是可以解决问题1(就是修改窗口的大小,嵌套时窗口大小要大于原先,释放时也要大于嵌套时,如果对释放和嵌套时对大小有要求可以多次修改窗口大小)。其他问题则是需要考虑windows消息机制(个人看法)了。
至于如何释放子窗口则是将子窗口的父窗口重新设置回去就好了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现