unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Edit1: TEdit;
    Button6: TButton;
    Button7: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  List:TStringList;
type
 TModuleArray = array of TModuleEntry32;
// Возвращает список описаний (TModuleEntry32) модулей по идентификатору процесса    
function GetModulesListByProcessId(ProcessId : Cardinal) : TModuleArray;

implementation



{$R *.dfm}
function GetModulesListByProcessId(ProcessId : Cardinal) : TModuleArray;
var
 hSnapshot : THandle;
 lpme : TModuleEntry32;

 procedure AddModuleToList;
   begin
     SetLength(Result,High(Result)+2);
     Result[high(Result)]:=lpme;
   end;

begin
 SetLength(Result,0);
 hSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,ProcessId);
 if hSnapshot=-1 then
   RaiseLastWin32Error;
 lpme.dwSize:=SizeOf(lpme);
 if Module32First(hSnapshot,lpme) then
   begin
     AddModuleToList;
     while Module32Next(hSnapshot,lpme) do
       AddModuleToList;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
VAR
  Wnd : hWnd;
  buff: ARRAY [0..127] OF Char;
  Pid : Cardinal;
 modarr : TModuleArray;
 Names : String;
  St1:string;
  i:integer;
begin
  ListBox1.Clear;
  // Получаем hWnd первого окна
  Wnd := GetWindow(Handle, gw_HWndFirst);
  // Цикл поиска окон
  WHILE Wnd <> 0 DO BEGIN
    IF (Wnd <> Application.Handle) AND // -Собственное окно
       {(GetWindow(Wnd, gw_Owner) = 0) AND }  // -Дочернии окна
       (GetWindowText(Wnd, buff, sizeof(buff)) <> 0) // -Окна без заголовков
    THEN
    BEGIN
      St1:='';
      GetWindowText(Wnd, buff, sizeof(buff));
      if GetWindow(Wnd, gw_Owner) = 0 then
      begin
      IF IsWindowVisible(Wnd) then              // -видимые окна
      St1:=StrPas(buff)  +''+'|'+IntToStr(Wnd) else
      if (CheckBox1.Checked) then
        St1:=StrPas(buff)+'|         Невидимое окно        '+'|'+IntToStr(Wnd);

      end else if (CheckBox2.Checked) then
             St1:=StrPas(buff)+'|         Дочернее окно        '+'|'+IntToStr(Wnd);

      if St1<>'' then
      begin
      GetWindowThreadProcessId(Wnd,@Pid);
     modarr:=GetModulesListByProcessId(Pid);
     St1:=St1+'|'+IntToStr(Pid);
 names:='Null';

 for i:=0 to High(modarr) do
   begin
     if Integer(modarr[i].modBaseAddr)=$400000 then
       begin
         names:=modarr[i].szExePath;
         break;
       end;
   end;
     if names<>'Null' then St1:=St1+'|'+names;
      ListBox1.Items.Add(St1)
      end;
    END;
    // Ищем новое окно
    Wnd := GetWindow(Wnd,gw_hWndNext);
  END;
  ListBox1.ItemIndex := 0;
end;

procedure TForm1.Button2Click(Sender: TObject);
var  Wnd : hWnd;
begin
if ListBox1.Itemindex<0 then Exit;
wnd:=FindWindow(nil,Pchar(cNFi(1,ListBox1.Items[ListBox1.ItemIndex],'|')));
if wnd<>0 then begin SetForegroundWindow(Wnd){ShowWindow(wnd, SW_Show)};end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var  Wnd : hWnd;
begin
if ListBox1.Itemindex<0 then Exit;
wnd:=FindWindow(nil,Pchar(cNFi(1,ListBox1.Items[ListBox1.ItemIndex],'|')));
if wnd<>0 then begin ShowWindow(wnd, SW_Hide);{ShowWindow(wnd, SW_Show)};end;
wnd:=FindWindow(nil,Pchar(cNFi(1,ListBox1.Items[ListBox1.ItemIndex],'|')));
IF Not IsWindowVisible(Wnd) then ListBox1.Items[ListBox1.ItemIndex]:=ListBox1.Items[ListBox1.ItemIndex]+'|         Невидимое окно        ';
end;

procedure TForm1.Button4Click(Sender: TObject);
var  Wnd : hWnd;
begin
if ListBox1.Itemindex<0 then Exit;
wnd:=FindWindow(nil,Pchar(cNFi(1,ListBox1.Items[ListBox1.ItemIndex],'|')));
if wnd<>0 then begin ShowWindow(wnd, SW_Show);{SetForegroundWindow(Wnd)};end;
wnd:=FindWindow(nil,Pchar(cNFi(1,ListBox1.Items[ListBox1.ItemIndex],'|')));
IF IsWindowVisible(Wnd) then ListBox1.Items[ListBox1.ItemIndex]:=cNFi(1,ListBox1.Items[ListBox1.ItemIndex],'|');
end;

procedure TForm1.Button5Click(Sender: TObject);
var  Wnd : hWnd;
begin
if ListBox1.Itemindex<0 then Exit;
wnd:=FindWindow(nil,Pchar(cNFi(1,ListBox1.Items[ListBox1.ItemIndex],'|')));
if wnd<>0 then begin SendMessage(wnd,WM_CLOSE,0,0);end;
Button1Click(Self);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
Edit1.Text:=cNFi(1,ListBox1.Items[ListBox1.ItemIndex],'|')
end;

procedure TForm1.Button6Click(Sender: TObject);
var i1,i2:integer;St1:string;
begin
List.Clear;
for i1:=0 to ListBox1.Items.Count -1  do List.add (ListBox1.Items[i1]);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
List:=TstringList.Create;
end;
{---------------------}
procedure TForm1.Button7Click(Sender: TObject);
var i1,i2:integer;St1:string;
begin
Button1Click(nil);
i1:=0;
While i1<ListBox1.items.Count do
 begin
   for i2:=0 to List.Count -1 do
      if ListBox1.items[i1]=List.strings[i2] then
        begin
         ListBox1.items.Delete(i1);
         Continue;
        end;
   Inc(i1);
 end;
end;
{---------------------}
end.
