此文记录的是检测桌面窗体的小函数。
/*** 桌面窗体工具类库 Austin Liu 刘恒辉 Project Manager and Software Designer E-Mail: lzhdim@163.com Blog: http://lzhdim.cnblogs.com Date: 2024-01-15 15:18:00 说明: 用于判断桌面是否有窗体显示,桌面是否被遮挡的判断。 使用方法: bool _Index = DesktopWindowUtil.IsHaveShowWindow(); bool _Index = DesktopWindowUtil.IsDesktopCovered(); ***/ namespace Lzhdim.LPF.Utility { using System; using System.Collections.Generic; using System.Runtime.InteropServices; /// <summary> /// 桌面窗体工具类 /// </summary> public class DesktopWindowUtil { #region 判断桌面是否显示了窗体 private const int GWL_STYLE = -16; private const int WS_VISIBLE = 262144; /// <summary> /// 判断桌面是否显示了窗体 /// </summary> /// <returns>true 桌面有窗体显示;false 桌面没窗体显示;</returns> public static bool IsHaveShowWindow() { IntPtr hWnd = GetForegroundWindow(); bool isVisible = IsWindowVisible(hWnd); // 检查窗体是否可见 int style = GetWindowLong(hWnd, GWL_STYLE); // 获取窗体样式 bool isNotMinimized = (style & WS_VISIBLE) == WS_VISIBLE; // 检查窗体是否最小化 if (isVisible && isNotMinimized) { return true; } else { return false; } } [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool IsWindowVisible(IntPtr hWnd); #endregion 判断桌面是否显示了窗体 #region 判断桌面是否被遮挡 /// <summary> /// 判断桌面是否被覆盖 /// </summary> /// <returns></returns> public static bool IsDesktopCovered() { List<IntPtr> handles = GetAllWindowHandles(); foreach (IntPtr handle in handles) { bool isVisible = IsWindowVisible(handle); // 检查窗体是否可见 int style = GetWindowLong(handle, GWL_STYLE); // 获取窗体样式 bool isNotMinimized = (style & WS_VISIBLE) == WS_VISIBLE; // 检查窗体是否最小化 //只要有一个窗体为最大化,而且已经显示并且不是最小化,则认为桌面有窗体被遮挡 if (IsZoomed(handle) && isVisible && isNotMinimized) { return true; } } return false; } #region 私有方法 private static List<IntPtr> windowHandles = new List<IntPtr>(); private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); private static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam) { // 将窗口句柄添加到列表中 windowHandles.Add(hWnd); // 继续枚举其他窗口 return true; } [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); private static List<IntPtr> GetAllWindowHandles() { // 清除之前的句柄列表(如果需要) windowHandles.Clear(); // 枚举所有顶级窗口 EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero); // 返回找到的窗口句柄列表 return windowHandles; } [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool IsZoomed(IntPtr hWnd); #endregion 私有方法 #endregion 判断桌面是否被遮挡 } }
![]() |
Austin Liu 刘恒辉
Project Manager and Software Designer E-Mail:lzhdim@163.com Blog:https://lzhdim.cnblogs.com 欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2023-08-27 5个前端开源项目带你在Web上体验Windows
2022-08-27 8、开发工具软件 - 软件技术系列文章
2022-08-27 7、软件复用 - 软件技术系列文章