Author: Tomas Rutkauskas
I want my application to keep focus at any time. So, if someone clicks another
window, I want my application to retrieve back focus.
Answer:
Solve 1:
To get your application into the foreground in W98, ME, W2K and XP, instead of
using SetForegroundWindow, try this:
1 procedure ShowMe;
2 var
3 Th1, Th2: Cardinal;
4 begin
5 Th1 := GetCurrentThreadId;
6 Th2 := GetWindowThreadProcessId(GetForegroundWindow, nil);
7 AttachThreadInput(Th2, Th1, true);
8 try
9 SetForegroundWindow(Application.Handle);
10 finally
11 AttachThreadInput(Th2, Th1, false);
12 end;
13 end;
Solve 2:
As well as the SetForegroundWindow (if you are using Win9X and not WinNT/2000), you
could trick the system that your application is a running screensaver. In this case
it will not loose focus, for screensavers by design maintain focus.
14 { ... }
15 var
16 old: Bool;
17 begin
18 {Make it a Screensaver}
19 SystemParametersInfo(SPI_SCREENSAVERRUNNING, Word(True), @old, 0);
20
21 or
22
23 {Make it not a Screensaver}
24 SystemParametersInfo(SPI_SCREENSAVERRUNNING, Word(False), @old, 0);
|