Results 1 to 10 of 10

Thread: Why doesn't this lost focus work?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183
    I need to have an event occur whenever someone multitasks to another program window. I thought that a move off of the program window would throw this:

    Private Sub Form_LostFocus()
    MsgBox "selected another window", vbOKOnly
    End Sub


    But apparently not... Any ideas?

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Will not fire when the focus shifts to another prog outside of Visual Basic.

  3. #3
    Addicted Member
    Join Date
    Jul 2000
    Location
    FeedMeTown, Ohio
    Posts
    176
    Might try Form_Deactivate

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    Nope

    Nope
    Private Sub Form_Deactivate()
    MsgBox "selected another window", vbOKOnly
    End Sub

    Doesn't work either, any other ideas?

  5. #5
    gaffa
    Guest
    I'm going to guess and say that a modal form attached to a specific hWnd (ie: a message box), may not display the the app or form loses focus. Does it worl using Debug.Print statements?

    - gaffa

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183
    hummm... I think I see what your saying.... but no the debug.print doesn't work either

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Seems like you may have to subclass the window and catch the lost focus message.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    FOCUS_EVENT_RECORD ??

    perhaps, but that coud be fairly messy with this specific program, I just consulted my win 32 api's and found this gem, does this look like what I'm going for here?

  9. #9
    Matthew Gates
    Guest
    You must subclass to detect the Lost_Focus of a form.


    Code:
    Option Explicit
    
    Public Declare Function SetWindowLong Lib "user32" _
    Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal _
    ndx As Long, ByVal newValue As Long) As Long
    
    Public 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
    
    ' This is used with the SetWindowLong API function.
    Public Const GWL_WNDPROC = -4
    
    Public Const WM_KILLFOCUS = &H8
    
    
    Dim saveHWnd As Long        ' The handle of the subclassed window.
    Dim oldProcAddr As Long     ' The address of the original window procedure
    
    
    Sub StartSubclassing(ByVal hWnd As Long)
        saveHWnd = hWnd
        oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
    End Sub
    
    Sub StopSubclassing()
        SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
    End Sub
    
    Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
        ' Send the message to the original window procedure, and then
        ' return Windows the return value from the original procedure.
        WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)
        
    Select Case uMsg
        Case WM_KILLFOCUS
                Debug.Print "Form has lost focus"
        End Select
    End Function
    
    
    Private Sub Form_Load()
        StartSubclassing Me.hWnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        StopSubclassing
    End Sub

  10. #10
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    Teaudirenopossum.Musasapientumfixaestinaure.
    (I can't hear you. There's a banana in my ear)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width