Solved

How to tell when a VB app is switched to from another application or after being minimised

Posted on 2004-08-04
7
204 Views
Last Modified: 2010-05-02
Hi,
I need to determine when my VB app regains focus after it has been switched to from another application (ALT+TAB) or restored after it has been minimised.

The form_gotfocus event isn't firedwhen this occurs.  The main control (infragistics data explorer)'s gotfocus event is fired, but is fired too often (whenever the user moves between the three panes on the form) and there is no way to distinguish between that and the aforementioned situation.

Does anyone know of a way i can achieve this (maybe through Windows API)?

Thanks in advance,

Jar
0
Comment
Question by:jpantherz
  • 3
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 500 total points
ID: 11723314
You can try subclassing the form to receive the WM_ACTIVATE message.. it'll notify you when it's activated/deactivated...


Form1:
-------------
Option Explicit
Private Sub Form_Load()
    Call WindowHook(True, Me.hWnd)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    Call WindowHook(False, Me.hWnd)
End Sub


Module1:
---------------
Option Explicit

Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Const GWL_WNDPROC As Long = (-4)

Private Const WM_ACTIVATE = &H6
'Private Const WM_ACTIVATEAPP = &H1C

Private lngPrevProc As Long
Public Sub WindowHook(ByVal blnHook As Boolean, ByVal lngHandle As Long)
    If blnHook = True Then
        lngPrevProc = SetWindowLong(lngHandle, GWL_WNDPROC, AddressOf WindowProc)
    Else
        Call SetWindowLong(lngHandle, GWL_WNDPROC, lngPrevProc)
    End If
End Sub
Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If uMsg = WM_ACTIVATE Then
        If wParam = 1 Then
            Form1.Caption = "Activated at: " & Second(Time)
        Else
            Form1.Caption = "DeActivated at: " & Second(Time)
        End If
    End If
    WindowProc = CallWindowProc(lngPrevProc, hWnd, uMsg, wParam, lParam)
End Function
0
 
LVL 15

Expert Comment

by:SRigney
ID: 11725370
There is a Form_Activate() that does that without having to subclass and catch the WM_ACTIVATE message.
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 11725894
From testing, those events aren't very reliable.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 11727865
Really.  That's good to know.  I've never had a problem with it, but I'll definitely keep this API code handy in case I need it.

Thanks z.
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 12331185
ping
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now