View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Dave D-C[_3_] Dave D-C[_3_] is offline
external usenet poster
 
Posts: 176
Default APIs FindWindow and GetWindowText

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 =----