Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 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…
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…

696 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