Главная » Delphi » Основная » Ищем все окна в системе.

Ищем все окна в системе.

15:21
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;
 
type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
function GetWindowTitle(hwnd: HWND): string;
begin
  SetLength(Result, 255);
  SetLength(Result, GetWindowText(hwnd, PChar(Result), 255));
end;
 
function GetWindowClass(hwnd: HWND): string;
begin
  SetLength(Result, 255);
  SetLength(Result, GetClassName(hwnd, PChar(Result), 255));
end;
 
function GetWindowInfo(hwnd: HWND): string;
begin
  Result := GetWindowTitle(hwnd) + ' [' + GetWindowClass(hwnd) +
    '] (#' + IntToHex(hwnd, 8) + ')';
end;
 
function EnumChildProc(hwnd: HWND; lParam: Integer): BOOL; stdcall;
var
  NewNode, ParentNode: TTreeNode;
begin
  application.ProcessMessages;
  Result := True;
  ParentNode := TTreeNode(lParam);
  NewNode := ParentNode.Owner.AddChild(ParentNode,
    GetWindowInfo(hwnd));
  EnumChildWindows(hwnd, @EnumChildProc, Integer(NewNode));
end;
 
function EnumWindowsProc(hwnd: HWND; lParam: Integer): BOOL; stdcall;
var
  NewNode: TTreeNode;
begin
  Result := True;
  NewNode := TTreeView(lParam).Items.Add(nil, GetWindowInfo(hwnd));
  EnumChildWindows(hwnd, @EnumChildProc, Integer(NewNode));
end;
 
procedure EnumWindowsTree(Tree: TTreeView);
begin
  EnumWindows(@EnumWindowsProc, Integer(Tree));
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  TreeView1.Items.Clear;
  EnumWindowsTree(TreeView1);
  TreeView1.SortType := stText;
  TreeView1.AlphaSort();
end;
 
end.