PDA

Click to See Complete Forum and Search --> : Classnames & Findwindowex


eng70640
Feb 19th, 2001, 02:58 AM
Is there a way to find out the class name of active window & then use this class name & findwindowex api to obtain the handles of the child windows of the active window ?

Lord Orwell
Feb 19th, 2001, 03:14 AM
If it is just the active window you are interested in, then Declare Function GetActiveWindow& Lib "user32" ()
dim hwnd as long
hwnd = GetActiveWindow()
will give you the handle of the active window. Use this instead of the class name in the findwindowex function. It will accept either.

eng70640
Feb 19th, 2001, 06:19 AM
Hi Orwell , my code is shown below whereby i only used
the caption to find the child handles using findwindowex api . But the returned value is 0 . Can you guide me why this is so ?

fish = GetForegroundWindow()

x = GetActiveWindowTitle(True)
hWnd_MD = FindWindowEx(0, 0, vbNullString, x)
' in module
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public sClassname As String
Public r As String
Public fish As Long
Public j As Long
Public 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
Public Declare Function GetForegroundWindow Lib "user32.dll" () As Long



' Returns the title of the active window.
' if GetParent = true then the parent window is
' returned.
Public Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String



fish = GetForegroundWindow


If ReturnParent Then
Do While fish <> 0
j = fish
fish = GetParent(fish)
Loop

fish = j
End If

GetActiveWindowTitle = GetWindowTitle(fish)
End Function
Public Function GetWindowTitle(ByVal hwnd As Long) As String
Dim l As Long
Dim s As String

l = GetWindowTextLength(hwnd)
s = Space(l + 1)

GetWindowText hwnd, s, l + 1

GetWindowTitle = Left$(s, l)
End Function

Lord Orwell
Feb 20th, 2001, 02:13 AM
fish = GetForegroundWindow()
minnow = findwindowex(fish, 0, vbnullstring, vbnullstring)
will store the handle to a child window in minnow.
I am not sure why you were using the caption, but in your code sample, it is not necessary.
This code will find all of the child windows:
fish = GetForegroundWindow()
minnow = 0
do
minnow = findwindowex(fish, minnow, vbnullstring, vbnullstring)
debug.print minnow
loop until minnow = 0

crispin
Feb 20th, 2001, 03:47 AM
Lord Orwell

Can you really use the hWnd instead of the classname? Thanks for that - tip of the day!! I never saw that documented anywhere - cool - that makes my life a lot easier. :)

Feb 20th, 2001, 02:52 PM
Why not just use the EnumChildWindows API function.

Lord Orwell
Feb 20th, 2001, 11:11 PM
[edited to correct my errors :))
How about a code sample? I have never needed that api call, and would like to see an example of it used. :)

eng70640
Feb 21st, 2001, 03:37 AM
Hi Orwell , my code which i want to save all the child handles to a dynamic array cannot work saying subscript out of range , can you help me ?

Public lHwnd() As long
Public iCount As Integer

iCount = 0
ReDim Preserve lHwnd(iCount)
fish = GetForegroundWindow()
hWnd_MD = FindWindowEx(fish, 0, vbNullString, vbNullString)
Do
hWnd_MD = FindWindowEx(fish, hWnd_MD, vbNullString, vbNullString)
lHwnd(iCount) = hWnd_MD
iCount = iCount + 1
Loop Until hWnd_MD = 0

------------------------------------------------------------------------
Originally posted by Lord Orwell
fish = GetForegroundWindow()
minnow = findwindowex(fish, 0, vbnullstring, vbnullstring)
will store the handle to a child window in minnow.
I am not sure why you were using the caption, but in your code sample, it is not necessary.
This code will find all of the child windows:
fish = GetForegroundWindow()
minnow = 0
do
minnow = findwindowex(fish, minnow, vbnullstring, vbnullstring)
debug.print minnow
loop until minnow = 0

Feb 21st, 2001, 02:29 PM
Here's an example of using EnumChildWindows.

Add to a Module.

Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public lHwnd() As Long
Public iCount As Integer

Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
iCount = iCount + 1
ReDim Preserve lHwnd(iCount)
lHwnd(iCount) = hwnd
EnumChildProc = 1
End Function


Add to a Form

Private Sub Command1_Click()
EnumChildWindows hwnd, AddressOf EnumChildProc, 0

For i = 0 To UBound(lHwnd)
Debug.Print lHwnd(i)
Next i
End Sub

Lord Orwell
Feb 21st, 2001, 08:10 PM
Megatron thanks for the code sample. It answers a question or two i had :)

Eng70640 The problem with your code is you are redimming the size of the array to zero. I can see what you were trying to do. You should have put the redim inside of the loop.
Working code(i hope :)):
Public lHwnd(0) As long
Public iCount As Integer

iCount = 0
ReDim Preserve lHwnd(iCount)
fish = GetForegroundWindow()
hWnd_MD = FindWindowEx(fish, 0, vbNullString, vbNullString)
Do
iCount = iCount + 1
hWnd_MD = FindWindowEx(fish, hWnd_MD,
vbNullString, vbNullString)
if hWnd_MD > 0 then
Redim Preserve lHwnd(iCount)
lHwnd(iCount) = hWnd_MD
end if
Loop Until hWnd_MD = 0

eng70640
Feb 22nd, 2001, 01:37 AM
But when i put the redim inside of the loop , there is error by the vb compiler . :)

Originally posted by Lord Orwell
Megatron thanks for the code sample. It answers a question or two i had :)

Eng70640 The problem with your code is you are redimming the size of the array to zero. I can see what you were trying to do. You should have put the redim inside of the loop.
Working code(i hope :)):
Public lHwnd(0) As long
Public iCount As Integer

iCount = 0
ReDim Preserve lHwnd(iCount)
fish = GetForegroundWindow()
hWnd_MD = FindWindowEx(fish, 0, vbNullString, vbNullString)
Do
iCount = iCount + 1
hWnd_MD = FindWindowEx(fish, hWnd_MD,
vbNullString, vbNullString)
if hWnd_MD > 0 then
Redim Preserve lHwnd(iCount)
lHwnd(iCount) = hWnd_MD
end if
Loop Until hWnd_MD = 0

eng70640
Feb 22nd, 2001, 01:39 AM
Moreover , i have deleted the line 'icount = 0' & there is still error .:)

Lord Orwell
Feb 22nd, 2001, 11:06 PM
heh i declared the array incorrectly.
To be able to resize an array, lHwnd has to be declared
like this:
dim lHwnd() as long
not like this:
dim lHwnd(0) as long