How do I detect the focus going to another window

Do this..
create a blank form and run it.
next to it put another open window (control panel) so both are on the desktop not overlapping.
highlight the "control bar" on the control panel windo.
the "control bar" on the Form1 window goes "grey".
How do I detect that that has happend in my Form1 program?
And..when detected, how to I shif the focus back to the Form1 app?
Who is Participating?
mcriderConnect With a Mentor Commented:
You can use the following code in a timer or call it yourself.  The example is in a timer event:

Private Declare Function GetActiveWindow Lib "user32" () As Long
Public ActiveWindow As Long
Private Sub Timer1_Timer()
ActiveWindow = GetActiveWindow
If ActiveWindow <> Form1.hwnd Then
    AppActivate App.Title
End If
End Sub

rbenderAuthor Commented:
10 is all I have left.
rbenderAuthor Commented:
Edited text of question.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Have you checked the Form_LostFocus() event?
rbenderAuthor Commented:
I did..
I put form1.caption = "I'm Gone"
in that event but nothing seemed to happen when I moved to the other window.
I think you will find your answer here:

"HOWTO: Determine When Your Application Gains or Loses Focus"
Thanks for the points! Glad I could help!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.