開発環境 |
Microsoft Visual C++ 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Win32 プロジェクト |
プロジェクト名 |
EnumWnd |
アプリケーションの種類 |
Windows アプリケーション |
追加のオプション |
空のプロジェクト |
文字セット |
Unicode |
ウィンドウ列挙。
EnumWnd.cpp
// Unicode
#include <Windows.h>
#include <string>
#include <vector>
#include "resource.h"
using namespace std;
struct WndInfo {
int Level;
wstring ClassName;
wstring WindowText;
};
// 関数プロトタイプ宣言
INT_PTR CALLBACK MainDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnGet(HWND hDlg);
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
// 外部変数
vector<WndInfo> WndInfoList;
//==============================================================================
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc);
return 0;
}
INT_PTR CALLBACK MainDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
INT_PTR nRet = TRUE; // メッセージを処理した
switch (uMsg) {
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_GET:
OnGet(hDlg);
break;
case IDCANCEL:
EndDialog(hDlg, IDCANCEL);
break;
}
break;
case WM_INITDIALOG:
nRet = TRUE; // SetFocusでフォーカスを設定した場合はFALSE
break;
case WM_CLOSE:
EndDialog(hDlg, 0);
break;
default:
nRet = FALSE; // メッセージを処理しなかった
}
return nRet;
}
void OnGet(HWND hDlg)
{
BOOL br = EnumWindows(EnumWindowsProc, 0);
wstring str;
for (vector<WndInfo>::iterator it = WndInfoList.begin(); it != WndInfoList.end(); it++) {
int level = (*it).Level;
while (level--) {
str += L"\t";
}
str += L"[" + (*it).ClassName + L"][" + (*it).WindowText + L"]\r\n";
}
SetDlgItemText(hDlg, IDC_EDIT, str.c_str());
}
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
WndInfo wi;
TCHAR buf[256];
int nr;
wi.Level = lParam;
nr = GetClassName(hWnd, buf, _countof(buf));
wi.ClassName = buf;
nr = GetWindowText(hWnd, buf, _countof(buf));
wi.WindowText = buf;
WndInfoList.push_back(wi);
BOOL br = EnumChildWindows(hWnd, EnumWindowsProc, lParam + 1);
return TRUE;
}
resource.h
#define IDD_MAIN 100
#define IDC_STATIC -1
#define IDC_GET 1000
#define IDC_EDIT 1001
EnumWnd.rc
// resource script
#include <windows.h>
#include "resource.h"
//------------------------------------------------------------------------------
IDD_MAIN DIALOGEX 100, 100, 320, 240
STYLE WS_POPUPWINDOW | WS_MINIMIZEBOX
EXSTYLE WS_EX_APPWINDOW
CAPTION "EnumWnd"
FONT 9, "MS Pゴシック"
BEGIN
PUSHBUTTON "Get(&G)",IDC_GET,5,5,40,15
EDITTEXT IDC_EDIT,5,25,310,210,
ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL
END
最終更新:2012年11月21日 10:16