View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default APIs FindWindow and GetWindowText

When it comes to Toolbar windows I suspect there's more to finding them than
merely the issue of 'Level' and Child parent relations (levels & parentage
are not necessarily the same). Eg if you pass the classname "MsoCommandBar"
your result will probably be the same (subject only one instance of Excel).

Try undocking your main toolbar "Worksheet Menu Bar". I expect you will now
find that FindWindow will return a value, but not the same value as returned
with your use of GetWindowText. If you re-dock the toolbar FindWindow will
probably continue to return the same non-zero value.

I don't know your overall objective but whatever it is, if you want to
read/write to a toolbar window, you will probably find you need to know
whether the toolbar is currently docked before knowing which hWin to use.

You may find the routine posted in this link useful. Paste all the code into
a new module, run Test() to call MySpy (discard Sub Test2 at the bottom).

http://groups.google.co.uk/group/mic...mming/msg/60ef
24582e3ca73f?dmode=source&hl=en

or if the above has wrapped -
http://tinyurl.com/23hqs2

Regards
Peter T


"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

=----