This code should do the job:
Option Explicit
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 Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Function FindWindowHwndLike(hWndStart As Long, _
ClassName As String, _
WindowTitle As String, _
Level As Long, _
lHolder As Long) As Long
'finds the first window where the class name starts with ClassName
'and where the Window title starts with WindowTitle, returns Hwnd
'-----------------------------------------------------------------
Dim hWnd As Long
Dim sWindowTitle As String
Dim sClassName As String
Dim r As Long
Static bFound As Boolean
If Level = 0 Then
bFound = False
End If
If bFound Then
Exit Function
End If
'Initialize if necessary. This is only executed
'when level = 0 and hWndStart = 0, normally
'only on the first call to the routine.
'----------------------------------------------
If Level = 0 Then
If hWndStart = 0 Then
hWndStart = GetDesktopWindow()
End If
End If
'Increase recursion counter
'--------------------------
Level = Level + 1
'Get first child window
'----------------------
hWnd = GetWindow(hWndStart, GW_CHILD)
Do While hWnd 0 And bFound = False
'Search children by recursion
'----------------------------
lHolder = FindWindowHwndLike(hWnd, _
ClassName, _
WindowTitle, _
Level, _
lHolder)
'Get the window text
'-------------------
sWindowTitle = Space$(255)
r = GetWindowText(hWnd, sWindowTitle, 255)
sWindowTitle = Left$(sWindowTitle, r)
'get the class name
'------------------
sClassName = Space$(255)
r = GetClassName(hWnd, sClassName, 255)
sClassName = Left$(sClassName, r)
If (InStr(1, sWindowTitle, WindowTitle, vbBinaryCompare) 0 Or _
sWindowTitle = WindowTitle) And _
(sClassName Like ClassName & "*" Or _
sClassName = ClassName) Then
bFound = True
FindWindowHwndLike = hWnd
lHolder = hWnd
Exit Function
End If
'Get next child window
'---------------------
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop
FindWindowHwndLike = lHolder
End Function
RBS
"Dave D-C" wrote in message
...
Hello,
I've had occasion to , given a Window Caption, to want the HWnd.
FindWindow works some of the time.
But my routine zSrch, using GetWindowText, searches all the
windows and finds some that FindWindow doesn't.
Can somebody explain this?
This is Excel97 on Win98. (Book1 is open)
The 1st Msgbox shows both methods working.
The 2nd Msgbox shows FindWindow doesn't find.
Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As
Long
Declare Function GetWindowTextLength Lib "user32" Alias
"GetWindowTextLengthA" ( _
ByVal hWnd As Long) As Long
Sub Sub1()
Call Sub2("Microsoft Excel - Book1") ' hWnds =
Call Sub2("Worksheet Menu Bar") ' hWnds <
End Sub
Sub Sub2(WinCaption$) ' displays hWnds
Dim hWnd1&, hWnd2&
hWnd1 = zFind(WinCaption)
hWnd2 = zSrch(WinCaption)
MsgBox WinCaption & ": " & hWnd1 & ", " & hWnd2
End Sub
Function zFind&(WinCaption$)
zFind = FindWindow(vbNullString, WinCaption)
End Function
Function zSrch&(WinCaption$) ' search all windows
Dim hWnd&, iLen1%, iLen2%, sStr$
For hWnd = 0 To 9999 ' search
iLen1 = GetWindowTextLength(hWnd)
sStr = Space$(iLen1 + 1)
iLen2 = GetWindowText(hWnd, sStr, iLen1 + 1)
If iLen1 < iLen2 Then Stop ' check
sStr = Left$(sStr, iLen1)
If sStr = WinCaption Then Exit For
Next hWnd
If hWnd = 10000 Then Stop ' window not found
' at this point hWnd has a caption of WinCaption
zSrch = hWnd
End Function
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----