Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet information about the window under the mouse
Keywordswindow information, mouse
CategoriesUtilities
 
Thanks to Bob Askey.

When a Timer fires, use the GetCursorPos and WindowFromPoint API functions to see where the mouse is and find the window under it. Use GetWindowText, GetClassName, GetWindowLong, GetWindowWord, and GetModuleFileName to get information about the window.

 
Private Sub Timer1_Timer()
#If Win16 Then
    ...
#ElseIf Win32 Then
    Dim pt32 As POINTAPI
    Dim ptx As Long
    Dim pty As Long
    Dim sWindowText As String * 100
    Dim sClassName As String * 100
    Dim hWndOver As Long
    Dim hWndParent As Long
    Dim sParentClassName As String * 100
    Dim wID As Long
    Dim lWindowStyle As Long
    Dim hInstance As Long
    Dim sParentWindowText As String * 100
    Dim sModuleFileName As String * 100
    Static hWndLast As Long

    Call GetCursorPos(pt32)               ' Get cursor
        ' position
    ptx = pt32.x
    pty = pt32.y
    hWndOver = WindowFromPointXY(ptx, pty)    ' Get window
        ' cursor is over
    If hWndOver <> hWndLast Then              ' If changed
        ' update display
       hWndLast = hWndOver                    ' Save change
       Cls                                      ' Clear the
           ' form
       Print "Window Handle: &H"; Hex(hWndOver) ' Display
           ' window handle

       r = GetWindowText(hWndOver, sWindowText, 100)      '
           ' Window text
       Print "Window Text: " & Left(sWindowText, r)

       r = GetClassName(hWndOver, sClassName, 100)         _
           ' Window Class
       Print "Window Class Name: "; Left(sClassName, r)

       lWindowStyle = GetWindowLong(hWndOver, GWL_STYLE)   _
           ' Window Style
       Print "Window Style: &H"; Hex(lWindowStyle)

       ' Get handle of parent window:
       hWndParent = GetParent(hWndOver)

       ' If there is a parent get more info:
       If hWndParent <> 0 Then
          ' Get ID of window:
          wID = GetWindowWord(hWndOver, GWW_ID)
          Print "Window ID Number: &H"; Hex(wID)
          Print "Parent Window Handle: &H"; Hex(hWndParent)

          ' Get the text of the Parent window:
          r = GetWindowText(hWndParent, sParentWindowText, _
              100)
          Print "Parent Window Text: " & _
              Left(sParentWindowText, r)

          ' Get the class name of the parent window:
          r = GetClassName(hWndParent, sParentClassName, _
              100)
          Print "Parent Window Class Name: "; _
              Left(sParentClassName, r)
       Else
          ' Update fields when no parent:
          Print "Window ID Number: N/A"
          Print "Parent Window Handle: N/A"
          Print "Parent Window Text : N/A"
          Print "Parent Window Class Name: N/A"
       End If

       ' Get window instance:
       hInstance = GetWindowWord(hWndOver, GWW_HINSTANCE)

       ' Get module file name:
       r = GetModuleFileName(hInstance, sModuleFileName, _
           100)
       Print "Module: "; Left(sModuleFileName, r)
    End If
#End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated