Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Fenster im im Vordergrund bringen. (https://www.delphipraxis.net/98178-fenster-im-im-vordergrund-bringen.html)

smart 22. Aug 2007 19:33


Fenster im im Vordergrund bringen.
 
Hallo Alle!

Wie kann ich ein Fenster von einem fremden Programm im Vordergrund bringen.

Gruß
Heike

DeddyH 22. Aug 2007 19:43

Re: Fenster im im Vordergrund bringen.
 
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var h: HWnd;
begin
  h := FindWindow('notepad',nil);
  if h <> 0 then
    SetForegroundWindow(h);
end;

mschaefer 22. Aug 2007 19:59

Re: Fenster im im Vordergrund bringen.
 
Moin, moin,

eignetlich sollte ich nicht mehr vor der Kiste sitzen,
aber nun bin ich doch tatsächlich wieder in der DP
gestrandet. Na denn man tau:

Notwendig sind zwei Schritte
1. das Handle bekommen und
2. das Fenster in den Vordergrund holen.
-------------------------------------------------------------------------------------------------



1 Sucht nach dem entsprechenden Fensterhandle
-------------------------------------------------------------------------------------------------

{
Beispiel, wie man ein Fenster "notepad" findet
und es anschliessend maximiert.
}

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  h: hwnd;
begin
  h := FindWindowByTitle('notepad');
  if h <> 0 then // if we found notepad
    ShowWindow(h, SW_MAXIMIZE)
  else
    ShowMessage('not found.');
end;
{ Holt das Handle aufgrund des Namensanteils }


Delphi-Quellcode:
function FindWindowByTitle(WindowTitle: string): Hwnd;
var
  NextHandle: Hwnd;
  NextTitle: array[0..260] of char;
begin
  // Get the first window
  NextHandle := GetWindow(Application.Handle, GW_HWNDFIRST);
  while NextHandle > 0 do
  begin
    // retrieve its text
    GetWindowText(NextHandle, NextTitle, 255);
    if Pos(WindowTitle, StrPas(NextTitle)) <> 0 then
    begin
      Result := NextHandle;
      Exit;
    end
    else
      // Get the next window
      NextHandle := GetWindow(NextHandle, GW_HWNDNEXT);
  end;
  Result := 0;
end;

2. Bringt das Fenster mit dem Handle in den Fordergrund
-------------------------------------------------------------------------------------------------

Delphi-Quellcode:
function ForceForegroundWindow(hwnd: THandle): Boolean;
const
  SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
  SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
  ForegroundThreadID: DWORD;
  ThisThreadID: DWORD;
  timeout: DWORD;
begin
  if IsIconic(hwnd) then ShowWindow(hwnd, SW_RESTORE);


  if GetForegroundWindow = hwnd then Result := True
  else
  begin
    // Windows 98/2000 doesn't want to foreground a window when some other
    // window has keyboard focus

    if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4)) or
      ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and
      ((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and
      (Win32MinorVersion > 0)))) then
    begin
      // Code from Karl E. Peterson, [url]www.mvps.org/vb/sample.htm[/url]
      // Converted to Delphi by Ray Lischner
      // Published in The Delphi Magazine 55, page 16 

      Result := False;
      ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow, nil);
      ThisThreadID := GetWindowThreadPRocessId(hwnd, nil);
      if AttachThreadInput(ThisThreadID, ForegroundThreadID, True) then
      begin
        BringWindowToTop(hwnd); // IE 5.5 related hack
        SetForegroundWindow(hwnd);
        AttachThreadInput(ThisThreadID, ForegroundThreadID, False);
        Result := (GetForegroundWindow = hwnd);
      end;
      if not Result then
      begin
        // Code by Daniel P. Stasinski
        SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
        SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0),
          SPIF_SENDCHANGE);
        BringWindowToTop(hwnd); // IE 5.5 related hack
        SetForegroundWindow(hWnd);
        SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE);
      end;
    end
    else
    begin
      BringWindowToTop(hwnd); // IE 5.5 related hack
      SetForegroundWindow(hwnd);
    end;


    Result := (GetForegroundWindow = hwnd);
  end;
end; { ForceForegroundWindow }
-------------------------------------------------------------------------------------------------


So das war es für mich heute ...
Grüße in den Westen // Martin

smart 22. Aug 2007 20:32

Re: Fenster im im Vordergrund bringen.
 
Hi,

vielen Dank Martin, für die sehr gute Antwort. Auch an alle Anderen mein Dank.

Gruß
Heike

Code 24. Aug 2007 14:15

Re: Fenster im im Vordergrund bringen.
 
Zitat:

Zitat von smart
Hi,

vielen Dank Martin, für die sehr gute Antwort. Auch an alle Anderen mein Dank.

Gruß
Heike

Hallo Heike!

Nur mal so eine Anmerkung, dass ganze klappt nur mit Fenstertitteln nicht mit Programmnamen.

Klaus.


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:06 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