#pragma once
#include <vector>
//////////////////////////////////////////////////////////////////////
// CEnumWindows
class CEnumWindows
{
protected:
CEnumWindows()
{
}
virtual ~CEnumWindows()
{
}
public:
// トップレベルウィンドウの列挙
static HRESULT EnumWindows(std::vector<HWND>& hWnds)
{
HRESULT hr = S_OK;
CEnumWindows enumWindows;
hr = enumWindows._EnumWindows();
if (FAILED(hr))
{
return hr;
}
hWnds = enumWindows.m_hWnds;
return S_OK;
}
// 子ウィンドウの列挙
static HRESULT EnumChildWindows(HWND hWndParent, std::vector<HWND>& hWnds)
{
HRESULT hr = S_OK;
CEnumWindows enumWindows;
hr = enumWindows._EnumChildWindows(hWndParent);
if (FAILED(hr))
{
return hr;
}
hWnds = enumWindows.m_hWnds;
return S_OK;
}
// 子ウィンドウがフォーカスを持っているかどうか
static HRESULT HasFocusChildWindows(HWND hWndParent)
{
HRESULT hr = S_OK;
CEnumWindows enumWindows;
hr = enumWindows._EnumChildWindows(hWndParent);
if (FAILED(hr))
{
return E_FAIL;
}
std::vector<HWND>& hWnds = enumWindows.m_hWnds;
for (int i = 0; i < (int)hWnds.size(); i++)
{
HWND hWndFocus = ::GetFocus();
if (hWnds[i] == hWndFocus)
{
return S_OK;
}
}
return S_FALSE;
}
public:
// ウィンドウクラス名の取得
static CString GetClassName(HWND hWnd)
{
TCHAR szResult[256];
int nr = ::GetClassName(hWnd, szResult, sizeof(szResult));
if (!nr)
{
return CString();
}
return szResult;
}
// ウィンドウキャプションの取得
static CString GetWindowText(HWND hWnd)
{
TCHAR szResult[256];
int nr = ::GetWindowText(hWnd, szResult, sizeof(szResult));
if (!nr)
{
return CString();
}
return szResult;
}
protected:
HRESULT _EnumWindows()
{
m_hWnds.clear();
BOOL br = ::EnumWindows(EnumWindowsProc, (LPARAM)this);
if (br == 0)
{
return E_FAIL;
}
return S_OK;
}
HRESULT _EnumChildWindows(HWND hWndParent)
{
m_hWnds.clear();
BOOL br = ::EnumChildWindows(hWndParent, EnumWindowsProc, (LPARAM)this);
if (br == 0)
{
return E_FAIL;
}
return S_OK;
}
protected:
static BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
CEnumWindows* pThis = (CEnumWindows*)lParam;
// ASSERT(pThis);
pThis->m_hWnds.push_back(hWnd);
return !0;
}
public:
std::vector<HWND> m_hWnds;
};