views:

137

answers:

2

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.

+3  A: 

If your application consists of a single form, then

GetForegroundWindow = Handle

will suffice. The expression above is true if and only if the your form is the foreground window, that is, if keyboard focus belongs to a control on this form (or to the form itself).

If your application consists of a number of forms, simply loop through them and check if any of them matches GetForegroundWindow.

Andreas Rejbrand
+6  A: 

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()) <> 0 then
  // has focus ...
else
  // does not have focus ...
Remy Lebeau - TeamB