抄自: https://blog.csdn.net/freedom2211/article/details/131288406
windowIterator.h
class CWindowIterator
{
private:
static char* wchar2char(const wchar_t* wchar);
static BOOL CALLBACK EnumWindowsProc(HWND window, LPARAM lParam);
public:
using EnumCallback = std::function<void(std::string &str)>;
void EnumWindow(EnumCallback callback);
};
+================================
windowIterator.cpp
char* CWindowIterator::wchar2char(const wchar_t* wchar)
{
char* pchar;
int len = WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), NULL, 0, NULL, NULL);
pchar = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), pchar, len, NULL, NULL);
pchar[len] = '\0';
return pchar;
}
BOOL CALLBACK CWindowIterator::EnumWindowsProc(HWND window, LPARAM lParam)
{
if (!IsWindow(window))
return TRUE;
if (!IsWindowVisible(window))
return TRUE;
DWORD flag = 0;
DwmGetWindowAttribute(window, DWMWA_CLOAKED, &flag, sizeof(flag));
if (flag)
return TRUE;
UINT wnd_style_ex = GetWindowLong(window, GWL_EXSTYLE);
if ((WS_EX_TOOLWINDOW & wnd_style_ex) == WS_EX_TOOLWINDOW)
return TRUE;
TCHAR szTitle[MAX_PATH] = { 0 };
::GetWindowText(window, szTitle, MAX_PATH); // 获取标题
if (wcslen(szTitle) == 0)
return TRUE;
std::string str = wchar2char(szTitle);
if (lParam) {
EnumCallback* cb = reinterpret_cast<EnumCallback*>(lParam);
(*cb)(str);
}
return TRUE;
}
void CWindowIterator::EnumWindow(EnumCallback callback)
{
::EnumWindows(EnumWindowsProc, (LPARAM)&callback);
}
==========================
windowIteratorTest.cpp
#include <iostream>
#include "windowIterator.h"
int main()
{
CWindowIterator wi;
wi.EnumWindow([](std::string &str) {
std::cout << str << std::endl;
});
return 0;
}
标签:pchar,窗口,Windows,获取,window,return,wchar,NULL,TRUE From: https://www.cnblogs.com/yyybill/p/17633225.html