delphiwinapivclnotification-area

How can I check if my application has focus?


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.


Solution

  • 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 ...