Click to See Complete Forum and Search --> : THe Usual: FindWindowEx
Rh0ads
Feb 5th, 2001, 07:25 AM
I know you guys have probably been asked this before, but i'd like to ask it now. What is the best was to get a handle? i use FindWindow to get the handle of the parent, and others have told me that i can use FindwindowEx to find children, but when i use FindWindowEx, it simply returns a 0 instead of a handle. What am I doing wrong?
Are you using the FindWindowEx API function the correct way?
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 Sub Command1_Click()
Dim hWin As Long
hWin = FindWindowEx(0, 0, "SciCalc", "Calculator")
If hWin <> 0 Then
MsgBox "Calculator's handle: " & hWin
Else
Msgbox "Calculator not found"
End If
End Sub
Rh0ads
Feb 5th, 2001, 11:26 AM
I already know that, what i was asking was how i use any function to get the handle of a child, like the division button on the calculator
FindWindowEx works from left to right, top to bottom.
So here is the code to find the division button.
Dim scicalc As Long, button As Long
scicalc = FindWindow("scicalc", vbNullString)
button = FindWindowEx(scicalc, 0&, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
Matthew Gates:
First of all, you don't need that much code to find the divide button. you can do it in one line like this:
button = FindWindowEx(SciCalc, 0&, "button", "/")
Secondly, you don't need both FindWindow and FindWindowEx, because FindwindowEx can do the same thing.
scicalc = FindwindowEx(0, 0, "SciCalc", "Calculator")
Rh0ads
Feb 5th, 2001, 05:37 PM
Above:
code:--------------------------------------------------------------------------------Dim scicalc As Long, button As Long
scicalc = FindWindow("scicalc", vbNullString)
button = FindWindowEx(scicalc, 0&, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)--------------------------------------------------------------------------------
How do i know how many times to put that line? Is there an easy way or is it trial and error?
It can be done much simpler by using this line.
button = FindWindowEx(SciCalc, 0&, "button", "/")
Rh0ads
Feb 6th, 2001, 07:22 PM
okay, if i passed the last string, "/", as a null string would it still return the handle?
For some reason, I cannot get Megatron's code to work.
But using the code I gave...
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
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 PostMessage Lib "user32" _
Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg _
As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const MK_LBUTTON = &H1
Private Sub Command1_Click()
Dim scicalc As Long, button As Long
scicalc = FindWindow("scicalc", vbNullString)
button = FindWindowEx(scicalc, 0&, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
button = FindWindowEx(scicalc, button, "button", vbNullString)
Msgbox "Clicking button with the hwnd of: " & button
PostMessage button, WM_LBUTTONDOWN, MK_LBUTTON, 0
PostMessage button, WM_LBUTTONUP, MK_LBUTTON, 0
End Sub
Rh0ads
Feb 7th, 2001, 02:12 PM
Okay, the way above, i like better. It uses FindWindow and FindwindowEx, one last question. The code above has :
button = FindWindowEx(scicalc, button, "button", vbNullString)
over and over again, how do i find out how many times to put that line, or is it trial and error?
Use this code. It's much shorter.
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 Sub Command1_Click()
Dim hCalc As Long
hCalc = FindWindowEx(0, 0, "SciCalc", "Calculator")
btn = FindWindowEx(hCalc, 0, "Button", "/")
'Display hWnd
MsgBox btn
End Sub
Originally posted by Megatron
Use this code. It's much shorter.
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 Sub Command1_Click()
Dim hCalc As Long
hCalc = FindWindowEx(0, 0, "SciCalc", "Calculator")
btn = FindWindowEx(hCalc, 0, "Button", "/")
'Display hWnd
MsgBox btn
End Sub
Msgbox btn 'returns 0 with Calculator opened
Well, since you posted again Megatron, before I tried the code, I was thinking, it probably works for him, since he has the magikal touch (even though I did the same exact way and it wasn't working).
But it's not working either.
Originally posted by Rh0ads
Okay, the way above, i like better. It uses FindWindow and FindwindowEx, one last question. The code above has :
button = FindWindowEx(scicalc, button, "button", vbNullString)
over and over again, how do i find out how many times to put that line, or is it trial and error?
Refer to this line.
Originally posted by Matthew Gates
FindWindowEx works from left to right, top to bottom.
Well...top to bottom, left to right :rolleyes:.
Logically it makes sense. The last argument of FindwindowEx specifies the WindowName (text). "/" is the name of the divide button.
Rh0ads
Feb 7th, 2001, 03:00 PM
Okay here's some code:
Dim a&
Dim Button&
a& = FindWindow("ThunderRT6FormDC", vbNullString)
Button& = FindWindowEx(a&, 0, "ThunderRT6CommandButton", vbNullString)
Button& = FindWindowEx(a&, Button&, "ThunderRT6CommandButton", vbNullString)
MsgBox Button&
Call Click_Button(Button)
I have the Click_Button sub working fine, but when the message box comes up that is supossed to tell me the Handle of the button, it just gives me a zero. What am i doing wrong now?
You can shorten your code by omitting the use for findWindow, because you can do the exact same thing with FindwindowEx (which you're using anyways).
FindWindow(0, 0, "ThunderRT6FormDC", vbNullString)
Secondly, you don't need to use that many lines of code to find the button you want. Simply replace vbNullString, with the TEXt of the button. For example, if the Text is "Command1" then you would use:
Button& = FindWindowEx(a&, 0, "ThunderRT6CommandButton", "Command1")
That also may be the reason why it's not working, because since you may have more than one class of "ThunderRT6FormDC" open, windows s only searching one of them and not the other, thus you need to be more specific by specifiying the name of the window as well.
a& = FindWindow("ThunderRT6FormDC", "My_Window_Caption")
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.