Find all buttons or statics of a parten hWnd
I am trying to get the caption of a class called static off of another app. There is more than one of static type class on the parent. Is there some way I can cycle through each "static" and get the caption from it? Or maybe is there someway that I can specify a partial caption and class "static" and then just get a single match? Thanks!
Re: Find all buttons or statics of a parten hWnd
I think that if you pass the previous hwnd of the object in the FindWindowEx API (I assume you're using it), you get the next one. I'll have to check though to be sure :)
Re: Find all buttons or statics of a parten hWnd
OK, something along these lines? I tested it with a form and 3 textboxes and it worked :
VB Code:
Option Explicit
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 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 Sub Command1_Click()
Dim h As Long
Dim hChild As Long
Dim sChildText As String
h = FindWindow(vbNullString, "Form1")
Do
hChild = FindWindowEx(h, hChild, "ThunderTextBox", vbNullString)
If hChild <> 0 Then
sChildText = String(GetWindowTextLength(hChild) + 1, Chr$(0))
GetWindowText hChild, sChildText, Len(sChildText)
Debug.Print sChildText
End If
Loop While hChild <> 0
End Sub
Re: Find all buttons or statics of a parten hWnd
If you know the control id (you can find it using MS Spy++) you can use this API to get the
handle of it directly (and thus the caption) instead of looping.
VB Code:
Private Declare Function GetDlgCtrlID Lib "user32.dll" (ByVal hwnd As Long) As Long
Re: Find all buttons or statics of a parten hWnd
Quote:
Originally Posted by RobDog888
If you know the control id (you can find it using MS Spy++) you can use this API to get the
handle of it directly (and thus the caption) instead of looping.
VB Code:
Private Declare Function GetDlgCtrlID Lib "user32.dll" (ByVal hwnd As Long) As Long
You wouldn't happen to have an example for that? I couldn't find it in the API Guide :)
Re: Find all buttons or statics of a parten hWnd
My code ex is at work :( But I think I made a mistake (I can admit it :D). The API works reverse
of how I described it (I think I have a bit of Dyslexia). You would need to enumerate through
the static windows checking if the id matches. This API is mostly used for when the caption
is a nullstring and you have several nullstring captions of the same window class. :blush:
Re: Find all buttons or statics of a parten hWnd
Quote:
Originally Posted by RobDog888
My code ex is at work :( But I think I made a mistake (I can admit it :D). The API works reverse
of how I described it (I think I have a bit of Dyslexia). You would need to enumerate through
the static windows checking if the id matches. This API is mostly used for when the caption
is a nullstring and you have several nullstring captions of the same window class. :blush:
That makes more sence since the handle is passed to the function ;)
Re: Find all buttons or statics of a parten hWnd
manavo11, I tried your code to list captions of all buttons on a msgbox from another app - worked PERFECTLY - thanks!
I was able to automatically click the "OK" button. But now what if I need to see the caption/text of the msgbox first, can that be done?
I tried playing with your line of code:
VB Code:
hChild = FindWindowEx(h, hChild, "Button", vbNullString)
...by replacing "Button" with "Label", "Caption", or whatever... none returned me the value of the msgbox caption.
Any idea?
Re: Find all buttons or statics of a parten hWnd
Quote:
Originally Posted by Krass
manavo11, I tried your code to list captions of all buttons on a msgbox from another app - worked PERFECTLY - thanks!
I was able to automatically click the "OK" button. But now what if I need to see the caption/text of the msgbox first, can that be done?
I tried playing with your line of code:
VB Code:
hChild = FindWindowEx(h, hChild, "Button", vbNullString)
...by replacing "Button" with "Label", "Caption", or whatever... none returned me the value of the msgbox caption.
Any idea?
I'm glad you found it useful :) Now for your specific problem, you need to search for "Static", not label or anything like that :
VB Code:
hChild = FindWindowEx(h, hChild, "Static", vbNullString)
:D
Re: Find all buttons or statics of a parten hWnd
This code works good:
VB Code:
hChild = FindWindowEx(h, hChild, "Button", vbNullString)
But how come it traps buttons ONLY from an IE msgbox? ("Proceed with entry submission, OK/CANCEL")... I want to use this method on one my own Vb application, which contains textboxes and buttons... But it doesn't seem to see any of them...
Any idea?
(my form has a button called "Command1")
Thanks
Re: Find all buttons or statics of a parten hWnd
Do you have the right window title in this line?
VB Code:
h = FindWindow(vbNullString, "Form1")
I have actually only tested it with VB messageboxes, so I know for a fact it works :)