The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries &nbsp


NOTE: this code has been superceded by the version at the new site.



&nbsp

Detecting when another application is activated

Reliable Deactivation Sample

Download the Deactivation detection sample project (13kb)

&nbsp Before you Begin &nbsp
&nbsp This project requires the SSubTmr.DLL component. Make sure you have loaded and registered this before trying the project. &nbsp

In a form, there is a Deactivate method. Exactly what this method is for is hard to determine, because it hardly ever seems to fire. Ok, that's perhaps a little unfair. But one thing you can't detect without a bit of additional work is when the user Alt-Tabs to another application. Detecting this can be useful, for example, when you are showing a pop-up tool window

When a form in your application is deactivated, Windows fires a WM_ACTIVATE message to the form. The wParam of this message tells you the reason the message has been fired:

wParam Meaning
0 Form deactivated
1 Form activated
2 Form activated by a mouse click

The code to make this work is very simple with SSUBTMR:

' Subclassing object to catch Alt-Tab
Implements ISubclass
Private Const WM_ACTIVATE = &H6

Private Sub Form_Load()
&nbsp &nbsp
' Start subclassing for WM_ACTIVATE
&nbsp &nbsp AttachMessage Me, Me.hwnd, WM_ACTIVATE
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
&nbsp &nbsp
' Clear up:
&nbsp &nbsp DetachMessage Me, Me.hwnd, WM_ACTIVATE
End Sub

Private Property Let ISubclass_MsgResponse(ByVal RHS As SSubTimer.EMsgResponse)
&nbsp &nbsp
' NR
End Property

Private Property Get ISubclass_MsgResponse() As SSubTimer.EMsgResponse
&nbsp &nbsp
' Respond to the message after windows has done its stuff:
&nbsp &nbsp ISubclass_MsgResponse = emrPreprocess
End Property

Private Function ISubclass_WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

&nbsp &nbsp Select Case wParam
&nbsp &nbsp Case 0
&nbsp &nbsp &nbsp &nbsp Me.Caption = "Deactivate"
&nbsp &nbsp Case 1
&nbsp &nbsp &nbsp &nbsp Me.Caption = "Activate"
&nbsp &nbsp Case 2
&nbsp &nbsp &nbsp &nbsp Me.Caption = "Mouse Activate"
&nbsp &nbsp End Select

End Function



TopBack to top
Source Code - What We're About!Back to Source Code

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 23 November 1998