Mirror

How to port the output of EnumWindows to a TStringList (Views: 100)


Problem/Question/Abstract:

How to port the output of EnumWindows to a TStringList

Answer:

Untested:

procedure TForm1.Button1Click(Sender: TObject);
begin
  StrList.clear;
  EnumWindows(@EnumWindowsProc, integer(StrList));
end;

function EnumWindowsProc(Wnd: HWND; lst: TStringList): BOOL; stdcall;
var
  capttxt: array[0..128] of Char;
begin
  Result := True;
  if IsWindowVisible(Wnd) { skip invisible windows } and
  ((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) { only top-level windows}
    or (HWND(GetWindowLong(Wnd, GWL_HWNDPARENT)) = GetDesktopWindow))
    and ((GetWindowLong(Wnd, GWL_EXSTYLE) and
    WS_EX_TOOLWINDOW) = 0) {skip Tool windows } then
  begin
    SendMessage(Wnd, WM_GETTEXT, Sizeof(capttxt), integer(@capttxt));
    List.Items.Add(capttxt);
  end;
end;

<< Back to main page