How to Trap VB Form Lost Focus with GetActiveWindow API

This article was previously published under Q69792
This article has been archived. It is offered "as is" and will no longer be updated.
The LostFocus event in Microsoft Visual Basic is useful when transferringcontrol within an application, and you can use the form deactivate andactivate events in versions 2.0 and 3.0 to see if the entire form haslost the focus. However, in version 1.0, no global routine exists to checkfor the entire form losing the focus. To check whether your version1.0 application has lost the focus, periodically check the Windows APIfunction GetActiveWindow in a Visual Basic timer event, as explained below.
The only way that version 1.0 provides a check for loss of focus on aform or control is by triggering the LostFocus event. A form doessupport a LostFocus event; however, a form will only get focus ifthere are no controls on that form. Focus goes to the controls on aform, and when you click any other visible form, the control'sLostFocus procedure will be called. A control's LostFocus procedurewill also be called when another control on the form is activated. Toperform a routine that occurs only when the form loses focus requirescareful management of what generated a LostFocus event on each control(such as setting a flag if another control's Click event was called).

For a simpler method to check if a whole form has lost the focus, youcan call the Windows API function GetActiveWindow, located in USER.EXE(a DLL provided with Windows 3.0). The GetActiveWindow API callreturns the window handle of the currently active window, which is thenew window that you last clicked anywhere in Microsoft Windows. In atimer event procedure for the form, call GetActiveWindow and comparethe handle of the currently active Window with the handle of the formwindow (Form1.hWND). If the handle differs, you know the form has lostthe focus. The following program example demonstrates this technique:

Program Example

This single-form example will print "Lost Focus" on the form when youclick a different window (such as when you click another programrunning in Windows).

In Visual Basic, draw one timer control (Timer1) and one commandbutton (Command1) on a single form (Form1).

From the VB.EXE Code menu, choose View Code, and enter the followingcode for Form1, using (general) from the Object box, and(declarations) from the Procedure box:
   Declare Function GetActiveWindow Lib "User" () As Integer   Dim FOCUS As Integer   Const TRUE = -1   Const FALSE = 0				

From the Object box, choose Timer1, and from the Procedure box, chooseTimer, and then put the following code in the Timer1_Timer procedure:
Sub Timer1_Timer ()   If FOCUS = TRUE Then    ' Compare the handle of the currently active Window with the handle    ' of the Form1 window:    If GetActiveWindow() <> Form1.hWND Then       'Do form's lost-focus routines here.       Print "Lost Focus"       FOCUS = FALSE    End If   End IfEnd Sub				

You must set FOCUS=TRUE in the Click event procedure of every controlon the form, as follows:

From the Object box, choose Command1, and from the Procedure box, chooseClick, then put the following code in the Command1_Click procedure:
   Sub Command1_Click ()      FOCUS = TRUE   End Sub				

Double-click Form1 (at design time) and enter the following codefor the Form_Click procedure:
   Sub Form_Click ()      FOCUS = TRUE      Timer1.Interval = 10   End Sub				

You can now run the program.
"Programming Windows: the Microsoft Guide to Writing Applications forWindows 3," Charles Petzold. Microsoft Press, 1990.

"Microsoft Windows Software Development Kit: Reference Volume 1,"version 3.0.

WINSDK.HLP file shipped with Microsoft Windows 3.0 SoftwareDevelopment Kit.
2.00 3.00

Article ID: 69792 - Last Review: 12/04/2015 09:06:46 - Revision: 2.0

Microsoft Visual Basic 1.0 Standard Edition, Microsoft Visual Basic 2.0 Standard Edition, Microsoft Visual Basic 3.0 Professional Edition

  • kbnosurvey kbarchive KB69792