BCB Client

- C++ Builderを用いたWindowsプログラミングメモ -

EnumWindows [ウィンドウハンドルを列挙し、タイトルを取得する]

今度は全ての親ウィンドウハンドルを列挙しつつタイトルも取得してみる。

EnumWindowsでハンドル列挙。
GetWindowTextでハンドルからタイトルの取得。


enumwindows.png

 ←こんな感じでフォームを作ってみた














TMemo名   Memo1
TButton名 DispHandleBtnとしました。

Memo1のプロパティ
ScrollBarsをssBothにして縦横スクロールバー表示。
Anchorsを全てtrueにしてフォームサイズ変更に対応。

DispHandleBtnを押すと、ズィっと取得してくれます。

前回のFindWindowでは、完全なタイトルを入力しないと
ハンドルを返してくれなかったのですが、
今回はハンドルを列挙する為、列挙されたハンドルから
指定の文字列とタイトルを比較すれば、
欲しいウィンドウハンドルを取得できたりと、応用できます。

<Unit1.cpp>
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

// コールバック関数宣言
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DispHandleBtnClick(TObject *Sender)
{
	// 画面上のウィンドウハンドルを全て列挙します
	EnumWindows((WNDENUMPROC)EnumWindowsProc, 0);
}
//---------------------------------------------------------------------------
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
	char *sTitle;
	int TitleLength;

	// タイトルの文字数を取得します
	TitleLength = GetWindowTextLength(hWnd) + 2;

	// タイトル格納用の領域確保
	sTitle = (char *)malloc(TitleLength);

	// ハンドルからタイトルを取得
	GetWindowText(hWnd, sTitle, TitleLength);

	// ハンドルとタイトルをMemo1へ表示
	AnsiString str = IntToHex((int)hWnd, 8) + " : " + sTitle;
	Form1->Memo1->Lines->Add(str);

	// 領域解放
	free(sTitle);

	// 列挙を続ける場合、TRUEを返します
	return TRUE;
}
//---------------------------------------------------------------------------

[ トップ | 一覧 | 前へ | 次へ ]

  • トップ
  • 一覧
  • リンク
  • 掲示板

Copyright © BCB Client. All Rights Reserved. Template by ネットマニア