我正在使用以下代码来获取我的计算机上运行的窗口列表
#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 个条目的列表,其中大多数不是我所说的窗口。
以下是在我的计算机上运行上述脚本所产生的输出的摘录,在 5 个条目中,只有 Microsoft Visual Studio 是一个 Window
...
Task Switching
Microsoft Visual Studio
CiceroUIWndFrame
Battery Meter
Network Flyout
...
我该如何过滤/解析这些数据,因为没有任何东西可以用作标识符。
我会用EnumDesktopWindows https://msdn.microsoft.com/en-us/library/windows/desktop/ms682615(v=vs.85).aspx枚举桌面上的所有顶级窗口;你甚至可以使用IsWindowsVisible https://msdn.microsoft.com/en-us/library/windows/desktop/ms633530(v=vs.85).aspxAPI在枚举过程中,过滤掉不可见的窗口。
这个可编译的 C++ 代码对我来说工作得很好(请注意,这里我展示了如何将一些附加信息传递给枚举过程,在本例中使用指向vector<wstring>
,其中存储窗口标题以供以后处理):
#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';
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)