program EnumWindow;
{$mode Delphi}
uses windows;
var
wClass: TWndClass;
hInst: HWND;
hWindow: HWND;
hButton: HWND;
Msg: TMSG;
function EnumChildWindowsProc(hWnd: HWnd; lParam: LPARAM): Bool; stdcall;
var
Title: array[0..255] of Char;
begin
GetWindowText(hWnd, Title, 255);
if IsWindowVisible(hWnd) and (String(Title) <> '') then
MessageBox(0, PChar(Title), PChar('Child window'), 0);
if GetWindow(hWnd, GW_CHILD) > 0 then
Enumchildwindows(hWnd, @EnumChildWindowsProc, 0);
Result := True;
end;
function EnumWindowsProc(hWnd: HWND; {%H-}lParam: LPARAM): BOOL stdcall;
var
Title: array[0..255] of Char;
begin
GetWindowText(hWnd, Title, 255);
if IsWindowVisible(hWnd) and (String(Title) <> '') then
MessageBox(0, PChar(Title), PChar('Top window'), 0);
EnumChildWindows(hWnd, @EnumChildWindowsProc, 0);
Result := True;
end;
procedure DoOnclick;
begin
EnumWindows(@EnumWindowsProc, 0);
end;
procedure DoDestroy;
begin
UnRegisterClass('My class', hInst);
ExitProcess(hInst);
end;
procedure DoPaint;
begin
//
end;
function WindowProc(hWnd, Msg, wParam, lParam: DWord): Longint; stdcall;
begin
Result := DefWindowProc(hWnd, Msg, wParam, lParam);
case Msg of
WM_COMMAND: if lparam = hButton then DoOnClick;
WM_DESTROY: DoDestroy;
WM_PAINT: DoPaint;
end;
end;
begin
hInst := GetModuleHandle(nil);
with wClass do
begin
hIcon := LoadIcon(hInst, 'MAINICON');
lpfnWndProc:= @WindowProc;
hInstance := hInst;
hbrBackground := COLOR_BTNFACE;
lpszClassName := 'My class';
end;
RegisterClass(wClass);
hWindow := CreateWindow('My class', 'My main window', WS_SYSMENU or WS_VISIBLE or WS_SIZEBOX, 10, 10, 400, 300, 0, 0, hInst, nil);
hButton := CreateWindow('Button', 'Enum windows', WS_CHILD or BS_PUSHLIKE or BS_TEXT or WS_VISIBLE, (400 - 125) div 2, 125, 125, 24, hWindow, 0, hInst, nil);
SetFocus(hButton);
while(GetMessage({%H-}Msg, hWindow, 0, 0))do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end.