2005. augusztus 4., csütörtök

How to get a list of all windows on the Desktop and their handles


Problem/Question/Abstract:

How to get a list of all windows on the Desktop and their handles

Answer:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure GetWins;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  WindowList: TList;

implementation

{$R *.DFM}

function GetWindows(Handle: HWND; Info: Pointer): BOOL; stdcall;
begin
  Result := True;
  WindowList.Add(Pointer(Handle));
end;

procedure TForm1.GetWins;
var
  TopWindow, CurrentWindow: HWND;
  Dest: array[0..80] of char;
  ClassName: array[0..80] of char;
  i: Integer;
begin
  try
    WindowList := TList.Create;
    TopWindow := Handle;
    EnumWindows(@GetWindows, Longint(@TopWindow));
    CurrentWindow := TopWindow;
    for i := 0 to WindowList.Count - 1 do
    begin
      CurrentWindow := GetNextWindow(CurrentWindow, GW_HWNDNEXT);
      GetWindowText(CurrentWindow, Dest, sizeof(Dest) - 1);
      GetClassName(CurrentWindow, ClassName, sizeof(ClassName) - 1);
      if StrLen(Dest) > 0 then
        Memo1.Lines.Add(Dest + ' = ' + ClassName + ' - ' + IntToStr(CurrentWindow));
    end;
  finally
    WindowList.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  GetWins;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése