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
LVL 7
MotazAsked:
Who is Participating?
 
dMaConnect With a Mentor Commented:
check forst the windows api function getfocus
GetActiveWindow(VOID)
GetFocus(VOID)
0
 
geobulCommented:
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
0
 
MotazAuthor Commented:
Thanks, that's exactly what I want.

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

Motaz
0
 
geobulCommented:
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
0
All Courses

From novice to tech pro — start learning today.