Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Handle eines Fenster (https://www.delphipraxis.net/38901-handle-eines-fenster.html)

idontwantaname 25. Jan 2005 19:33


Handle eines Fenster
 
hi!

weiß nicht, ob das hier reinpasst, aber ich versuche es halt mal.
wie kann ich ein programm starten, und dessen handle herausbekommen ??

ich will es nämlich beim starten verstecken, ok, das kann man per ShellExecute auch, aber ich will es dann wieder mit CloseWindow(handle) schließen, also meine frage, wie krieg ich das handle heraus ??

Sprint 25. Jan 2005 20:12

Re: Handle eines Fenster
 
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;

Sprint 25. Jan 2005 20:17

Re: Handle eines Fenster
 
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;

idontwantaname 26. Jan 2005 12:17

Re: Handle eines Fenster
 
thx, das funktioniert super
:hi:


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:45 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz