How can I check if my application has focus?

fossilz picture fossilz · Sep 14, 2010 · Viewed 8.1k times · Source

What I want to do is check if my application has focus because if it is not then I will popup an Alert Window just over the Notification Area to display some message to the end user.

Answer

Remy Lebeau picture Remy Lebeau · Sep 14, 2010

Call Windows.GetForegroundWindow() and then pass the HWND to the Controls.FindControl() function. It will return a non-nil TWinControl pointer if the HWND belongs to your process. For example:

if FindControl(GetForegroundWindow()) <> nil then
  // has focus ...
else
  // does not have focus ...