?
Solved

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

Posted on 2004-08-04
7
Medium Priority
?
222 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
5 Comments
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 2000 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 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