動作環境
C++ Builder XE4
起動している多数のソフトウェアのキャプションを取得してみる。
参考
- Win32API ウィンドウを検索する FindWindowEx @ s-kitaの日記さん
情報感謝です。
実装
- 一つ目のサンプルコードをXE4で動くようにコードを修正
- TMemoへ出力
- UnicodeStringに対応
- 階層を定義し、1階層下までを表示
Unit1.cpp
//---------------------------------------------------------------------------
# include <vcl.h>
# pragma hdrstop
# include "Unit1.h"
# include <Windows.h>
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
static const int kMaxDepth = 1; // チェックする階層数
void TForm1::EnumetareWindows(HWND hWndParent, int depth)
{
wchar_t szBuff[512] = {0};
GetWindowText(hWndParent, szBuff, 512);
if (szBuff[0] != 0x00) {
if (depth <= kMaxDepth) {
String msg = IntToStr(depth) + L":" + String(szBuff);
Memo1->Lines->Add(msg);
}
}
HWND hWndChild = FindWindowEx(hWndParent, NULL, NULL, NULL);
if (hWndChild == NULL) {
return;
}
do {
EnumetareWindows(hWndChild, (depth+1));
} while ( (hWndChild = FindWindowEx(hWndParent, hWndChild, NULL, NULL)) != NULL );
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo1->Lines->Clear();
HWND hWndParent = GetDesktopWindow();
EnumetareWindows(hWndParent, /*depth=*/0);
}
//---------------------------------------------------------------------------
実行
- C++ Builder XE4起動状態
- ビルドしたソフト起動状態
- Project1.exe
- Tera Term起動状態
- メモ帳起動状態
備考
余分なウィンドウも捉えられるが、フィルタ条件を考慮すれば、希望のソフトのキャプションを取得できるだろう。
別の方法 > EnumWindows()
(追記 2018/10/24)
下記の記事を作る中、EnumWindows()を使う方法があると知りました。