我使用以下代码获取在我的机器上运行的窗口列表
#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是一个窗口
...
Task Switching
Microsoft Visual Studio
CiceroUIWndFrame
Battery Meter
Network Flyout
...
如何过滤/解析此数据,因为没有任何东西可用作标识符。
答案 0 :(得分:1)
我会使用EnumDesktopWindows
来枚举桌面上的所有顶级窗口;您甚至可以在枚举过程中使用IsWindowsVisible
API来过滤掉不可见的窗口。
这个可编译的C ++代码对我来说很好(注意,这里我展示了如何将一些额外的信息传递给枚举proc,在这种情况下使用指向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';
}
}
答案 1 :(得分:0)
定义您所谓的Window并查询Windows句柄以获取正确的属性。使用GetWindowLong()和GWL_HWNDPARENT来测试是否没有父窗口,或者父窗口是否是桌面窗口。可能需要额外的测试,例如,你可以使用(扩展)窗口样式。有关可用测试的其他想法,另请参阅here。