// EnumWindows 的功能是遍歷所有頂層窗口
function EnumWindows(
lpEnumFunc: TFNWndEnumProc; {回調函數指針}
lParam: LPARAM {給回調函數的參數, 它對應回調函數的第二個參數}
): BOOL; stdcall; //成功與否, 其實是返回了回調函數的返回值
// EnumWindows 專用的回調函數的格式:
function EnumWindowsProc(
hwnd: HWND; {找到的窗口句柄}
lParam: LPARAM {EnumWindows 傳給的參數; 因為它是指針, 可傳入, 但一般用作傳出數據}
): Boolean; stdcall; {函數返回 False 時, 調用它的 EnumWindows 將停止遍歷並返回 False}
例一: 獲取所有頂層窗口的句柄
//需要個 TListBox 顯示數據
function EnumWindowsProc_1(hwnd: HWND; lParam: LPARAM): Boolean; stdcall;
begin
Form1.ListBox1.Items.Add(IntToStr(hwnd));
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc_1, 0);
end;
例二: 獲取所有頂層窗口的標題(排除無標題窗口)
function EnumWindowsProc_2(hwnd: HWND; lParam: LPARAM): Boolean; stdcall;
var
buf: array[Byte] of Char;
begin
GetWindowText(hwnd, buf, SizeOf(buf));
if buf <> '' then Form1.ListBox1.Items.Add(buf);
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc_2, 0);
end;
例三: 查找第一個記事本的句柄
function EnumWindowsProc_3(hwnd: HWND; lParam: LPARAM): Boolean; stdcall;
var
buf: array[Byte] of Char;
begin
GetClassName(hwnd, buf, SizeOf(buf));
if buf = 'Notepad' then
begin
Form1.ListBox1.Items.Add(Format('記事本句柄: %d', [hwnd]));
Result := False; //如果不需要繼續找, 找到第一個就結束
end
else
begin
Result := True;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc_3, 0);
end;
例四: 獲取所有頂層窗口的句柄與類名數組:
type
TMyStruct = record
hwnd: HWND;
ClassName: string;
end;
TMyStructArray = array of TMyStruct;
PMyStructArray = ^TMyStructArray;
function EnumWindowsProc_4(hwnd: HWND; lParam: LPARAM): Boolean; stdcall;
var
buf: array[Byte] of Char;
p: PMyStructArray;
begin
GetClassName(hwnd, buf, SizeOf(buf));
p := PMyStructArray(lParam);
SetLength(p^, Length(p^) + 1);
p^[High(p^)].hwnd := hwnd;
p^[High(p^)].ClassName := buf;
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Arr: TMyStructArray;
begin
EnumWindows(@EnumWindowsProc_4, Integer(@Arr));
ShowMessage(IntToStr(Length(Arr))); //總數
ShowMessageFmt('%d - %s', [Arr[0].hwnd, Arr[0].ClassName]); //第一個元素
end;
例五: 重復上一個例子, 如果數組是全局遍歷並不靠參數傳遞, 代碼會簡單些
type
TMyStruct = record
hwnd: HWND;
ClassName: string;
end;
var
Arr: array of TMyStruct;
function EnumWindowsProc_5(hwnd: HWND; lParam: LPARAM): Boolean; stdcall;
var
buf: array[Byte] of Char;
begin
GetClassName(hwnd, buf, SizeOf(buf));
SetLength(Arr, Length(Arr) + 1);
Arr[High(Arr)].hwnd := hwnd;
Arr[High(Arr)].ClassName := buf;
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetLength(Arr, 0);
EnumWindows(@EnumWindowsProc_5, Integer(Arr));
ShowMessage(IntToStr(Length(Arr))); //總數
ShowMessageFmt('%d - %s', [Arr[0].hwnd, Arr[0].ClassName]); //第一個元素
end;
SplendourChiang 的目的是獲取進程 ID 和窗口句柄的數組, 按 "例四" 的方法給修改如下:
type
MyProcessInfo = record
PID, WinHWND : DWord;
end;
TMyProcessInfoArray = array of MyProcessInfo;
PMyProcessInfoArray = ^TMyProcessInfoArray;
function MyEnumWindowsProc(Wnd: DWord; lParam: LPARAM): Bool; stdcall;
var
PID : DWord;
p: PMyProcessInfoArray;
begin
GetWindowThreadProcessID(Wnd, @PID);
p := PMyProcessInfoArray(lParam);
SetLength(p^, Length(p^) + 1);
p^[High(p^)].PID := PID;
p^[High(p^)].WinHWND := Wnd;
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
CC: TMyProcessInfoArray;
begin
SetLength(CC, 0);
EnumWindows(@MyEnumWindowsProc, Integer(@CC));
ShowMessage(IntToStr(Length(CC))); //總數
ShowMessageFmt('%d - %d', [CC[0].PID, CC[0].WinHWND]); //第一個元素
end;