Jump to content

_GUICtrlToolbar_ClickButton() does not click a proper icon


JYoun
 Share

Go to solution Solved by ad777,

Recommended Posts

Hi, I starting to create some test automation script using AutoIt v3 and still novice on it.

I want to right-click an icon in system tray on Windows 10 or 11 using _GUICtrlToolbar_ClickButton(), but it does not click a proper icon.

This is my code for testing what button is clicked :

#Include <GuiToolBar.au3>
Global $hSysTray_Handle

Get_Systray_Info()
_GUICtrlToolbar_ClickButton($hSysTray_Handle, 5, "right", True)

Func Get_Systray_Info()
   ; Find systray handle
   $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', 'User Promoted Notification Area', '[Class:ToolbarWindow32;Instance:2]')
   If @error Then
      MsgBox(16, "Error", "System tray not found")
      Exit
   EndIf
   Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
   If $iSystray_ButCount = 0 Then
      MsgBox(16, "Error", "No items found in system tray")
      Exit
   EndIf
   For $iSystray_ButtonNumber = 0 To ($iSystray_ButCount-1)
      $sButtonText = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber)
      ConsoleWrite("Button Index : " & $iSystray_ButtonNumber & @CRLF)
      ConsoleWrite("Button Text : "& $sButtonText & @CRLF)
   Next

EndFunc

As for console output, index 5 is a button that I want to click. But when `_GUICtrlToolbar_ClickButton($hSysTray_Handle, 5, "right", True)` has executed, the button index 4 is clicked.

If I change the index parameter of `_GUICtrlToolbar_ClickButton` to 6, it clicks button #6. And for index parameter 4, it clicks button #3.

I suspecting that AutoIt cannot calculate width of systray buttons properly. Is there any method to define or adjust the width of buttons?

Thank you.

Link to comment
Share on other sites

  • Solution

@JYoun

this how you Sort Button by Command:

#include <GuiToolBar.au3>
;;Tested in Win 7 [Instance:1]
Global $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', 'User Promoted Notification Area', '[Class:ToolbarWindow32;Instance:1]')
_GUICtrlToolbar_SortByButton($hSysTray_Handle)
Func _GUICtrlToolbar_SortByButton($hSysTray_Handle)
    Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1
        _GUICtrlToolbar_MoveButton($hSysTray_Handle, _GUICtrlToolbar_CommandToIndex($hSysTray_Handle, $iSystray_ButtonNumber), $iSystray_ButtonNumber)
    Next
EndFunc   ;==>_GUICtrlToolbar_SortByButton

As for console output, index 5 is a button that I want to click. But when `_GUICtrlToolbar_ClickButton($hSysTray_Handle, 5, "right", True)` has executed, the button index 4 is clicked.

and this how you click Button by command:

Global $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', 'User Promoted Notification Area', '[Class:ToolbarWindow32;Instance:1]')
_GUICtrlToolbar_ClickButtonByCommand($hSysTray_Handle, 1)
Func _GUICtrlToolbar_ClickButtonByCommand($hSysTray_Handle, $Button, $Click = "right", $tp = True)
    _GUICtrlToolbar_ClickButton($hSysTray_Handle, _GUICtrlToolbar_IndexToCommand($hSysTray_Handle, _GUICtrlToolbar_CommandToIndex($hSysTray_Handle, $Button)), $Click, $tp)
EndFunc   ;==>_GUICtrlToolbar_ClickButtonByCommand

i did little fix on your script:

#include <GuiToolBar.au3>
Global $hSysTray_Handle

Get_Systray_Info()

Func Get_Systray_Info()
    ; Find systray handle
    $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', 'User Promoted Notification Area', '[Class:ToolbarWindow32;Instance:1]')
    If @error Then
        MsgBox(16, "Error", "System tray not found")
        Exit
    EndIf
    Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    If $iSystray_ButCount = 0 Then
        MsgBox(16, "Error", "No items found in system tray")
        Exit
    EndIf
    For $iSystray_ButtonNumber = 0 To ($iSystray_ButCount - 1)
        $sButtonText = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber)
        ConsoleWrite("Button Text : " & $sButtonText & @CRLF)
        ConsoleWrite("Button Index : " & $iSystray_ButtonNumber & @CRLF)

    Next

EndFunc   ;==>Get_Systray_Info

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

Link to comment
Share on other sites

  • 2 weeks later...

@Nine Thank you for reply. My testing environment is Win11, and I'll check again on Win10.

@ad777 Thank you very much for solution. Actually it does not exactly works in my environment(still clicks right next of target icon), but I got some clue from your solution - move target icon to the corner of system tray and click it using IndexToCommand and CommandToIndex. Seems works fine currently and I'll try to test in another environments.

Edited by JYoun
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...