Results 1 to 11 of 11

Thread: [RESOLVED] about get and lost focus event

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Resolved [RESOLVED] about get and lost focus event

    i'm using visual basic 6 and i have these simple project:
    Code:
    Option Explicit
    
    Private Sub Form_GotFocus()
         MsgBox "Hello"
    End Sub
    
    Private Sub Form_LostFocus()
        MsgBox "Bye"
    End Sub
    why the LostFocus form Event is ignored?(i don't know if these is importante: i don't have any updates installed..)
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: about get and lost focus event

    How are you changing focus?

    Are you leaving the app by clicking on another app or some other window??

    Those events don't fire when you leave and return to your app. There is a windows-message you can listen for that will tell you this - it's just a bit more complicated.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: about get and lost focus event

    Quote Originally Posted by szlamany
    How are you changing focus?

    Are you leaving the app by clicking on another app or some other window??

    Those events don't fire when you leave and return to your app. There is a windows-message you can listen for that will tell you this - it's just a bit more complicated.
    "Are you leaving the app by clicking on another app or some other window??"
    yes i'm... why visual basic 6 have these events, if are ignored?
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: about get and lost focus event

    You can have several forms in your app - those events fire when you move from form to form in your app.

    Look at post #3 of this thread

    http://www.vbforums.com/showthread.p...WM_ACTIVATEAPP

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: about get and lost focus event

    but i recive some errors:

    Code:
    Public Sub AppFocus(ByVal v_bFocus As Boolean)
    
    Dim i As Long, j As Long, k As Long, x As Long, y As Long, z As Long
    Dim s1 As String, s2 As String, s3 As String, s4 As String, s5 As String
      
      If v_bFocus Then
        'App has just gained focus
        Me.Caption = "ActivateApp; has-focus with TAB...TAG=" & tabForms.SelectedItem.Tag 'here
        'Me.Caption = "ActivateApp gmodal" & gModal
        If Not gModal Then 'these condition
            For x = 2 To Forms.Count - 1
                If tabForms.SelectedItem.Tag = CStr(Forms(x).mintTabNo) Then
        '            Me.Caption = "ActivateApp; has-focus TXTINPUT.TAG=" & Forms(x).txtInput.Tag & " CBOINPUT.TAG=" & Forms(x).cboInput.Tag
                    gfrmKeyDown(Forms(x).mintTabNo) = -1 'here
                    Call FocusPilot(Forms(x), 0, 0, 0, 0)
                    Exit For
                End If
            Next
        End If
      Else
        'App has just lost focus
        'Me.Caption = "ActivateApp; no-focus"
      End If
    End Sub
    and i don't know if there is more...
    can you help me complete these?
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: about get and lost focus event

    In the hook they are calling the form "m_frmHooked" with a Sub named "AppFocus", so is the form you are using named m_frmHooked?

  7. #7

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: about get and lost focus event

    i did what you said.. but i continue with some problems... heres my project...
    can anyone help me finish the problem?
    thanks
    Attached Files Attached Files
    VB6 2D Sprite control

    To live is difficult, but we do it.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: about get and lost focus event

    The concept is that the function APPFOCUS gets called when the app gains or loses focus. Subclassing tells windows that when that specific message (WM_ACTIVATEAPP) is "heard" to call that function in your app.

    You do not need all that code that appears in APPFOCUS - that's specific to what I needed to have happen.

    Put just a msgbox in that function for now - so you can see how the process itself works.

    You can see that I originally had it change the FORM CAPTION - so you can see the actual event occur.

    btw - careful about using this in the IDE - it will cause VB to abort out to desktop sometimes - save often.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: about get and lost focus event

    but continue be ignored and i don't hunderstand why...
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: about get and lost focus event

    Ok - try this

    In a new project create a module - and put this code in it.

    Code:
    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_ACTIVATEAPP = &H1C
    
    'm_frmHooked is the form that the subclass 'listens' in on
    Private m_lPrevProc As Long, m_frmHooked As Form
    
    Public Function WndProc(ByVal hWnd As Long, _
            ByVal wMsg As Long, ByVal wParam As Long, _
            ByVal lParam As Long) As Long
            
      'Let the window process messages as well
      WndProc = CallWindowProc(m_lPrevProc, hWnd, wMsg, wParam, lParam)
    
      'See what message has been sent to the window
      If wMsg = WM_ACTIVATEAPP Then
        'It's the activateapp message so call the sub on the form
        Call m_frmHooked.AppFocus(CBool(wParam))
      End If
    End Function
    
    Public Sub Unsubclass()
      Debug.Print "UnSubClass"
      Call SetWindowLong(m_frmHooked.hWnd, GWL_WNDPROC, m_lPrevProc)
      Set m_frmHooked = Nothing
    End Sub
    
    Public Sub Subclass(ByRef r_frm As Form)
      Debug.Print "SubClass"
      Set m_frmHooked = r_frm
      m_lPrevProc = SetWindowLong(r_frm.hWnd, GWL_WNDPROC, AddressOf WndProc)
    End Sub
    Now in the form itself - put this code.
    Code:
    Private Sub Form_Load()
    
    Call Subclass(Me)
    
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    
    Call Unsubclass
    
    End Sub
    
    Public Sub AppFocus(ByVal v_bFocus As Boolean)
    
      If v_bFocus Then
        'App has just gained focus
        Me.Caption = "ActivateApp; has-focus"
      Else
        'App has just lost focus
        Me.Caption = "ActivateApp; no-focus"
      End If
    End Sub
    You are probably best to create an .EXE to run this - as subclassing in the IDE is a bad idea.

    Does this work for you??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: about get and lost focus event

    yes... now is working thanks... thank you for everything
    VB6 2D Sprite control

    To live is difficult, but we do it.

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