主要是获取Alt+Tab中展示的窗口

原理主要是获取窗口的样式来判断是否会在Alt+Tab中显示

具体代码如下

/// <summary>
/// Alt+Tab 应用
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
public static bool IsAltTabWindow(IntPtr hWnd)
{
// The window must be visible
if (!IsWindowVisible(hWnd))
return false; // The window must be a root owner
if (GetAncestor(hWnd, GA_ROOTOWNER) != hWnd)
return false; // The window must not be cloaked by the shell
DwmGetWindowAttribute(hWnd, DwmWindowAttribute.DWMWA_CLOAKED, out int cloaked, sizeof(uint));
if (cloaked == DWM_CLOAKED_SHELL)
return false; // The window must not have the extended style WS_EX_TOOLWINDOW
int style = Utils.Win32Api.GetWindowLong(hWnd, GWL_EXSTYLE);
if ((style & WS_EX_TOOLWINDOW) != 0)
return false; return true;
} [DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll")]
public static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);
[DllImport("dwmapi.dll")]
public static extern int DwmGetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, out int attrValue, int cbAttribute); const uint DWM_CLOAKED_SHELL = 0x00000002;
const uint GA_ROOTOWNER = 3; const uint WS_EX_TOPMOST = 0x00000008; [Flags]
public enum DwmWindowAttribute : uint
{
DWMWA_NCRENDERING_ENABLED = 1,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
DWMWA_CLOAKED,
DWMWA_FREEZE_REPRESENTATION,
DWMWA_LAST
}
/// <summary>
/// 获取窗体句柄
/// </summary>
/// <param name="hwnd"></param>
/// <param name="nIndex"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hwnd, int nIndex); const int GWL_EXSTYLE = -20;//得到扩展的窗口风格
public const int WS_EX_TOOLWINDOW = 0x00000080;

  以上方式对于全屏的UWP窗口时无法获取得到的,因此需要引入以下方式获取全屏UWP窗口

/// <summary>
/// 全屏的UWP应用
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
public static bool IsFullScreenUwpWindows(IntPtr hWnd)
{
// Get the extended style of the window
var style = GetWindowLong(hWnd, GWL_EXSTYLE); // The window must have the extended style WS_EX_TOPMOST
if ((style & WS_EX_TOPMOST) == 0)
return false; // The window must not have the extended style WS_EX_NOACTIVATE
if ((style & WS_EX_NOACTIVATE) != 0)
return false; // The window must not have the extended style WS_EX_TOOLWINDOW
if ((style & WS_EX_TOOLWINDOW) != 0)
return false; return true;
} public const int WS_EX_NOACTIVATE = 0x08000000;

然后通过枚举窗口的方式就可以获取到所有窗口了

/// <summary>
/// 枚举窗体的回调
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public delegate bool EnumWindowsCallBack(IntPtr hwnd, IntPtr lParam);
/// <summary>
/// 枚举出窗体
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
[DllImport("user32")]
public static extern int EnumWindows(EnumWindowsCallBack x, IntPtr y); /// <summary>
/// 枚举子窗口
/// </summary>
/// <param name="hWndParent"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
[DllImport("user32")]
public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsCallBack x, IntPtr y);
List<KeyValuePair<IntPtr, string>> handles = new List<KeyValuePair<IntPtr, string>>();
EnumWindows((hWnd, lPam) =>
{
if (!IsAltTabWindow(hWnd)) return true;//继续枚举
var title = GetWindowTitle(hWnd);
handles.Add(new KeyValuePair<IntPtr, string>(hWnd, title));
return true;//继续枚举
}, IntPtr.Zero); EnumChildWindows(GetDesktopWindow(), (hWnd, lPam) =>
{
if (handles.Any(kv => kv.Key == hWnd)||!IsFullScreenUwpWindows(hWnd)) return true;//继续枚举
var title = GetWindowTitle(hWnd);
handles.Add(new KeyValuePair<IntPtr, string>(hWnd, title));
return true;//继续枚举
}, IntPtr.Zero); [DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
/// <summary>
/// 获取窗体的名称
/// </summary>
/// <param name="hWnd"></param>
/// <param name="lpString"></param>
/// <param name="nMaxCount"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int GetWindowTextW(IntPtr hWnd, IntPtr lpString, int nMaxCount); /// <summary>
/// 默认获取字符串的长度
/// </summary>
private const int NumChars = 256;
public static string GetWindowTitle(IntPtr hwnd)
{
IntPtr intPtr = Marshal.AllocHGlobal(NumChars);
Utils.Win32Api.GetWindowTextW(hwnd, intPtr, 100);
var s = Marshal.PtrToStringUni(intPtr);
Marshal.FreeHGlobal(intPtr);
return s;
}

以上代码需要做调整才能运行起来,有空我补上完整代码

参考:https://stackoverflow.com/questions/72069771/show-a-list-of-all-alttab-windows-even-full-screen-uwp-windows-and-retrieve

获取Windows正在运行的窗口进程的更多相关文章

  1. [Android Pro] 获取手机已经安装的应用 和 获取当前正在运行的所有进程(一个uid对应多个pid)

    1: 获取PackageManager 获取全部静态已安装的应用: PackageManager pm = getPackageManager(); List<PackageInfo> i ...

  2. C#获取运行程序的进程ID

    C#获取运行程序的进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int G ...

  3. 【linux】linux下准确查询正在tomcat下运行的java进程。准确获取正在运行的java进程的PID

    准确获取定位到tomcat下正在运行的java进程的PID命令: ps -ef|grep java | grep catalina | awk '{print $2}' 准确定位到tomcat下正在运 ...

  4. 电脑出现“损坏的图像”窗口提示dll没有被指定在Windows上运行如何解决

    电脑中出现了无法运行应用程序的情况,弹出一个“***.exe - 损坏的图像”的窗口,上面提示“***.dll没有被指定在Windows上运行……”,如果我们遇到这样的问题,应该要如何解决呢? 1.我 ...

  5. 黄聪:使用srvany.exe将任何程序作为Windows服务运行

    srvany.exe是什么? srvany.exe是Microsoft Windows Resource Kits工具集的一个实用的小工具,用于将任何EXE程序作为Windows服务运行.也就是说sr ...

  6. 使用srvany.exe将任何程序作为Windows服务运行

    使用srvany.exe将任何程序作为Windows服务运行 2011 年 3 月 7 日 !本文可能 超过1年没有更新,今后内容也许不会被维护或者支持,部分内容可能具有时效性,涉及技术细节或者软件使 ...

  7. .NET Worker Service 作为 Windows 服务运行及优雅退出改进

    上一篇文章我们了解了如何为 Worker Service 添加 Serilog 日志记录,今天我接着介绍一下如何将 Worker Service 作为 Windows 服务运行. 我曾经在前面一篇文章 ...

  8. 通过PowerShell获取Windows系统密码Hash

    当你拿到了系统控制权之后如何才能更长的时间内控制已经拿到这台机器呢?作为白帽子,已经在对手防线上撕开一个口子,如果你需要进一步扩大战果,你首先需要做的就是潜伏下来,收集更多的信息便于你判断,便于有更大 ...

  9. windows使用命令行杀进程

    在windows有时使用任务管理器杀进程,一直杀不掉: 这个时候,可以使用命令行: 先使用tasklist 命令查看当前系统中的进程列表,然后针对你要杀的进程使用taskkill命令 如要杀nginx ...

随机推荐

  1. 开始讨论离散型随机变量吧!《考研概率论学习之我见》 -by zobol

    上一文中,笔者给出了随机变量的基本定义:一个可测映射,从结果空间到实数集,我们的目的是为了引入函数这个数学工具到考研概率论中,但是我们在现实中面对的一些事情结果,映射而成的随机变量和其对应的概率值,并 ...

  2. JavaScript 防盗链的原理以及破解方法

    先说说防盗链的原理,http 协议中,如果从一个网页跳到另一个网页,http 头字段里面会带个 Referer.这里的Referer是由于历史原因导致了拼写错误 后来也就一直沿用.图片服务器通过检测 ...

  3. 【前端面试】(三)JavaScript相等(==)和全等(===)运算符的区别

    视频链接: JavaScript相等()和全等(=)运算符的区别 - Web前端工程师面试题讲解 参考链接: JavaScript == 与 === 区别 区别 对于string.number 等基础 ...

  4. input标签的事件之oninput事件

    最近在写前端的时候,用到了oninput事件.(其中也涉及了onclick) 问题:鼠标点击数字和运算符的时候,文本框里的内容到达一定长度时,会出现无法继续往后面跟随光标的问题. 解决:见下面代码 这 ...

  5. windows如何结束某个端口的进程

    netstat -ano | findstr 端口号 (查询端口号被哪个进程占用) tasklist | findstr 进程PID (根据PID找到进程名称) taskkill -PID 进程PID ...

  6. NC14326 Rails

    NC14326 Rails 题目 题目描述 There is a famous railway station in PopPush City. Country there is incredibly ...

  7. Idea Maven调试properties 找不到报错

    问题:maven执行package命令打包时,src/main/java路径下的properties文件偶尔丢失 解决方式:pom.xml中加入resources配置 <build> &l ...

  8. Solution -「构造」专练

    记录全思路过程和正解分析.全思路过程很 navie,不过很下饭不是嘛.会持续更新的(应该). 「CF1521E」Nastia and a Beautiful Matrix Thought. 要把所有数 ...

  9. JavaScript进阶内容——jQuery

    JavaScript进阶内容--jQuery 我们在前面的文章中已经掌握了JavaScript的全部内容,现在让我们了解一下JavaScript库 这篇文章主要是为了为大家大致讲解JavaScript ...

  10. HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界

    从单声道.立体声.环绕声发展到三维声,音频回放技术的迭代演进是为了还原真实世界的声音.其中,三维声技术使用信号处理的方法对到达两耳的声音信号进行模拟,将声场还原为三维空间,更接近真实世界.凭借这个技术 ...