Results 1 to 2 of 2

Thread: FindWindow API helper

Threaded View

  1. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: FindWindow API helper

    I came up with something similar to this for a scripting language I was messing around with. It outputted the code on a single line in manner that my parser would understand, so I've modified it to output VB code instead - it can handle windows with the same classname & caption:
    VB Code:
    1. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
    2.     ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    3.  
    4. Private Declare Function GetAncestor Lib "user32.dll" ( _
    5.     ByVal hwnd As Long, ByVal gaFlags As Long) As Long
    6.  
    7. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
    8.     ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    9.  
    10. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
    11.     ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    12.  
    13. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" ( _
    14.     ByVal hwnd As Long) As Long
    15.    
    16. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    17.  
    18. Private Const GA_PARENT As Long = 1
    19.  
    20. Private Function WindowCode(ByVal lhWnd As Long) As String
    21.     Dim lParent As Long, lChild As Long, lLen As Long, lCount As Long
    22.     Dim sClass As String, sCaption As String
    23.  
    24.     Do
    25.         ' ClassName
    26.         sClass = Space$(256)
    27.         lLen = GetClassName(lhWnd, sClass, 256)
    28.         sClass = Left$(sClass, lLen)
    29.  
    30.         ' Caption
    31.         sCaption = String$(GetWindowTextLength(lhWnd), vbNullChar)
    32.         GetWindowText lhWnd, sCaption, Len(sCaption) + 1
    33.  
    34.         ' Find Window
    35.         lParent = GetAncestor(lhWnd, GA_PARENT)
    36.        
    37.         lCount = 1: lChild = 0
    38.         WindowCode = "lhWnd = lChild: lChild = 0" & vbCrLf & WindowCode
    39.         Do
    40.             lChild = FindWindowEx(lParent, lChild, sClass, sCaption)
    41.             WindowCode = "lChild = FindWindowEx(lhWnd, lChild, """ & sClass & """, """ & sCaption & """)" & vbCrLf & WindowCode
    42.             If lChild = lhWnd Then Exit Do
    43.             lCount = lCount + 1
    44.         Loop Until lChild = lhWnd
    45.         lhWnd = lParent
    46.     Loop Until lhWnd = GetDesktopWindow
    47.    
    48.     WindowCode = "Dim lChild As Long, lhWnd As Long" & vbCrLf & vbCrLf & WindowCode & vbCrLf & "Debug.Print Hex(lhWnd)"
    49. End Function
    example output (the "=" button on the calculator):
    Code:
    Dim lChild As Long, lhWnd As Long
    
    lChild = FindWindowEx(lhWnd, lChild, "SciCalc", "Calculator")
    lhWnd = lChild: lChild = 0
    lChild = FindWindowEx(lhWnd, lChild, "Button", "=")
    lhWnd = lChild: lChild = 0
    
    Debug.Print Hex(lhWnd)
    you could use GetAsyncKeyState to get the hWnd to pass to the function as Rob showed above, or you could also use the MouseDown / MouseUp events of a picturebox, e.g.:
    VB Code:
    1. Private Type POINTAPI
    2.     X As Long
    3.     Y As Long
    4. End Type
    5.  
    6. Private Declare Function ClientToScreen Lib "user32" ( _
    7.     ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    8.  
    9. Private Declare Function WindowFromPoint Lib "user32" ( _
    10.     ByVal xPoint As Long, ByVal yPoint As Long) As Long
    11.  
    12. Private bFinding As Boolean
    13.  
    14. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    15.     If Button = vbLeftButton Then
    16.         Me.MousePointer = vbSizePointer
    17.         bFinding = True
    18.     End If
    19. End Sub
    20.  
    21. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    22.     Dim pt As POINTAPI
    23.     If bFinding Then
    24.         pt.X = X \ Screen.TwipsPerPixelX
    25.         pt.Y = Y \ Screen.TwipsPerPixelY
    26.         ClientToScreen Picture1.hwnd, pt
    27.         Debug.Print WindowCode(WindowFromPoint(pt.X, pt.Y))
    28.     End If
    29.     Me.MousePointer = vbNormal
    30.     bFinding = False
    31. End Sub
    Left-Click on the picturebox, hold it down and then move the mouse over the window of interest, release the left button and it'll find the window's hWnd (just like Spy++)

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