Mirror

How to keep your application focused at all times (Views: 100)


Problem/Question/Abstract:

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:


procedure ShowMe;
var
  Th1, Th2: Cardinal;
begin
  Th1 := GetCurrentThreadId;
  Th2 := GetWindowThreadProcessId(GetForegroundWindow, nil);
  AttachThreadInput(Th2, Th1, true);
  try
    SetForegroundWindow(Application.Handle);
  finally
    AttachThreadInput(Th2, Th1, false);
  end;
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.

{ ... }
var
  old: Bool;
begin
  {Make it a Screensaver}
  SystemParametersInfo(SPI_SCREENSAVERRUNNING, Word(True), @old, 0);

or

{Make it not a Screensaver}
SystemParametersInfo(SPI_SCREENSAVERRUNNING, Word(False), @old, 0);

<< Back to main page