Mega Code Archive

 
Categories / Delphi / Forms
 

A class for EnumWindows & Co

Title: a class for EnumWindows & Co Question: how enumerate windows and/or child windows the smart way Answer: The unit EnumWindowUtil in the attachment contains a class that capsules the Windows API functions EnumWindows, EnumChildWindows, EnumDesktopWindows, EnumThreadWindows and EnumWindowStations. This class is derived from TStringList. it is very easy to get all top level windows: var wlist : TWindowList; begin wlist := TWindowList.Create; // create TWindowList object try wlist.AddClassname := True; // we want windows Caption and classname wlist.EnumTopLevelWindows; // enum all windows ListBox1.Items.Assign(wlist); // copy the result to a listbox finally wlist.Free; end; end; You have access to the window handles, too. All window handles are stored internally in the Objects[] property and exposed through the Handles[] property // minimize all windows for i := 0 to wlist.Count-1 do begin ShowWindow(wlist.Handles[i], SW_MINIMIZE); end; Here is the Unit: unit EnumWindowUtil; { Wrapper-Class for EnumWindows, EnumChildWindows, EnumDesktopWindows 29.07.2004 Andreas Schmidt } interface uses Classes, Windows; type TWindowList = class(TStringList) private FUseCaption : Boolean; FShowUnvisibleWindows: Boolean; FAddClassname: Boolean; function GetHandles(idx: Integer): HWND; function GetClassNames(idx: Integer): string; public procedure EnumTopLevelWindows; procedure EnumDesktopWindows(handle:HWND); procedure EnumChildWindows(handle:HWND); procedure EnumThreadWindows(ThreadId:Cardinal); procedure EnumWindowStations; property ShowUnvisibleWindows:Boolean read FShowUnvisibleWindows write FShowUnvisibleWindows; property AddClassname:Boolean read FAddClassname write FAddClassname; property Handles[idx:Integer]:HWND read GetHandles; property ClassNames[idx:Integer]:string read GetClassNames; end; implementation uses SysUtils; { TWindowList } function GetWindowClassName(hwnd:HWND):string; begin SetLength(Result, 1024); GetClassName(hwnd, PChar(Result), Length(Result)); Result := PChar(Result); end; procedure EnumWindowCallback(hwnd:HWND; lParam:TWindowList); stdcall; var caption : string; begin if (not lParam.ShowUnvisibleWindows) and (not IsWindowVisible(hwnd)) then Exit; if lParam.FUseCaption then begin SetLength(caption, GetWindowTextLength(hwnd)); GetWindowText(hwnd, PChar(caption), Length(caption)+1); end else begin caption := Format('%6.6x', [hwnd]); end; if lParam.AddClassname then caption := caption + ':' +GetWindowClassName(hwnd); lParam.AddObject(caption, TObject(hwnd)); end; procedure EnumWindowStationCallback(station:PChar;lParam:TWindowList); stdcall; begin lParam.Add(station); end; procedure TWindowList.EnumChildWindows(handle: HWND); begin Clear; FUseCaption := False; if Windows.EnumChildWindows(handle, @EnumWindowCallback, Integer(Self)) then ; // RaiseLastWin32Error; end; procedure TWindowList.EnumDesktopWindows(handle: HWND); begin Clear; if Windows.EnumDesktopWindows(handle, @EnumWindowCallback, Integer(self)) then RaiseLastWin32Error; end; procedure TWindowList.EnumThreadWindows(ThreadId:Cardinal); begin if ThreadId = 0 then ThreadId := GetCurrentThreadId; Clear; FUseCaption := True; if not windows.EnumThreadWindows(ThreadId,@EnumWindowCallback, Integer(self)) then RaiseLastWin32Error; end; procedure TWindowList.EnumTopLevelWindows; begin Clear; FUseCaption := True; if not EnumWindows(@EnumWindowCallback, Integer(self)) then RaiseLastWin32Error; end; procedure TWindowList.EnumWindowStations; begin Clear; if not Windows.EnumWindowStations(@EnumWindowStationCallback, Integer(Self)) then RaiseLastWin32Error; end; function TWindowList.GetClassNames(idx: Integer): string; begin result := GetWindowClassName(GetHandles(idx)); end; function TWindowList.GetHandles(idx: Integer): HWND; begin Result := HWND(Objects[idx]); end; end.