枚举当前打开的程序窗口

枚举当前打开的程序窗口  作者 taishan_sword
<script language=VBScript> document.title="枚举当前打开的程序窗口 "&document.title </script>
原作者姓名 taishan_sword

正文
一、原理
     其原理是利用GetWindowLong(hWnd,GWL_STYLE)& WS_VISIBLE对打开的窗口进行查找,并
将窗口的句柄存放在一个HWND的数组中,结合EnumWindows进行窗口的枚举,从而得到响应
的窗口名称;
二、主要函数介绍:
   BOOL EnumWindows(
  WNDENUMPROC lpEnumFunc,  // callback function
  LPARAM lParam            // application-defined value
);
LONG GetWindowLong(
  HWND hWnd,  // handle to window
  int nIndex  // offset of value to retrieve
);
详细资料请查看MSDN;
在此我定义了一个过程EnumWindowsProc,通过GetWindowLong来完成对打开程序窗口的枚举;通过ListBox实现程序的列举;并将每次调用的枚举结果存放在/info目录下当天日期为文件名称的.txt文件里;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Windows API 中的 EnumWindows 和 EnumChildWindows 函数来枚举所有窗口及其子窗口。以下是一个 C# 示例代码: ```csharp using System; using System.Collections.Generic; using System.Runtime.InteropServices; class Program { delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll")] static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); [DllImport("user32.dll")] static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsProc enumProc, IntPtr lParam); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount); [DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr hWnd); static void Main(string[] args) { List<IntPtr> windows = new List<IntPtr>(); EnumWindows(delegate(IntPtr hWnd, IntPtr lParam) { if (IsWindowVisible(hWnd)) { windows.Add(hWnd); EnumChildWindows(hWnd, delegate(IntPtr hWndChild, IntPtr lParamChild) { if (IsWindowVisible(hWndChild)) { windows.Add(hWndChild); } return true; }, IntPtr.Zero); } return true; }, IntPtr.Zero); foreach (IntPtr hWnd in windows) { int length = GetWindowTextLength(hWnd); if (length > 0) { StringBuilder sb = new StringBuilder(length + 1); GetWindowText(hWnd, sb, sb.Capacity); Console.WriteLine("Window title: " + sb.ToString()); } } } } ``` 这个示例代码使用 EnumWindows 函数枚举所有顶级窗口,然后使用 EnumChildWindows 函数枚举每个顶级窗口的子窗口。然后,它使用 GetWindowText 函数获取每个窗口的标题,并将其输出到控制台。你可以根据需要修改代码以执行其他操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值