Thema: Delphi Handle eines Fenster

Einzelnen Beitrag anzeigen

Benutzerbild von Sprint
Sprint

Registriert seit: 18. Aug 2004
Ort: Edewecht
712 Beiträge
 
Delphi 5 Professional
 
#3

Re: Handle eines Fenster

  Alt 25. Jan 2005, 21:17
Hab noch was vergessen. Fenster soll ja wieder geschlossen werden.

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);

  function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
  begin
    TList(lParam).Add(Pointer(hWnd));
    Result := True;
  end;

var
  SI: TStartupInfo;
  PI: TProcessInformation;
  List: TList;
  ProcessId: DWORD;
  AppHWnd: HWND;
  I: Integer;
begin

  AppHWnd := 0;
  FillChar(SI, SizeOf(TStartupInfo), 0);
  SI.cb := SizeOf(TStartupInfo);
  SI.dwFlags := STARTF_USESHOWWINDOW;
  SI.wShowWindow := SW_HIDE;
  if CreateProcess(nil, 'NOTEPAD.EXE', nil, nil, False, 0, nil, nil, SI, PI) then
  begin
    WaitForInputIdle(PI.hProcess, INFINITE);
    CloseHandle(PI.hProcess);
    CloseHandle(PI.hThread);
    List := TList.Create;
    try
      if EnumWindows(@EnumWindowsProc, Longint(List)) then
      begin
        for I := 0 to List.Count - 1 do
          if GetWindowThreadProcessId(HWND(List.Items[I]), @ProcessId) <> 0 then
            if ProcessId = PI.dwProcessId then
            begin
              AppHWnd := HWND(List.Items[I]);
              Break;
            end;
        if IsWindow(AppHWnd) then
        begin
          ShowMessage('Fensterhandle ist $' + IntToHex(AppHWnd, 8));
          SendMessage(AppHWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
        end;
      end;
    finally
      List.Free;
    end;
  end;

end;
Ciao, Sprint.

"I don't know what I am doing, but I am sure I am having fun!"
  Mit Zitat antworten Zitat