Author: Jonas Bilinkevicius
I cannot get my overlapped or minimized window to come to the top by hook or crook.
I have tried the SetForegroundWindow, BringToTop and ShowWindow (show, restore) API
calls using the form, application and the application.mainform handles. Every
combination reacted the same. I get the visual clue of the application blinking on
the task bar for about 5 seconds and that's it. It never comes to the top to be
viewable. This is a Win98 SE test station.
Answer:
1
2 function ForceForegroundWindow(hwnd: THandle): boolean;
3 const
4 SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
5 SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
6 var
7 ForegroundThreadID: DWORD;
8 ThisThreadID: DWORD;
9 timeout: DWORD;
10 begin
11 if IsIconic(hwnd) then
12 ShowWindow(hwnd, SW_RESTORE);
13 if GetForegroundWindow = hwnd then
14 Result := true
15 else
16 begin
17 {Windows 98/2000 doesn't want to foreground a window when some
18 other window has keyboard focus}
19 if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4))
20 or ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and ((Win32MajorVersion > 4)
21 or ((Win32MajorVersion = 4) and (Win32MinorVersion > 0)))) then
22 begin
23 {Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
24 Converted to Delphi by Ray Lischner
25 Published in The Delphi Magazine 55, page 16}
26 Result := false;
27 ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow, nil);
28 ThisThreadID := GetWindowThreadProcessId(hwnd, nil);
29 if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then
30 begin
31 BringWindowToTop(hwnd); {IE 5.5 related hack}
32 SetForegroundWindow(hwnd);
33 AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
34 Result := (GetForegroundWindow = hwnd);
35 end;
36 if not Result then
37 begin
38 {Code by Daniel P. Stasinski}
39 SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
40 SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0),
41 SPIF_SENDCHANGE);
42 BringWindowToTop(hwnd); {IE 5.5 related hack}
43 SetForegroundWindow(hWnd);
44 SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout),
45 SPIF_SENDCHANGE);
46 end;
47 end
48 else
49 begin
50 BringWindowToTop(hwnd); {IE 5.5 related hack}
51 SetForegroundWindow(hwnd);
52 end;
53 Result := (GetForegroundWindow = hwnd);
54 end;
55 end;
|