Solved

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

Posted on 2004-08-04
7
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 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