[RESOLVED] Help get child window!!!
I created a new feature for my program that will find the "Quick Query" window in the database application we use here, fill in some numbers, and run the query. My problem is that I often have 2 instances of the database application running, usually both with a "Quick Query" dialog box open. I was hoping I could use FindWindow or GetAncestor to get the parent/owner (still having a hard time with which is which) of the Quick Query dialog and then get the dialog from that parent. I can easily get the handle of the parent window titled "Record Storage Item Process Status", but when I search the child windows with FindWindowEx AND EnumChildWindows the dialog isn't found.
Appreciate your time!!!!
fEtch
Re: Help get child window!!!
is the dialog modal? If not, it may have the desktop as a parent.
Re: Help get child window!!!
Re: Help get child window!!!
modal basically boils down to this: You can't click on any other form in the program while it's being displayed. If it doesn't display this property, then it's not a child window of the other window and you won't find it the way you are searching. You would need to search for children of the desktop... use hwnd 0& as a built-in shortcut to the desktop hwnd.
Re: Help get child window!!!
Quote:
modal basically boils down to this: You can't click on any other form in the program while it's being displayed. If it doesn't display this property, then it's not a child window of the other window and you won't find it the way you are searching. You would need to search for children of the desktop... use hwnd 0& as a built-in shortcut to the desktop hwnd.
Yes, the dialog is modal. Any ideas on why it won't work?
Re: Help get child window!!!
try getting the class with a spy program and search for that instead.
Re: Help get child window!!!
I use a FindWindowLike function for those hard to find window handles.
You can find MS's version of one here, http://support.microsoft.com/kb/147659
Re: Help get child window!!!
I have all the handles and classes that I need. The problem is there's usually another Quick Query window open that's pretty much exactly the same as the other one :-\. It's gonna be hard to find a difference in them, but I'll try your suggestions.
Thank you!
Re: Help get child window!!!
One way I differ dialogs is you get the parent handles first, then when you want to send something to one of those dialogs do a FindWindowLike to get all the handles of similar named windows, then do a IsChild(parent) to find out which handle is the child of the parent you want to send something to.
Re: Help get child window!!!
the findwindow function will find every window with the same class. It just returns them one at a time. You find the first one, and pass the handle of that window as a parameter to the 2nd call. It will then find the next one. You can find them all in sequence that way.
Re: Help get child window!!!
Doesn't FindWindow only have 2 parameters, class and title?, how do you pass a handle to FindWindow?
Re: Help get child window!!!
use findwindowex i believe
Re: Help get child window!!!
Is there a way I can cycle through all the windows titled "Quick Query"?
Below is my code...I'm getting the children of the main form, but it's only cycling through the command buttons and **** on that form; it's not returning any dialogs...yet the dialog is modal...so i'm stuck in an infinite loop.
Code:
Public Function ChildTest() As Long
Dim hWndIPC As Long, hWndDialog As Long, slength As Long, r As Long
Dim strText As String
Dim i As Integer
Const GW_HWNDNEXT = 2
Const GW_CHILD = 5
i = 0
hWndIPC = FindWindow(vbNullString, "Record Storage Item Process Status")
hWndDialog = GetWindow(hWndIPC, GW_CHILD)
Do
If i > 0 Then
hWndDialog = GetWindow(hWndDialog, GW_HWNDNEXT)
End If
slength = GetWindowTextLength(hWndDialog) + 1
strText = Space(slength)
r = GetWindowText(hWndDialog, strText, slength)
Debug.Print strText
i = i + 1
Loop Until IsChild(hWndIPC, hWndDialog) <> 0 And (strText Like "Quick*") = True
ChildTest = hWndDialog
End Function
Re: Help get child window!!!
I just used "GetParent" to find the parent of the dialog (after making sure the dialog i wanted was the only one open and that my code could not mistake it for anything else) and I get the same handle i got using:
Code:
hWndIPC = FindWindow(vbnullstring, "Record Center Item Process Status")
So why is the dialog nowhere to be found if I use GetWindow to get the children of padre IPC?
Re: Help get child window!!!
Quote:
Originally Posted by fEtchboi88
Is there a way I can cycle through all the windows titled "Quick Query"?
Below is my code...I'm getting the children of the main form, but it's only cycling through the command buttons and **** on that form; it's not returning any dialogs...yet the dialog is modal...so i'm stuck in an infinite loop.
I just tried that code with Notepad and an open About Notepad dialog and it just got stuck in a loop too. I slapped some code together (probably needs tweaking) and it returns all the open About Notepad dialog handles in a list, selecting one of those hWnds returns its parent hWwnd in a msgbox.
Add a button and 2 list boxes to a form. List2 holds the dialog hwnds, clicking on them returns their parents hwnd. Change the title/class names for your needs and see if that helps any, if not sorry I wasted your time.
Code:
Option Explicit
Private WinHwnd(512) As Long
Private WindCnt As Integer
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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 GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Function FindWindowLike(ByVal hWndStart As Long, WindowText As String, Classname As String) As Long
Dim hwnd As Long
Dim sWindowText As String
Dim sClassname As String
Dim r As Long
Static level As Integer
If level = 0 Then
If hWndStart = 0 Then hWndStart = GetDesktopWindow()
End If
level = level + 1
hwnd = GetWindow(hWndStart, GW_CHILD)
Do Until hwnd = 0
Call FindWindowLike(hwnd, WindowText, Classname)
sWindowText = Space$(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
sClassname = Space$(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = Left(sClassname, r)
If (sWindowText Like WindowText) And (sClassname Like Classname) Then
WinHwnd(WindCnt) = hwnd
WindCnt = WindCnt + 1
End If
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
level = level - 1
End Function
Private Sub Command1_Click()
Dim i As Integer
List1.Clear
List2.Clear
WindCnt = 0 ' reset count & clear array of Hwnds.
Erase WinHwnd()
' find all open notepads with title text starting,
' with the word "Untitled" (note: case sensitive!)
FindWindowLike 0, "Untitled*", "Notepad"
For i = 0 To WindCnt - 1 ' save parents to list1
List1.AddItem WinHwnd(i)
Next i
WindCnt = 0 ' reset count & clear array
Erase WinHwnd()
FindWindowLike 0, "About Notepad", "*" ' find any open "About Notepad" windows
For i = 0 To WindCnt - 1 ' add hWnds to list2
List2.AddItem WinHwnd(i)
Next i
End Sub
Private Sub List2_Click()
MsgBox "My parent hWnd is " & GetParent(List2.List(List2.ListIndex))
End Sub