![]() |
Fenster im im Vordergrund bringen.
Hallo Alle!
Wie kann ich ein Fenster von einem fremden Programm im Vordergrund bringen. Gruß Heike |
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; |
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:
{ Holt das Handle aufgrund des Namensanteils }
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;
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 |
Re: Fenster im im Vordergrund bringen.
Hi,
vielen Dank Martin, für die sehr gute Antwort. Auch an alle Anderen mein Dank. Gruß Heike |
Re: Fenster im im Vordergrund bringen.
Zitat:
Nur mal so eine Anmerkung, dass ganze klappt nur mit Fenstertitteln nicht mit Programmnamen. Klaus. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:03 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz