从 C++ 中的 EnumWindows 生成的过滤/解析列表 [英] Filtering/Parsing list produced from EnumWindows in C++
问题描述
我正在使用以下代码来获取在我的机器上运行的窗口列表
I am using the following code to get a list of windows running on my machine
#include <iostream>
#include <windows.h>
using namespace std;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
TCHAR buffer[512];
SendMessage(hwnd, WM_GETTEXT, 512, (LPARAM)(void*)buffer);
wcout << buffer << endl;
return TRUE;
}
int main()
{
EnumWindows(EnumWindowsProc, NULL);
return 0;
}
我想得到一个通常被称为窗口的列表——我这么说是因为在运行上面的代码时,我得到了大约 40 个条目的列表,其中大部分不是我所说的窗口.
I want to get a list of what is normally refered to as a Window - I say this because when running the above code I get a list of around 40 entries, most of these are not what I would call windows.
这是在我的机器上运行上述脚本产生的输出的摘录,在 5 个条目中只有 Microsoft Visual Studio 是一个窗口
Here is an excerpt of the output produced by running the above script on my machine, out of the 5 entries only Microsoft Visual Studio is a Window
...
Task Switching
Microsoft Visual Studio
CiceroUIWndFrame
Battery Meter
Network Flyout
...
我该如何过滤/解析这些数据,因为没有任何东西可以用作标识符.
How can I go about filtering/parsing this data, as there is not anything to use as an identifier.
推荐答案
我会使用 EnumDesktopWindows
枚举桌面上的所有顶级窗口;您甚至可以使用 IsWindowsVisible
枚举过程中的 API,用于过滤掉不可见的窗口.
I would use EnumDesktopWindows
to enumerate all top-level windows in your desktop; you may even use the IsWindowsVisible
API during the enumeration process, to filter non-visible windows out.
这个可编译的 C++ 代码对我来说很好用(请注意,这里我展示了如何将一些附加信息传递给枚举过程,在这种情况下使用指向 vector
的指针,其中存储窗口标题供以后处理):
This compilable C++ code works fine for me (note that here I showed how to pass some additional info to the enumeration proc, in this case using a pointer to a vector<wstring>
, in which the window titles are stored for later processing):
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
using std::vector;
using std::wcout;
using std::wstring;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
if (!IsWindowVisible(hwnd))
{
return TRUE;
}
wchar_t titleBuf[512];
if (GetWindowText(hwnd, titleBuf, _countof(titleBuf)) > 0)
{
auto pTitles = reinterpret_cast<vector<wstring>*>(lParam);
pTitles->push_back(titleBuf);
}
return TRUE;
}
int main()
{
vector<wstring> titles;
EnumDesktopWindows(nullptr, EnumWindowsProc, reinterpret_cast<LPARAM>(&titles));
for (const auto& s : titles)
{
wcout << s << L'\n';
}
}
这篇关于从 C++ 中的 EnumWindows 生成的过滤/解析列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!