Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Detect when the application gets or loses the input focus
[ All Languages » VB »  Windows]

Total Hit ( 3467)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


VB forms exposes the Activate and Deactivate events, which fire when the form gets and loses the focus because the user has clicked on another form of the same application. However, VB doesn't fire any event when the user clicks on a form that belongs to another Windows application. In some cases this can be a problem, because your program might rely on that that has been gathered when the form has been loaded - for example, the list of files in a given directory - and you can't be 100% sure that the data hasn't been modified by the user (for example, by deleting the files from Explorer).

To get a notification when the application as a whole gets or loses the input focus, you must subclass a form in your application, and watch for the WM_ACTIVATEAPP message. When this message is received, the wParam argument holds zero if the application is losing the focus, or a non-zero value if the application is getting the input focus. This is the code that does the trick:

Click here to copy the following block
' REQUIRES THE MSGHOOK.DLL COMPONENT

' you can omit the following constant, because it is
' defined in the MsgHook type library
Const WM_ACTIVATEAPP = &H1C

Dim WithEvents FormHook As MsgHook

Private Sub Form_Load()
  ' subclass the form with events
  Set FormHook = New MsgHook
  FormHook.StartSubclass hWnd
End Sub

Private Sub FormHook_AfterMessage(ByVal uMsg As Long, ByVal wParam As Long, _
  ByVal lParam As Long, retValue As Long)
  If uMsg = WM_ACTIVATEAPP Then
    If wParam Then
      ' The application is being activated
    Else
      ' The application is being deactivated
    End If
  End If
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.