Solved

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

Posted on 2004-08-04
7
202 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Window placement 17 65
VBA loop through headers using value 3 48
MsgBox 2 38
Saving history changes to sub form 4 26
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

14 Experts available now in Live!

Get 1:1 Help Now