Mbee

[Solved] New "sys tray" access problems under Windows 10

10 posts in this topic

#1 ·  Posted (edited)

One of my GUI applications requires that a particular third-party application not only be running, but be present in the "sys tray" (aka Taskbar). When this 3p app is active but not in the systray/taskbar, it is not ready for proper use, and I must inform the user about this.

Here is a code snippet that used to work fine under Windows 7:

;
; #FUNCTION# =========================================================================================================
; Name...........: _Get_Systray_BtnIndex
; Description ...: Determines if an appllication with the specified title is running in the Systen Tray
;                   and if so, returns the relative index number of it's button on the tray.
; Syntax.........: _Get_Systray_BtnIndex($iTitleText)
; Parameters ....: $iTitleText  -> The (tool-tip) title of the desired application
; Return values .: Success - Returns the index number of the desired appliction button, 1 being the first
;                  Failure - Returns 0 if desired application is not found in the System Tray
;                             Sets @error as follows:
;                                0:  Normal return
;                               -1: System Tray was empty (i.e., no appllications running the tray)
;                               -2: No System Tray was found
; Remarks .......; Setting any parameter to -1 leaves the current value unchanged
;                  Setting the $iStyle parameter to 'Default' resets ALL parameters to default values <<<<<<<<<<<<<<<<<<<<<<<
;                  Setting any other parameter to "Default" only resets that parameter
; Author ........: Poster "war59312", and code was found at URL:
;                       http://www.autoitscript.com/forum/topic/40550-check-if-program-is-running-under-system-tray/#entry902746
;                   Modified by mjb on 26-June-2014 to return @extended error code rather than using MsgBox() to report errors.
;
Func _Get_Systray_BtnIndex($sToolTipTitle)
    ; Find systray handle
    Local $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:1]')
    If @error Then
        Return SetError( -2, 0, 0 )
    EndIf

    ; Get systray item count
    Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    If $iSystray_ButCount = 0 Then
        Return SetError( -1, 0 , 0 )
    EndIf

    ; Look for wanted tooltip
    For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1
        If StringInStr(_GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber), $sToolTipTitle) > 0 Then ExitLoop
    Next

    If $iSystray_ButtonNumber = $iSystray_ButCount Then
        Return 0                        ; Desired application is NOT running in the System Tray
    Else
        Return $iSystray_ButtonNumber   ; Application was found in the SysTray
    EndIf
EndFunc     ;==> _Get_Systray_BtnIndex
;

 

But what's happening under Windows 10 is that the "_GUICtrlToolbar_ButtonCount()" always returns 1, even though there are 10-14 items visible in the Task Bar.  Unsurprisingly, the single item returned has nothing to do with the 3'rd party application in question.

As you probably know, Microsoft made significant changes to the systray/taskbar since Windows 7 (or at least by Windows 10). I spent quite a while searching MSDN for some other way to accomplish what I need, but I must be too stupid to figure it out.

Please help.   Thanks!

 

Edited by Mbee

Share this post


Link to post
Share on other sites



You're not looking in the NotifyIconOverflowWindow window for more icons.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
6 hours ago, BrewManNH said:

You're not looking in the NotifyIconOverflowWindow window for more icons.

Thanks for your reply, but I'm afraid I have no idea how to do that.  Can you tell me, please?

I looked around for help, and I found and then tried using the "Systray UDF" by @tuapeand modified by @Erik Pilsits posted here: _systray udf.

I chose it because if you set the $iWin parameter to 2, apparently the suggestion is that it is supposed to "look in the NotifyIconOverflowWindow for more icons".  But when I call "_SysTrayIconCount(2)", it always returns zero icons even though there are actually 10-14.

Please advise.  Thanks!

Share this post


Link to post
Share on other sites

Run this small example:

#include <Array.au3>
#include <GuiToolBar.au3>

$sSearchtext='Lautsprecher: 100%'   ;change to your needs
$iButton=Get_SysTray_IconText($sSearchtext)
MsgBox (64,'Searched Button','Button: '&$iButton&@CRLF&'Instance: '&@extended)

Func Get_SysTray_IconText($sSearch)
    Local $hSysTray_Handle
    For $i = 1 To 99
        ; Find systray handles
        $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:' & $i & ']')
        If @error Then
            ;MsgBox(16, "Error", "System tray not found")
            ExitLoop
        EndIf

        ; Get systray item count
        Local $iSysTray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
        If $iSysTray_ButCount = 0 Then
            ;MsgBox(16, "Error", "No items found in system tray")
            ContinueLoop
        EndIf

        Local $aSysTray_ButtonText[$iSysTray_ButCount]

        ; Look for wanted tooltip
        For $iSysTray_ButtonNumber = 0 To $iSysTray_ButCount - 1
            If $sSearch= _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSysTray_ButtonNumber) Then _
            Return SetError(0, $i, $iSysTray_ButtonNumber)
        Next
    Next
    Return SetError(1, -1, -1)

EndFunc   ;==>Get_SysTray_IconText


Exit

it returns:

  • succes: the ButtonNumber and in @extended the instance of the toolbarwindow.
  • failur: -1 @error -1 and @extended -1
1 person likes this

Share this post


Link to post
Share on other sites

Try this, it looks at both the visible system tray icons and the overflow (hidden) icons.

#include <GuiToolBar.au3>

Sleep(1000)
MsgBox(0, "", Get_SysTray_IconCount())

Func Get_SysTray_IconCount() ; http://www.autoitscript.com/forum/topic/157933-hinttray-tip-how-to-get-text/?p=1145366

    ; Find systray handle for "User Promoted Notification Area"
    Local $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:1]')
    If @error Then
        MsgBox(16, "Error", "System tray not found")
    EndIf
    ; Find systray handle for "Overflow Notification Area" i.e. hidden icons
    Local $hSysTray_Handle_Hidden = ControlGetHandle('[Class:NotifyIconOverflowWindow]', '', '[Class:ToolbarWindow32;Instance:1]')
    If @error Then
        MsgBox(16, "Error", "Overflow Notification Area not found")
    EndIf

    ; Get systray icon count
    Local $iSysTray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    If $iSysTray_ButCount = 0 Then
        MsgBox(16, "Error", "No items found in system tray")
        Return ""
    EndIf

    ; Get hidden systray icon count

    $iSysTray_ButCount += _GUICtrlToolbar_ButtonCount($hSysTray_Handle_Hidden)

    Return $iSysTray_ButCount

EndFunc   ;==>Get_SysTray_IconCount

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thanks to you both for trying to help me out!!

@AutoBert: I got the failure return from your test code: Button -1  and Instance -1.

@BrewManNH: The output was only 1, which is way wrong.  So it appears the Overflow didn't count after all.

 

If it matters (and for all I know it does), I've tweaked my Win 10 Pro settings. For example, I'm using the Classic start menu and Classic Explorer settings, and I've used two different Win 10 tweakers: (1): WinAero tweaker and (2): Ultimate Windows Tweaker v4.2.1

I'm about to try the two tests again with the Classic stuff disabled.  If the results are any different, I'll post back to let you both know.

Thanks again!

Share this post


Link to post
Share on other sites

Well, even though I said I'd post if I got different results with all the Classic stuff disabled, I figured I'd post to confirm that there were no differences.  I got the same erroneous results...

Share this post


Link to post
Share on other sites

The script I posted showed me that I had 13 icons. This is on Windows 10, Enterprise x64. The script works, you're doing something wrong, or one of the programs you're using is causing it to fail.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
15 minutes ago, BrewManNH said:

The script I posted showed me that I had 13 icons. This is on Windows 10, Enterprise x64. The script works, you're doing something wrong, or one of the programs you're using is causing it to fail.

Well, I figured it out...  What I did was to use Au3Info_x64 and then pointed to the systray/taskbar.  It turned out that "[Class:ToolbarWindow32;Instance:1]" was irrelevant and had an incorrect count.  However, Instance:2 showed all 14 taskbar items!

@AutoBert's code correctly covered all instances, and so it would have worked fine except for my mistake, which was that I didn't enter the full exactly correct string for his "$sSearchtext" parameter.  When I revised his code to use a substring search instead of a full exact search, it worked exactly right!

Problem solved.

Thanks again to you both! :)

 

Share this post


Link to post
Share on other sites
51 minutes ago, Mbee said:

@AutoBert's code correctly covered all instances, a

nice to read. :)

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

  • Similar Content

    • ModemJunki
      By ModemJunki
      Hello,
      In Windows 10 PowerShell, one can do this to change the metric for a NIC in Windows 10:
      Get-NetAdapter | Where-Object -FilterScript {$_.InterfaceAlias -Eq "Ethernet 2"} | Set-NetIPInterface -InterfaceMetric 2 I know I can script the above PowerShell line (and it works!), but I wanted to try something I hadn't done before after looking into jguinch's most excellent Network configuration UDF. I wanted to make use of the SetIPConnectionMetric method in the WMI classes. There is an example VBscript here but this is not for Windows 10. Using AutoIT would also give better control over capturing error return codes than with PowerShell.
      But I cannot get my script to work! The return from SetIPConnectionMetric() is 0, which would indicate success. Yet the change does not happen. I also tried WMI methods using .put_ but this fails.
      Anyone more experienced than I have ideas to make this work?
      #RequireAdmin _SetNicInterfaceMetric2("Ethernet 2", "2") Func _SetNicInterfaceMetric2($NIC_NAME, $METRIC) Local $s_setIndx = 0 $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2") $colNICItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & $NIC_NAME & "'", "WQL") If IsObj($colNICItems) Then For $objItem In $colNICItems $s_nicIndex = $objItem.Index Next ConsoleWrite("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = '" & $s_nicIndex & "'" & @CRLF) $colNAC = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = '" & $s_nicIndex & "'", "WQL") If IsObj($colNAC) Then For $objNetCard In $colNAC If $METRIC <> $objNetCard.IPConnectionMetric Then ConsoleWrite("Metric was set to " & $objNetCard.IPConnectionMetric & ". Setting to " & $METRIC & "." & @CRLF) $s_isSet = $objNetCard.SetIPConnectionMetric($METRIC) ConsoleWrite("SetIPConnectionMetric Result = " & $s_isSet & @CRLF) Else ConsoleWrite("Metric is already set to " & $METRIC & @CRLF) EndIf Next EndIf EndIf EndFunc ;==>_SetNicInterfaceMetric2  
    • mlazovjp
      By mlazovjp
      OK, this one is baffling me and my coworkers.
      I created an AutoIt script with Windows 10 1607 (or maybe 1511) that would do a RunWait "DisplaySwitch.exe /clone" to have both monitors display the same content.  It worked without issue.  Then I upgraded a couple of weeks ago to Windows 10 1703 (Creators Update).  Now when I run the script, it executes everything before and after the RunWait command but the display properties never change.  I used variables to capture the result of the RunWait command and the value of @error.  RunWait returns 0, @error returns 1 (though I don't know what that means exactly).
      I started over with a two-line AutoIt script which attempts to run DisplaySwitch.exe and nothing happens.  I modified the RunWait command to run C:\windows\system32\DisplaySwitch.exe but nothing happens.  So, I created a batch file which just attempts to run C:\windows\system32\DisplaySwitch.exe followed by a pause statement so I could read the results.  It reports "'C:\WINDOWS\System32\DisplaySwitch.exe' is not recognized as an internal or external command, operable program or batch file.".
      So I modified the batch file to perform a directory listing of C:\Windows\system32\*.exe .  It generates a list of 337 files, but if you look through the list, DisplaySwitch.exe is missing.  I then modified the batch file to perform a recursive search for DisplaySwitch.exe from the root of C:\ and it eventually finds this single file: C:\Windows\WinSxS\amd64_microsoft-windows-displayswitch_31bf3856ad364e35_10.0.15063.0_none_fdd58a325d4a2de2\displayswitch.exe
      I can run that file from the batch file and it works fine.  If I run the batch file directly rather than through an AutoIt script, it works fine and it lists DisplaySwitch.exe in C:\Windows\system32 .  
      I have compiled the AutoIt script using v3.3.14.2 and in desperation, I even compiled it with autoit-v3.3.15.0-beta-setup.exe with the same results.  I also tried adding #RequireAdmin with the same results.  If I look at the security properties of the file I cannot find anything which implies that a script could not access it.  I also notice that if I open a command prompt and do the same directory listing of all .exe files in C:\windows\system32 it lists 660 files, compared to only 337 files when that command is run through my AutoIt script.  I can also report that when I made these two files available for download through Google Drive to my colleague running Windows 10 1511, Smart Filter and Symantec both complained about the files, but we bypassed the warning and they executed properly (i.e. DisplaySwitch.exe was visible in C:\windows\system32 and it switched to Clone Mode).  My other colleague with 1703 has the same problem as me where DisplaySwitch.exe is invisible to scripts running from within an AutoIt script.
      switch.au3
      switch.cmd
    • rg20
      By rg20
      Greetings all,
       
      I have an app that runs fine in windows 7 but not windows 10.
      issue : when entering the filename to save, it enters it into the saveAs dialog box, but Office still tries to save as "Diagram1" or "Presentation1"
       
      $FileError = "" MonitorProcess2("VISIO") _DebugOut("Visio Done") MonitorProcess2("POWERPNT") _debugOut("Powerpoint Done") CloseSave($FileSavePathName, $SaveAsWindow) _DebugOut("Saved and closed Powerpoint") _VSO_DocumentSave($objVisioFile) while not _VSO_DocumentSaved($objVisioFile) WEnd _VSO_VisioClose($objVisioFile) _DebugOut("Saved and closed Visio") else _DebugOut("Filename Does not exist = "& $FilePathName) EndIf the monitor process2 just makes sure the processes are complete before trying to save the files.
       
      The closeSave function is below, but since I am opening the file with the proper name, this is not an issue
      func CloseSave($SaveFile, $SaveAsWindow) _DebugOut("Save File ") _DebugOut($SaveFile) send("!{F4}") send ("!S") $title = WinGetTitle("[ACTIVE]") _DebugOut("WINDOW - current window is Powerpoint to enter filename " & $title) while not ($title = $SaveAsWindow) $title = WinGetTitle("[ACTIVE]") wend ControlSetText ($SaveAsWindow,"","[CLASS:Edit; INSTANCE:1]",$SaveFile) send ("!S") $title = WinGetTitle("[ACTIVE]") while not StringInStr($title,"Visio") $title = WinGetTitle("[ACTIVE]") wend EndFunc  
    • brodillo
      By brodillo
      Hello, I've experience using automation anywhere. Now I want to learn Auto It for my automatization projects.
      I've tried to implement an example using calculator program in Windows 10.
      When I used au3Info program to identify  an escefific zone in the calc window , au3info always return the same advanced class  [CLASS:ApplicationFrameInputSinkWindow; INSTANCE:1] 
      Is there a restriction for windows 10?
      See my little script
       
      Run("Calc.exe")
      WinWaitActive("Calculadora")
      WinActive("Calculadora")
      $old="0"
      $new="1020"
      $controlNumber="[CLASS:ApplicationFrameInputSinkWindow; INSTANCE:1]"
      ControlSetText("Calculadora","",$controlNumber,$new)
      ControlSend("Calculadora","",$controlNumber,$new)
       
      I've tried using controlSetText and ControlSend.
       
      Thank you for your help
       
    • DLS
      By DLS
      I have a working script that changes the core affinity and process priority of multiples of a specific application I have running.  
      I have that part figured out.

      I would like to make a little modification to it.

      Windows10 introduced virtual desktops.  I am trying to have different core affinity and priority of processes on the visible and non-visible desktops.  
      What I need is a bool function that could be described as IsWindowOnCurrentDesktop($hWnd).  I have searched the winAPI.au3 but I do not believe anything like that exists built it.
      I have searched multiple places before asking for help. I found a Microsoft supplied example of the function I need using C#, but I am unfamiliar with C# to a degree that I cannot port the system call over.
      https://blogs.msdn.microsoft.com/winsdk/2015/09/10/virtual-desktop-switching-in-windows-10/
      Help is appreciated but not expected.  Thanks in advanced.   In the meantime I will be learning C# syntax and class structure.