Sign in to follow this  
Followers 0
MisterBates

Can't find my taskbar icon position

3 posts in this topic

I'm trying to write a script that sits in the taskbar and changes the taskbar icon based on where the mouse pointer is in relation to the taskbar icon. Here's a cut-down version of some of the code showing how I'm trying to find the taskbar icon position (using Auto3Lib):

#include <A3LToolbar.au3>

Const $sScriptName = StringTrimRight(@ScriptName, 4)
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sScriptName = ' & $sScriptName & @crlf & '>Error code: ' & @error & @crlf);### Debug Console
Const $sVersionNum = "0.0.0.1"; Release.Patch
Const $sScriptNameVersion = $sScriptName & " (" & $sVersionNum & ")"

AutoItWinSetTitle($sScriptName)
AutoItSetOption("TrayMenuMode", 1)  
$hActive = TrayCreateItem("Active")
TrayCreateItem("")
$hExit = TrayCreateItem("Exit")
TraySetState(1)
TraySetToolTip($sScriptName)

MoveEyes()
while 1
    $nMsg = TrayGetMsg()
    Select
        case $nMsg = $hExit
            ExitLoop
    EndSelect
WEnd

Exit

Func MoveEyes()
; Find the taskbar icon area
    $hWnd   = ControlGetHandle("[CLASS:Shell_TrayWnd]", "", "Notification Area")
    $nWndPos = ControlGetPos("[CLASS:Shell_TrayWnd]", "", "Notification Area")
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nWndPos = (' & $nWndPos[0] & ',' & $nWndPos[1] & ',' & $nWndPos[2] & ',' & $nWndPos[3] & ')' & @crlf);### Debug Console
    $nCount = _Toolbar_ButtonCount($hWnd)
    local $nIconPos = 0
; Find the icon within the taskbar icon area
    For $nIconNo = 1 to $nCount
        $nCommand = _Toolbar_IndexToCommand($hWnd, $nIconNo)
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nCommand = ' & $nCommand & @crlf);### Debug Console
        $sText  = _Toolbar_GetButtonText ($hWnd, $nCommand)
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sText = ' & $sText & @crlf & '>Error code: ' & @error & @crlf);### Debug Console
        If StringInStr($sText, $sScriptName) Then
            $nIconPos = _Toolbar_GetItemRect($hWnd, $nIconNo)
            ExitLoop
        Endif
    Next
 ; Icon position is relative to taskbar icon area - adjust
    if IsArray($nIconPos) Then
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nIconPos = (' & $nIconPos[0] & ',' & $nIconPos[1] & ',' & $nIconPos[2] & ',' & $nIconPos[3] & ')' & @crlf);### Debug Console
        For $iI = 0 to 1
            $nIconPos[$iI] += $nWndPos[$iI]
            $nIconPos[$iI + 2] += $nWndPos[$iI]
        Next
    Else; Didn't find taskbar icon, return taskbar icon area
        $nIconPos = $nWndPos
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nIconPos = (' & $nIconPos[0] & ',' & $nIconPos[1] & ',' & $nIconPos[2] & ',' & $nIconPos[3] & ')' & @crlf);### Debug Console
    EndIf
EndFunc

My problem is that the debug output from this shows that my icon (with tooltip set to the script name) is not being found. I can hover over the toolbar icon and see the correct tooltip, but the Auto3Lib function to return the button text is returning a blank string.

Any ideas what's going wrong? Does this work on your machine? Any other way people can think of to find the position of my taskbar icon?

Share this post


Link to post
Share on other sites



I'm trying to write a script that sits in the taskbar and changes the taskbar icon based on where the mouse pointer is in relation to the taskbar icon. Here's a cut-down version of some of the code showing how I'm trying to find the taskbar icon position (using Auto3Lib):

#include <A3LToolbar.au3>

Const $sScriptName = StringTrimRight(@ScriptName, 4)
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sScriptName = ' & $sScriptName & @crlf & '>Error code: ' & @error & @crlf);### Debug Console
Const $sVersionNum = "0.0.0.1"; Release.Patch
Const $sScriptNameVersion = $sScriptName & " (" & $sVersionNum & ")"

AutoItWinSetTitle($sScriptName)
AutoItSetOption("TrayMenuMode", 1)  
$hActive = TrayCreateItem("Active")
TrayCreateItem("")
$hExit = TrayCreateItem("Exit")
TraySetState(1)
TraySetToolTip($sScriptName)

MoveEyes()
while 1
    $nMsg = TrayGetMsg()
    Select
        case $nMsg = $hExit
            ExitLoop
    EndSelect
WEnd

Exit

Func MoveEyes()
; Find the taskbar icon area
    $hWnd   = ControlGetHandle("[CLASS:Shell_TrayWnd]", "", "Notification Area")
    $nWndPos = ControlGetPos("[CLASS:Shell_TrayWnd]", "", "Notification Area")
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nWndPos = (' & $nWndPos[0] & ',' & $nWndPos[1] & ',' & $nWndPos[2] & ',' & $nWndPos[3] & ')' & @crlf);### Debug Console
    $nCount = _Toolbar_ButtonCount($hWnd)
    local $nIconPos = 0
; Find the icon within the taskbar icon area
    For $nIconNo = 1 to $nCount
        $nCommand = _Toolbar_IndexToCommand($hWnd, $nIconNo)
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nCommand = ' & $nCommand & @crlf);### Debug Console
        $sText  = _Toolbar_GetButtonText ($hWnd, $nCommand)
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sText = ' & $sText & @crlf & '>Error code: ' & @error & @crlf);### Debug Console
        If StringInStr($sText, $sScriptName) Then
            $nIconPos = _Toolbar_GetItemRect($hWnd, $nIconNo)
            ExitLoop
        Endif
    Next
; Icon position is relative to taskbar icon area - adjust
    if IsArray($nIconPos) Then
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nIconPos = (' & $nIconPos[0] & ',' & $nIconPos[1] & ',' & $nIconPos[2] & ',' & $nIconPos[3] & ')' & @crlf);### Debug Console
        For $iI = 0 to 1
            $nIconPos[$iI] += $nWndPos[$iI]
            $nIconPos[$iI + 2] += $nWndPos[$iI]
        Next
    Else; Didn't find taskbar icon, return taskbar icon area
        $nIconPos = $nWndPos
                                                                    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $nIconPos = (' & $nIconPos[0] & ',' & $nIconPos[1] & ',' & $nIconPos[2] & ',' & $nIconPos[3] & ')' & @crlf);### Debug Console
    EndIf
EndFunc

My problem is that the debug output from this shows that my icon (with tooltip set to the script name) is not being found. I can hover over the toolbar icon and see the correct tooltip, but the Auto3Lib function to return the button text is returning a blank string.

Any ideas what's going wrong? Does this work on your machine? Any other way people can think of to find the position of my taskbar icon?

Buttons in a Toolbar are zero based. Change your loop constructor to this:

For $nIconNo = 0 to $nCount - 1

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

Buttons in a Toolbar are zero based. Change your loop constructor to this:

For $nIconNo = 0 to $nCount - 1
Aaaaaaarrrrrgh! You know when you stare and stare at code thinking this must be obvious ... :rolleyes:

Thanks!

Share this post


Link to post
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
Sign in to follow this  
Followers 0