Link to home
Start Free TrialLog in
Avatar of Motaz
Motaz

asked on

Check application main form focus

I want to know if my main form is focused or there is another application focused.
I tried Form's Focused property, IsWindowShowing, etc, but I didn't get any result.

Motaz
ASKER CERTIFIED SOLUTION
Avatar of dMa
dMa
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of geobul
geobul

Hi,

procedure TForm1.Timer1Timer(Sender: TObject);
var
  h: HWND;
begin
  h := GetForegroundWindow;
  if IsWindow(h) then
    if Application.MainForm.Handle = h then
      Label1.Caption := 'Active'
    else Label1.Caption := 'Not active';
end;

Regards, Geo
Avatar of Motaz

ASKER

Thanks, that's exactly what I want.

   If GetActiveWindow = 0 then
      FlashWindow(Application.Handle, False);

Motaz
The code you've chosen doesn't guarantee you that your main form has the focus. It could be any form belonging to your app.

On the other hand, my code does.

Regards, Geo