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