Results 1 to 2 of 2

Thread: FindWindow API helper

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    FindWindow API helper

    This code will Debug.Print the required steps to take to find a window handle using FindWindow(Ex) calls. It will print out the steps for the handle of the window your cursor is over when you press the left-control button

    Based on the code from
    http://www.vbforums.com/attachment....achmentid=48851
    You need a timer called timer1 on the form

    VB Code:
    1. Private Type POINTAPI
    2.     x As Long
    3.     y As Long
    4. End Type
    5. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    6. Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    7. Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
    8. Private Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer
    9. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    10. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    11. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    12. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    13. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    14. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    15. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    16.  
    17. Private Sub Form_Load()
    18. Timer1.Interval = 1
    19. End Sub
    20.  
    21. Sub Timer1_Timer()
    22. Dim pt32 As POINTAPI
    23. Dim sWindowText As String * 100, sWindowLen As Integer
    24. Dim sClassName As String * 100, sClassLen As Integer
    25. Dim hWndOver As Long
    26. Dim hWndParent As Long
    27. Dim sParentClassName As String * 100, sParentWindowLen As Integer
    28. Dim sParentWindowText As String * 100, sParentClassLen As Integer
    29. Static hWndLast As Long
    30.  
    31. If GetAsyncKeyState(17) Then
    32.     Call GetCursorPos(pt32)
    33.     hWndOver = WindowFromPointXY(pt32.x, pt32.y)
    34.     If hWndOver <> hWndLast Then
    35.         hWndLast = hWndOver
    36.         Debug.Print "-------------------------" & vbCrLf & "Window Info:"
    37.         Debug.Print "Window Handle: &H"; Hex(hWndOver)
    38.         sWindowLen = GetWindowText(hWndOver, sWindowText, 100)
    39.         Debug.Print "Window Text: " & Left(sWindowText, sWindowLen)
    40.         sClassLen = GetClassName(hWndOver, sClassName, 100)
    41.         Debug.Print "Window Class Name: "; Left(sClassName, sClassLen)
    42.         hWndParent = GetParent(hWndOver)
    43.        
    44.         Dim parentHandles() As Long, parentCount As Integer
    45.         Debug.Print vbCrLf & "#################################################" & vbCrLf & "Dim hWnd as Long"
    46.         While hWndParent <> 0
    47.             ReDim Preserve parentHandles(parentCount)
    48.             parentHandles(parentCount) = hWndParent
    49.             hWndParent = GetParent(hWndParent)
    50.             parentCount = parentCount + 1
    51.         Wend
    52.        
    53.         Dim printStr As String
    54.         For i = parentCount - 1 To 0 Step -1
    55.             sParentWindowLen = GetWindowText(parentHandles(i), sParentWindowText, 100)
    56.             sParentClassLen = GetClassName(parentHandles(i), sParentClassName, 100)
    57.            
    58.             If i = parentCount - 1 Then
    59.                 printStr = "hWnd = FindWindow(" & Chr(34) & Left(sParentClassName, sParentClassLen) & Chr(34) & ", " & Chr(34) & Left(sParentWindowText, sParentWindowLen) & Chr(34) & ")"
    60.             Else
    61.                 printStr = "hWnd = FindWindowEx(hWnd, 0&, " & Chr(34) & Left(sParentClassName, sParentClassLen) & Chr(34) & ", " & Chr(34) & Left(sParentWindowText, sParentWindowLen) & Chr(34) & ")"
    62.             End If
    63.             Debug.Print Replace(Replace(printStr, Chr(34) & Chr(34) & ",", "0&,"), Chr(34) & Chr(34), "vbNullString")
    64.        
    65.         Next
    66.         If parentCount = 0 Then
    67.             printStr = "hWnd = FindWindow(" & Chr(34) & Left(sClassName, sClassLen) & Chr(34) & ", " & Chr(34) & Left(sWindowText, sWindowLen) & Chr(34) & ")"
    68.         Else
    69.             printStr = "hWnd = FindWindowEx(hWnd, 0&, " & Chr(34) & Left(sClassName, sClassLen) & Chr(34) & ", " & Chr(34) & Left(sWindowText, sWindowLen) & Chr(34) & ")"
    70.         End If
    71.         Debug.Print Replace(Replace(printStr, Chr(34) & Chr(34) & ",", "0&,"), Chr(34) & Chr(34), "vbNullString")
    72.         Debug.Print "#################################################" & vbCrLf & "-------------------------" & vbCrLf
    73.        
    74.     End If
    75. End If
    76.  
    77. End Sub

    Note, this will always take the first window, if it finds two windows with the same class+caption with the same parents.
    If needed, you could add a loop to keep checking the windows until the right hWnd was reached, and I might do this some time in the future
    Last edited by Rob123; Dec 14th, 2006 at 10:42 PM.

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