Sign in to follow this  
Followers 0
Decipher

_ToggleSystemTrayState with support for specific icons

5 posts in this topic

#1 ·  Posted (edited)

I was helping another user the other day And decided To make a UDF so here it is. Let me know what ya think. ;)

#include-once
#include <GuiListView.au3>

Global Const $SW_HIDENA = 2, $SW_HIDENOACTIVE = 2

; Examples are below.

; #FUNCTION# ====================================================================================================================
; Name ..........: _ToggleSystemTrayState
; Description ...: Hide/Show System Tray Icons by Name or Set the State for all Icons
; Syntax ........: _ToggleSystemTrayState([$vTrayItems = ""[, $iStartSearchIndex = 0[, $iSystemTrayState = 0[, $iDebug = 0]]]])
; Parameters ....: $vTrayItems           - [optional] A variant value. Default is "".
;                $iStartSearchIndex - [optional] An integer value. Default is 0. Set this to -1 to search from end to start.
;                $iSystemTrayState - [optional] An integer value. Default is 0.
;                $iDebug             - [optional] An integer value. Default is 0. Set this to either @SW_SHOW or 1 to see dialogs and debug information.
; Return values .: None
; Author ........: Decipher
; Modified ......:
; Remarks .......: Tested only on XP
;
; $iSystemTrayState can be one of the following:
; @SW_SHOW == ALWAYS_SHOW == 5
; @SW_HIDE == ALWAYS_HIDE == 0
; $SW_HIDENA == HIDE WHEN INACTIVE == 2
;
; $vTrayItems can be a string for a specific icon or an array for multiple icons
; There is no error checking implemented but feel free to change it to your needs.
;
; Related .......:
; Link ..........:
; Example .......: Yes, The examples need to be uncommented to run. Remove the ( ;- ) prefix on the example to be ran.
; ===============================================================================================================================


    ;;;; Set all icons to hide when inactive. ;;;;
;-    _ToggleSystemTrayState($SW_HIDENA) ; Example #1

    ;;;; Hide all system tray icons. ;;;;
;-    _ToggleSystemTrayState(@SW_HIDE) ; Example #2

    ;;;; Show two specific icons. ;;;;
;-    Local $aIcons[2] = ["Local Area Connection", "Volume"] ; Example #3
;-    _ToggleSystemTrayState($aIcons, 0, @SW_SHOW)

    ;;;; Hide a specific icon. ;;;;
;-    _ToggleSystemTrayState("Volume", 0, @SW_HIDE) ; Example #4

    ;;;; Show all system tray icons. ;;;;
;-    _ToggleSystemTrayState(@SW_SHOW) ; Example #5

    ;;;; Hide all system tray icons but show the dialogs aka debug mode and log the names of the icons hidden. ;;;;
;-    _ToggleSystemTrayState("", 0, @SW_HIDE, 1) ; Example #6


Func _ToggleSystemTrayState($vTrayItems = "", $iStartSearchIndex = 0, $iSystemTrayState = 0, $iDebug = 0)
    If IsInt($vTrayItems) Then
        $iSystemTrayState = $vTrayItems
        $vTrayItems = ""
    EndIf
    Local Const $sOPTIONS_DIALOG_PROCESS = "rundll32.exe shell32.dll, Options_RunDLL 1"
    Local Const $sTASKBAR_PROPERTIES_TITLE = "[TITLE:Taskbar and Start Menu Properties; CLASS:#32770; INSTANCE:1]"
    Local Const $sCUSTOMIZE_BUTTON = "[ClassnameNN:Button10]", $sSENDKEYS = "!c"
    Local Const $sTASKBAR_CUSTOMIZE_TITLE = "[TITLE:Customize Notifications; CLASS:#32770; INSTANCE:1]"
    Local Const $sTRAYICONS_LISTVIEW = "[CLASS:SysListView32; INSTANCE:1]"
    Local Const $sLISTVIEW_COMBOX_CONTROL = "[CLASS:ComboBox; INSTANCE:1]"
    Local Const $sTASKBAR_CHILD_OK_BUTTON = "[CLASS:Button; INSTANCE:2]"
    Local Const $iRUNDLL32 = Run($sOPTIONS_DIALOG_PROCESS)
    WinWait($sTASKBAR_PROPERTIES_TITLE, "", 2000)
    Local Const $hTASKBAR_DIALOG = WinGetHandle($sTASKBAR_PROPERTIES_TITLE)
    WinSetState($hTASKBAR_DIALOG, "", $iDebug)
    ControlSend($hTASKBAR_DIALOG, "", ControlGetHandle($hTASKBAR_DIALOG, "", $sCUSTOMIZE_BUTTON), $sSENDKEYS)
    WinWait($sTASKBAR_CUSTOMIZE_TITLE, "", 2000)
    $hTASKBAR_CHILD_DIALOG = WinGetHandle($sTASKBAR_CUSTOMIZE_TITLE)
    WinSetState($hTASKBAR_CHILD_DIALOG, "", $iDebug)
    Local Const $hDIALOG_LISTVIEW = ControlGetHandle($hTASKBAR_CHILD_DIALOG, "", $sTRAYICONS_LISTVIEW)
    Local $iListViewItem = 0, $hComboBox_Instance = 0, $aTrayIcon[1], $iTrayIcon, $iSkipTask = False, $sTrayItemText
    If IsArray($vTrayItems) Or $vTrayItems <> "" Then
        If Not IsArray($vTrayItems) Then
            $aTrayIcon[0] = $vTrayItems
        Else
            $aTrayIcon = $vTrayItems
        EndIf
        For $i = 0 To UBound($aTrayIcon, 1) - 1 Step 1
            $iTrayIcon = _GUICtrlListView_FindInText($hDIALOG_LISTVIEW, $aTrayIcon[$i], $iStartSearchIndex)
            If $iDebug Then
                ConsoleWrite("Tray Icon: " & @TAB & _GUICtrlListView_GetItemText($hDIALOG_LISTVIEW, $iTrayIcon) & @CRLF)
            EndIf
            _GUICtrlListView_SetItemFocused($hDIALOG_LISTVIEW, $iTrayIcon)
            $hComboBox_Instance = ControlGetHandle($hTASKBAR_CHILD_DIALOG, "", $sLISTVIEW_COMBOX_CONTROL)
            ControlSend($hTASKBAR_CHILD_DIALOG, "", $hDIALOG_LISTVIEW, "{SPACE}")
            For $iLoopIndex = 1 To 3 Step 1
                ControlSend($hTASKBAR_CHILD_DIALOG, "", $hComboBox_Instance, "{DOWN}")
            Next
            If $iSystemTrayState <> 5 Then
                ControlSend($hTASKBAR_CHILD_DIALOG, "", $hComboBox_Instance, "{UP}")
                If $iSystemTrayState = 2 Then
                    ControlSend($hTASKBAR_CHILD_DIALOG, "", $hComboBox_Instance, "{UP}")
                EndIf
            EndIf
        Next
        $iSkipTask = True
    EndIf
    While WinExists($hTASKBAR_CHILD_DIALOG)
        If $iSkipTask Then
            ExitLoop
        EndIf
        If $iListViewItem >= _GUICtrlListView_GetItemCount($hDIALOG_LISTVIEW) + 1 Then
            ExitLoop
        EndIf
        $iListViewItem += 1
        ControlFocus($hTASKBAR_CHILD_DIALOG, "", $hDIALOG_LISTVIEW)
        If $iDebug Then
            $sTrayItemText = _GUICtrlListView_GetItemText($hDIALOG_LISTVIEW, $iListViewItem)
            If $sTrayItemText <> "" Then
                ConsoleWrite("Tray Icon: " & @TAB & $sTrayItemText & @CRLF)
            EndIf
        EndIf
        $hComboBox_Instance = ControlGetHandle($hTASKBAR_CHILD_DIALOG, "", $sLISTVIEW_COMBOX_CONTROL)
        For $i = 1 To 3 Step 1
            ControlSend($hTASKBAR_CHILD_DIALOG, "", $hComboBox_Instance, "{DOWN}")
        Next
        If $iSystemTrayState <> 5 Then
            ControlSend($hTASKBAR_CHILD_DIALOG, "", $hComboBox_Instance, "{UP}")
            If $iSystemTrayState = 2 Then
                ControlSend($hTASKBAR_CHILD_DIALOG, "", $hComboBox_Instance, "{UP}")
            EndIf
        EndIf
        ControlSend($hTASKBAR_CHILD_DIALOG, "", $hDIALOG_LISTVIEW, "{DOWN}")
    WEnd
    ControlSend($hTASKBAR_CHILD_DIALOG, "", $sTASKBAR_CHILD_OK_BUTTON, "{ENTER}")
    WinClose($hTASKBAR_DIALOG)
    If ProcessExists($iRUNDLL32) Then
        ProcessClose($iRUNDLL32)
    EndIf
EndFunc   ;==>_ToggleSystemTrayState
Edited by Decipher

Spoiler

censored.jpg

 

Share this post


Link to post
Share on other sites



JohnOne,

Do you mind elaborating as to how your implementing the UDF?

Are you just running the example? What OS version are you using?

The best of my knowledge this function works as advertised when used appropiately. ;)

Anonymous


Spoiler

censored.jpg

 

Share this post


Link to post
Share on other sites

This UDF depends on the dialog's design remain unchanged which might not be the case with version of Windows after XP. I have updated the examples where you can understand how to use them.


Spoiler

censored.jpg

 

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

  • Similar Content

    • 9252Survive
      By 9252Survive
      Hello Guys, 
      Is there a way to have Auto it Exe messages/line number when you hover over the tray icon just at it shows when you run Au3.?
      For an example, if I the script has paused due to WinWait or Sleep I could hover over the icon and it shows the line where it's at. Is there a way to achieve the same with the compiled exe? 
      Many thanks for the help! 
       
    • nacerbaaziz
      By nacerbaaziz
      Hi guys
      I have an inquiry please
      I want an example of how to add an icon file to the compiled autoit file and how to use it in GUICtrlCreateIcon
      Preferably in a separate list within the resourceFor example myIcons
      I hope my request is clear and I hope you will help me solve this problem
      Because I found an example in the help files but I did not understand the method well
    • nacerbaaziz
      By nacerbaaziz
      Hi dear
      I have a question
      about the display language of the system
      How can I get the current display language and how can I change it by autoit
      Of course, if this is possible
      Greetings to all and hope you help me
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning community!
      I was working on a script which read the content of a .ini file to retrieve the file that has to open, and, based on the value of a second key stored in the .ini file, set the window on top, minimized or maximized...
      I did something like this, but seems to not work properly...
      Could anyone please tell me what I'm doing wrong?
      Thank you  
       
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile_x64=prova.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <AutoItConstants.au3> ;#include <WinAPIEx.au3> #include <Array.au3> Local $sFileConfigurazione = @ScriptDir & "\configurazione_exe.ini" If(FileExists($sFileConfigurazione)) Then Local $aSezioneIni = IniReadSection($sFileConfigurazione, "CONFIGURAZIONE_EXE") If @error Then MsgBox($MB_ICONERROR, "Errore!", "Errore durante l'apertura del file: " & @CRLF & $sFileConfigurazione & @CRLF & "Errore: " & @error) Else ; Lancio dell'applicativo indicato nel file di configurazione Local $iPID = ShellExecute($aSezioneIni[1][1]) Local $hWnd If($iPID <> 0) Then Local $aWinList = WinList() For $i = 1 To $aWinList[0][0] If(WinGetProcess($aWinList[$i][1] = $iPID)) Then $hWnd = $aWinList[$i][1] EndIf Next Switch($aSezioneIni[2][1]) Case $aSezioneIni[2][1] = "MIN" ToolTip("MIN", 0, 0) WinSetState($hWnd, WinGetTitle($hWnd), @SW_MINIMIZE) Case $aSezioneIni[2][1] = "MAX" ToolTip("MAX", 0, 0) WinSetState($hWnd, WinGetTitle($hWnd), @SW_MAXIMIZE) Case $aSezioneIni[2][1] = "TOP" ToolTip("TOP", 0, 0) WinSetOnTop($hWnd, WinGetTitle($hWnd), $WINDOWS_ONTOP) EndSwitch EndIf EndIf EndIf And the .ini file is like this:
       
      [CONFIGURAZIONE_EXE] PercorsoExe=Here goes the path and the .exe to execute ModalitaAperturaExe=Here goes one of the value below /* TOP Shows the windows on top */ /* MIN Shows the window minimized */ /* MAX Shows the window maximized */  
    • amimemeami
      By amimemeami
      I have a simple script that switches between IE and Chrome for the default browser, I even made a custom half chrome half ie icon for it. Question is, can I have the script toggle between two icons to signify the last browser it changed to? I would have the active browser's half of the icon in color, and inactive half grey scale. As a workaround, I put link next to it, and refresh the icon cache after it runs. 
      #RequireAdmin #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=..\..\Icons\IE_Chrome.ico #AutoIt3Wrapper_Outfile=..\..\..\..\Users\dmking\Desktop\Browser Switcher.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** $sCurrentDefault = RegRead('HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice',"ProgId") if $sCurrentDefault = "IE.HTTP" Then     ChromeDefault() Else     IEDefault() EndIf ;~ Run("ie4uinit.exe -ClearIconCache") Run("C:\Windows\System32\ie4uinit.exe -ClearIconCache") Func IEDefault()     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice","Progid","REG_SZ","IE.FTP")     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice","Progid","REG_SZ","IE.HTTP")     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice","Progid","REG_SZ","IE.HTTPS")     RegWrite("HKCU\Software\Clients\StartMenuInternet","","REG_SZ","IEXPLORE.EXE") EndFunc Func ChromeDefault()     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice","Progid","REG_SZ","ChromeHTML")     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice","Progid","REG_SZ","ChromeHTML")     RegWrite("HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice","Progid","REG_SZ","ChromeHTML")     RegWrite("HKCU\Software\Clients\StartMenuInternet","","REG_SZ","Google Chrome") EndFunc