Jump to content

Spy on toolbars, commandbars etc


junkew
 Share

Recommended Posts

Over time I made a small spy on the objects that are not default in AU3Info spied on

As in new release ticket #140 wish was added to AU3Info and I missed still some things I decided to share at least below code that easily spies on treeview, commandbars, toolbars etc. a level deeper then AU3Info does that normally takes me a lot of time to find out by hand.

Additions please share them and maybe later adds a ticket for wishes of AU3Info

usage: run the script, hover the mouse over the toolbar, commandbar you want information on, press the hotkey alt+shift+a

Somehow I don't get the text of the toolbarwindow32 whereas AU3Info does this properly on ToolsBar tab (latest version has this tab) but seems to have a bug (reported in tracker) as only instance 1 information is retrieved.

#include <GUIConstants.au3>
#include <GUIToolbar.au3> 
#include <WindowsConstants.au3>
#include <SliderConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <Winapi.au3>
#Include <GuiTreeView.au3>

#include <GuiConstantsEx.au3>
#include <GuiTab.au3>
;Some windowspy on objects not spyed on by AU3Info
;Shows information for following objects
;"Toolbar"  / "ToolbarWindow32      #Not able to get text?
;"SysTabControl32"
;"SysTreeView32"
;"MsoCommandBar"

HotKeySet("+!a", "getInfo")  ;Shift-Alt-a
HotKeySet("{ESC}", "Terminate")

While 1
    Sleep(100)
WEnd

Func Terminate()
    Exit 0
EndFunc

func getInfo()
     $aMPos = MouseGetPos()
     $sWinInfo = _WinInfoFromPoint($aMPos[0], $aMPos[1]) 
     consolewrite($sWinInfo)
EndFunc

;Gets window/control info from cursor pos, returns formatted string
Func _WinInfoFromPoint($nX, $nY)
    Local $tStrBuff, $pStrBuff, $aRet, $hWnd, $hOwnerWnd, $sClassName, $sOwnerClass, $sWinText
    local $tbCount, $tbText, $i, $aRect[4], $mPos[2], $wPos[4]
    local $iCount
    
    $tStrBuff = DllStructCreate("char[100]")
    $pStrBuff = DllStructGetPtr($tStrBuff)
    $aRet = DllCall("user32.dll", "hwnd", "WindowFromPoint", "uint", $nX, "uint", $nY)
    $hWnd = $aRet[0]
    $aRet = DllCall("user32.dll", "int", "GetClassName", "hwnd", $hWnd, "ptr", $pStrBuff, "int", 100)
    $sClassName = DllStructGetData($tStrBuff, 1)
    DllStructSetData($tStrBuff, 1, "")
    DllCall("user32.dll", "int", "GetWindowText", "hwnd", $hWnd, "ptr", $pStrBuff, "int", 100)
;~  DllCall("user32.dll", "int", "SendMessage", "hwnd", $hWnd, "uint", $WM_GETTEXT, "uint", 100, "ptr", $pStrBuff)
    $sWinText = DllStructGetData($tStrBuff, 1)
    DllStructSetData($tStrBuff, 1, "")  
    $aRet = DllCall("user32.dll", "hwnd", "GetAncestor", "hwnd", $hWnd, "uint", 2) ;$GA_ROOT = 2
    $hOwnerWnd = $aRet[0]
    $aRet = DllCall("user32.dll", "int", "GetClassName", "hwnd", $hOwnerWnd, "ptr", $pStrBuff, "int", 100)
    $sOwnerClass = DllStructGetData($tStrBuff, 1)
    DllStructSetData($tStrBuff, 1, "")

;If we are over a toolbar
if (stringinstr($sClassname,"Toolbar") > 0) Then
    $iCount = _GUICtrlToolbar_ButtonCount($hWnd)
    for $i=0 to $icount -1
        ;Determine rectangle and check matching area
        $aRect = _GUICtrlToolbar_GetButtonRect ($hWnd, _GUICtrlToolbar_IndexToCommand($hWnd, $i))
        $mPos=mousegetpos()
        $wPos=wingetpos($hWnd)
        if (($mPos[0] - $wPos[0]) > $arect[0] ) and (($mPos[0] - $wPos[0]) < $arect[2] ) _
        And (($mPos[1] - $wPos[1]) > $arect[1] ) and (($mPos[1] - $wPos[1]) < $arect[3] ) Then
            $tbText = "[Index: "     & $i & _ 
                      " CommandID: " & _GUICtrlToolbar_IndexToCommand($hWnd, $i) & _
                      " text: "      & _GUICtrlToolbar_GetButtonText($hWnd, $i) & _
                      _GUICtrlToolbar_GetString($hWnd,$i) & " ]"
;~          GUICtrlSetData($mainLabel,$tbText)
;~          consolewrite($tbText)
        endif   
    next
endIf
if (stringinstr($sClassname,"SysTabControl32") > 0) Then
    $iCount=_GUICtrlTab_GetItemCount($hWnd)
;~  GUICtrlSetData($mainLabel,$iCount)
;~  consolewrite($iCount)
    for $i=0 to $icount -1
        ;Determine rectangle and check matching area
        $aRect = _GUICtrlTab_GetItemRect ($hWnd,  $i)
        $mPos=mousegetpos()
        $wPos=wingetpos($hWnd)
        if (($mPos[0] - $wPos[0]) > $arect[0] ) and (($mPos[0] - $wPos[0]) < $arect[2] ) _
        And (($mPos[1] - $wPos[1]) > $arect[1] ) and (($mPos[1] - $wPos[1]) < $arect[3] ) Then
            $tbText = "[Index: "     & $i & _ 
                      " text: "      & _GUICtrlTab_GetItemText($hWnd, $i) & " ]"
;~          GUICtrlSetData($mainLabel,$tbText)
;~          consolewrite($tbText)
        endif   
    next
EndIf
if (stringinstr($sClassname,"SysTreeView32") > 0) Then
    $iCount=_GUICtrlTreeView_GetCount($hWnd)
;~  GUICtrlSetData($mainLabel,$iCount)
;~  consolewrite($iCount)
    ;for $i=0 to $icount -1
    $wPos=wingetpos($hWnd)
    $mPos=mousegetpos()
    $tvItem=_GUICtrlTreeView_HitTestItem($hWnd, ($mPos[0] - $wPos[0]), ($mPos[1] - $wPos[1]))
    $tbText = $mpos[0] & ";" & $mpos[1] & ";" & $wpos[0] & ";" & $wpos[1] & ";" & $wpos[2] & ";" & $wpos[3]
    $tbText = _GUICtrlTreeView_GetText($hWnd, $tvItem)
    
;       if (($mPos[0] - $wPos[0]) > $arect[0] ) and (($mPos[0] - $wPos[0]) < $arect[2] ) _
;       And (($mPos[1] - $wPos[1]) > $arect[1] ) and (($mPos[1] - $wPos[1]) < $arect[3] ) Then
;           $tbText = "[Index: "     & $i & _ 
;                     " text: "      & _GUICtrlTab_GetItemText($hWnd, $i) & " ]"
;~          GUICtrlSetData($mainLabel,$tbText)
;~ consolewrite($tbText)
;       endif   
;   next
EndIf

if (stringinstr($sClassname,"MsoCommandBar") > 0) Then
    $wPos=wingetpos($hWnd)
    $mPos=mousegetpos()
    $tbText = $wpos[0] & ";" & $wpos[1] & ";" & $wpos[2] & ";" & $wpos[3]
    
    $msoAppObjClass = "[CLASS:" & $sOwnerClass & "]"
    $msoAppObjName="" 
    if $sOwnerClass="OpusApp" then      $msoAppObjName="word.application" 
    if $sOwnerClass="XLMAIN" then       $msoAppObjName="Excel.application" 
    if $sOwnerClass="OMain" then        $msoAppObjName="Access.application" 
    if $sOwnerClass="FrontPageExplorerWindow40" then $msoAppObjName="frontpage.application" 
    if $sOwnerClass="rctrl_renwnd32" then $msoAppObjName="Outlook.application" 
    if stringinstr($sOwnerClass,"FrameClass")>0 then $msoAppObjName="PowerPoint.application" 
    
;~ ;Get the MSOffice application (assumes its running only 1 instance)
    $msoApp=objGet("",$msoAppObjName)

    if @error then 
        Msgbox (0,"Test","Error Getting an active MS Object. Error code: " & hex(@error,8))
        exit
    endif

    $myBar = getMSOBar($msoApp, $sWinText)

    if $myBar <> -1 Then
    $iCount=$mybar.controls.count 
;~      consolewrite("Mouse at:" & $mpos[0] & ";" & $mpos[1]  & @crlf)
        for $j=1 to $myBar.controls.count

            if ($mpos[0]>$mybar.controls($j).left) and ($mpos[0] < ($mybar.controls($j).left +$mybar.controls($j).width)) and _
                ($mpos[1]>$mybar.controls($j).top) and ($mpos[1] < ($mybar.controls($j).top +$mybar.controls($j).height)) Then
                consolewrite("match button " & $j & " at "  & $mybar.controls($j).left & ":" )
                consolewrite($mybar.controls($j).top & " " &  $mybar.controls($j).tooltiptext & @crlf)
                
;~          consolewrite(" width:" & $mybar.controls($j).width)
;~          consolewrite(" height:" & $mybar.controls($j).height)
            
;~          consolewrite("  ")
;~          consolewrite(" caption:" & $mybar.controls($j).caption)
;~          consolewrite(" enabled:" & $mybar.controls($j).enabled)
;~          consolewrite(" id:" & $mybar.controls($j).id)
;~          consolewrite(" caption:" & $mybar.controls($j).caption)
;~          consolewrite(" left:" & $mybar.controls($j).left)
;~          consolewrite(" top5:" & $mybar.controls($j).top)
;~          consolewrite(" width:" & $mybar.controls($j).width)
;~          consolewrite(" height:" & $mybar.controls($j).height)
;~          consolewrite(" tooltiptext:" & $mybar.controls($j).tooltiptext)
;~          consolewrite(" visible:" & $mybar.controls($j).visible)
;~          consolewrite(" descriptiontext:" & $mybar.controls($j).descriptiontext & @crlf)
            endif                   

;~      
;~      ;Some fun with the mouse
;~      if $mybar.controls($j).visible = -1 then
;~          $left=$mybar.controls($j).left + ($mybar.controls($j).width/2)
;~          $top =$mybar.controls($j).top + ($mybar.controls($j).height/2) 
;~          mousemove(int($left),int($top),5)
;~      endIf
        next

        EndIf

EndIf

Return $sWinText & @CRLF & "[ Class: " & $sClassName & "; hWnd: " & $hWnd & " " & @CRLF & $tbText & "]" & @CRLF & _
                        "( Owner: " & $sOwnerClass & "; " & $hOwnerWnd & " )" & @CRLF & " having " & $iCount & " items" & @crlf
EndFunc
                    
;The actual function for the MsoCommandBar
;$msoApp = Object reference
;$msoClass = The window class to search control in
;$ctrlName = The name (localized) of the bar
func getMSOBar($msoApp, $ctrlName)
    for $j=1 to $msoApp.commandbars.count-1
            if $msoApp.commandbars($j).namelocal=$ctrlName then
;~              consolewrite($msoApp.commandbars($j).namelocal & " Match: " & $j & @crlf )
                Return $msoApp.commandbars($j)
            endif
    next
    return -1
endfunc
Link to comment
Share on other sites

  • 3 months later...

Hey, I found the same issue where the _GUICtrlToolbar_GetButtonText was returning '' as in empty string, but not every time.

So I went in the GuiToolbar.au3 and commented out this line flagged below in the code.

Seems like this was excluding text that is one character is length, or that's my guess anyway. The code seems to work for me now.

Maybe there should be a ticket created? I'm not too well versed in that.

If $fUnicode Then
                $iBuffer = _SendMessage($hWnd, $TB_GETBUTTONTEXTW, $iCommandID)
        Else
                $iBuffer = _SendMessage($hWnd, $TB_GETBUTTONTEXTA, $iCommandID)
        EndIf

        If $iBuffer = 0 Then Return SetError(True, 0, "")
;---->  If $iBuffer = 1 Then Return SetError(False, 0, "")
        If $iBuffer <= -1 Then Return SetError(False, -1, "")
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...