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
 
#2

Re: Handle eines Fenster

  Alt 25. Jan 2005, 20:12
Programm mit CreateProcess starten. Mit WaitForInputIdle warten bis das Programm soweit ist und das Fenster erzeugt worden ist. Mit EnumWindows alle TopLevel Fenster ermitteln und mit GetWindowThreadProcessId das Fenster finden, dass zu dem Programm gehört. Fertig. Hier mal ein Beispiel mit Notepad.
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
        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
          ShowMessage('Fensterhandle ist $' + IntToHex(AppHWnd, 8));
    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