Friday, September 11, 2009

How to get all desktop windows

Need to get a list of all desktop visible windows?
Here's a function which does just that
procedure GetDesktopWindows(List: TStrings);
var
  hDesktop,
  hWindow: Hwnd;
  Buffer: array[0..255] of char;
begin
  hDesktop := GetDeskTopWindow;
  hWindow := GetWindow(hDesktop, GW_CHILD);
  while hWindow <> 0 do begin
    GetWindowText(hWindow, Buffer, 255);
    if (Buffer <> '') and (IsWindowVisible(hWindow) > False) then
      List.Add(Buffer);
    hWindow := GetWindow(hWindow, GW_HWNDNEXT);
  end;
end;

Usage
procedure TForm1.Button1Click(Sender: TObject);
var
  List: TStrings;
begin
  List := TStringList.Create;
  GetDesktopWindows(List);
  ShowMessage(List.Text);
  FreeAndNil(List);
end;

And you get this



SEE huh?

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)