|
-
Dec 14th, 2006, 05:46 PM
#2
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:
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
Private Declare Function GetAncestor Lib "user32.dll" ( _
ByVal hwnd As Long, ByVal gaFlags As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" ( _
ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Const GA_PARENT As Long = 1
Private Function WindowCode(ByVal lhWnd As Long) As String
Dim lParent As Long, lChild As Long, lLen As Long, lCount As Long
Dim sClass As String, sCaption As String
Do
' ClassName
sClass = Space$(256)
lLen = GetClassName(lhWnd, sClass, 256)
sClass = Left$(sClass, lLen)
' Caption
sCaption = String$(GetWindowTextLength(lhWnd), vbNullChar)
GetWindowText lhWnd, sCaption, Len(sCaption) + 1
' Find Window
lParent = GetAncestor(lhWnd, GA_PARENT)
lCount = 1: lChild = 0
WindowCode = "lhWnd = lChild: lChild = 0" & vbCrLf & WindowCode
Do
lChild = FindWindowEx(lParent, lChild, sClass, sCaption)
WindowCode = "lChild = FindWindowEx(lhWnd, lChild, """ & sClass & """, """ & sCaption & """)" & vbCrLf & WindowCode
If lChild = lhWnd Then Exit Do
lCount = lCount + 1
Loop Until lChild = lhWnd
lhWnd = lParent
Loop Until lhWnd = GetDesktopWindow
WindowCode = "Dim lChild As Long, lhWnd As Long" & vbCrLf & vbCrLf & WindowCode & vbCrLf & "Debug.Print Hex(lhWnd)"
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:
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function ClientToScreen Lib "user32" ( _
ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" ( _
ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private bFinding As Boolean
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
Me.MousePointer = vbSizePointer
bFinding = True
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim pt As POINTAPI
If bFinding Then
pt.X = X \ Screen.TwipsPerPixelX
pt.Y = Y \ Screen.TwipsPerPixelY
ClientToScreen Picture1.hwnd, pt
Debug.Print WindowCode(WindowFromPoint(pt.X, pt.Y))
End If
Me.MousePointer = vbNormal
bFinding = False
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++)
Last edited by bushmobile; Dec 18th, 2006 at 07:19 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|