Solved

Check for form focus true or false

Posted on 2004-08-17
11
1,308 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
11 Comments
 
LVL 7

Expert Comment

by:petoskey-001
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Jimmyx1000
Comment Utility


I think the form.LostFocus should  work for me

how can i use this ?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Expert Comment

by:petoskey-001
Comment Utility
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
Comment Utility
0
 
LVL 4

Expert Comment

by:ark4877
Comment Utility
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
Comment Utility
Im just using a single form.

not mdi

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now