Solved

Check for form focus true or false

Posted on 2004-08-17
11
1,314 Views
Last Modified: 2008-01-09
How can i check when the mouse is moved outside the
vb form area or if another application is selected outside
the vb form.

0
Comment
Question by:Jimmyx1000
[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
11 Comments
 
LVL 7

Expert Comment

by:petoskey-001
ID: 11821828
If another form received focus, you could set your form.LostFocus event.  

The mouse moving outside of your form would be a little harder because mouse move event only works when the mouse is over your form.  You could hook use SetWindowsHookEx to hook the mouse and then check if it's over your form or not.  

Can you give us a better idea of what your trying to achieve?

0
 

Author Comment

by:Jimmyx1000
ID: 11821886
i think the form.LostFocus should  work for me

how can i use this ?

0
 
LVL 4

Accepted Solution

by:
hamood earned 500 total points
ID: 11821889
Unfortunately, in VB the forms don't include an event when the application loses and receives focus. The good news that you can still get it. The bad news is that the solution relies on AddressOf operator and subclassing. The form receives a message that it lost focus, VB just doesn't expose it. The message to catch is WM_ACTIVATEAPP. Then the complimentary parameter in the message wParam indicates whether the app has gained or lost focus.

Warning, never end programs that use AddressOf operator by pressing the End button on the VB toolbar. End the program by clicking the X (close) button on the form.

Add the following to a .BAS module


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 WM_ACTIVATEAPP = &H1C
Private Const GWL_WNDPROC =    -4
Public lpPrevWndProc As Long
Public gHW As Long

Public Sub Hook()

      'Establish a hook to capture messages to this window
      lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)

End Sub

Public Sub Unhook()

      Dim temp As Long
      
      'Reset the message handler for this window
      temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
      
End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, _
      ByVal wParam As Long, ByVal lParam As Long) As Long

      'Check for the ActivateApp message
      If uMsg = WM_ACTIVATEAPP Then
            'Check to see if Activating the application
            If wParam = 0 Then      'Application Received Focus
                  Form1.Caption = "Focus Restored"
            Else
                  'Application Lost Focus
                  Form1.Caption = "Focus Lost"
            End If
      End If
      
      'Pass message on to the original window message handler
      WindowProc =  CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Function


Add the following code to the form


Option Explicit

Sub Form_Load()

    gHW = Me.hwnd   'Store handle to this form's window
    Hook    'Call procedure to begin capturing messages for this window
   
End Sub
Private Sub Form_Unload(Cancel As Integer)

    'Call procedure to stop intercepting the messages for this window
    Unhook
   
End Sub

HAMOOD
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Jimmyx1000
ID: 11822016


I think the form.LostFocus should  work for me

how can i use this ?
0
 
LVL 7

Expert Comment

by:petoskey-001
ID: 11822083
Ahh I see.  Form.LostFocus only tells you that your application has changed focus to a different form within the same application.  It doesn't tell you that the application has lost focus.  Form_Deactivate isn't quite it either.  

I must have been confusing it with .Net or Delphi...  His code should work.
0
 
LVL 7

Expert Comment

by:petoskey-001
ID: 11822121
0
 
LVL 4

Expert Comment

by:ark4877
ID: 11830428
hi,
  r u using single form or ur form is part of MDI and u like check which form is active with in the MDI forms.
0
 

Author Comment

by:Jimmyx1000
ID: 11841648
Im just using a single form.

not mdi

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

749 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