6

In Windows 7, I had an AutoHotKey script that would automatically Right-Click on a tray icon.

#Include %A_Scriptdir%\TrayIcon.ahk
TrayIcon_Button("CCC.exe", "R")

Which used the TrayIcon.ahk library from FanaticGuru's post.

This worked just fine on Windows 7, but no longer works on Windows 10.

Is there a way to right click on a TrayIcon in an AutoHotKey script on Windows 10?

Here is the TrayIcon_Button function from the library. I refrained from posting the entire library since it is fairly long.

; ----------------------------------------------------------------------------------------------------------------------
; Function .....: TrayIcon_Button
; Description ..: Simulate mouse button click on a tray icon.
; Parameters ...: sExeName - Executable Process Name of tray icon.
; ..............: sButton  - Mouse button to simulate (L, M, R).
; ..............: bDouble  - True to double click, false to single click.
; ..............: index    - Index of tray icon to click if more than one match.
; ----------------------------------------------------------------------------------------------------------------------
TrayIcon_Button(sExeName, sButton := "L", bDouble := false, index := 1)
{
    Setting_A_DetectHiddenWindows := A_DetectHiddenWindows
    DetectHiddenWindows, On
    WM_MOUSEMOVE      = 0x0200
    WM_LBUTTONDOWN    = 0x0201
    WM_LBUTTONUP      = 0x0202
    WM_LBUTTONDBLCLK = 0x0203
    WM_RBUTTONDOWN    = 0x0204
    WM_RBUTTONUP      = 0x0205
    WM_RBUTTONDBLCLK = 0x0206
    WM_MBUTTONDOWN    = 0x0207
    WM_MBUTTONUP      = 0x0208
    WM_MBUTTONDBLCLK = 0x0209
    sButton := "WM_" sButton "BUTTON"
    oIcons := {}
    oIcons := TrayIcon_GetInfo(sExeName)
    msgID  := oIcons[index].msgID
    uID    := oIcons[index].uID
    hWnd   := oIcons[index].hWnd
    if bDouble
        PostMessage, msgID, uID, %sButton%DBLCLK, , ahk_id %hWnd%
    else
    {
        PostMessage, msgID, uID, %sButton%DOWN, , ahk_id %hWnd%
        PostMessage, msgID, uID, %sButton%UP, , ahk_id %hWnd%
    }
    DetectHiddenWindows, %Setting_A_DetectHiddenWindows%
    return
}
SimCard
  • 305
  • 3
  • 13

3 Answers3

6

I tested it on Windows 10. It was not working for the icons hidden under overflow window, although it was working perfectly for the visible icons.

Update these three lines in TrayIcon_GetInfo() for a quick solution

For key, sTray in ["Shell_TrayWnd","NotifyIconOverflowWindow"]
SendMessage, 0x418, 0, 0, ToolbarWindow32%idxTB%, ahk_class %sTray%   ; TB_BUTTONCOUNT
SendMessage, 0x417, A_Index - 1, pRB, ToolbarWindow32%idxTB%, ahk_class %sTray%   ; TB_GETBUTTON

Replace them with

For key, sTray in ["NotifyIconOverflowWindow", "Shell_TrayWnd"]
SendMessage, 0x418, 0, 0, ToolbarWindow32%key%, ahk_class %sTray%   ; TB_BUTTONCOUNT
SendMessage, 0x417, A_Index - 1, pRB, ToolbarWindow32%key%, ahk_class %sTray%   ; TB_GETBUTTON

Update: To awesome users who have upgraded to Windows 1607, it is broken again :)

To make it work again in Windows 10 1607, first follow those last rules. After that replace these with:

SendMessage, 0x418, 0, 0, ToolbarWindow32%key%, ahk_class %sTray%   ; TB_BUTTONCOUNT
SendMessage, 0x417, A_Index - 1, pRB, ToolbarWindow32%key%, ahk_class %sTray%   ; TB_GETBUTTON

with

if ("Shell_TrayWnd" == sTray) {
    SendMessage, 0x418, 0, 0, ToolbarWindow32%idxTB%, ahk_class %sTray%   ; TB_BUTTONCOUNT
} else if ("NotifyIconOverflowWindow" == sTray) {
    SendMessage, 0x418, 0, 0, ToolbarWindow32%key%, ahk_class %sTray%   ; TB_BUTTONCOUNT
}
if ("Shell_TrayWnd" == sTray) {
    SendMessage, 0x417, A_Index - 1, pRB, ToolbarWindow32%idxTB%, ahk_class %sTray%   ; TB_GETBUTTON
} else if ("NotifyIconOverflowWindow" == sTray) {
    SendMessage, 0x417, A_Index - 1, pRB, ToolbarWindow32%key%, ahk_class %sTray%   ; TB_GETBUTTON
}

Note: I don't think any of these changes are backward compatible.

  • Doesn't work anymore with Windows 10 1709. Any idea how to fix this? Maybe from the [latest version](https://autohotkey.com/boards/viewtopic.php?p=9186#p9186) of the library as well? – Otiel Oct 20 '17 at 09:35
  • @Otiel That's why I love the Windows update. I am going to update windows this weekend. Hopefully I will try to figure out a solution by monday. –  Oct 20 '17 at 13:33
  • @Otiel Hey I forgot to reply. I upgraded Windows and it's still working fine for me. –  Oct 29 '17 at 21:20
  • Huh, that's weird. Thanks for letting me know, I'll try again. Could you please share the entire library code you're using? – Otiel Oct 31 '17 at 08:41
  • Thanks, it works better with your version (I had a more recent version according to the header date). But it doesn't work for every software... Tried with `keepass.exe`, all good. Tried with `slack.exe`, it doesn't work, even though the AHK Window Spy displays `ahk_exe slack.exe` for the Slack window. :( – Otiel Nov 02 '17 at 12:50
  • @Otiel How to run it? I copied the pastebin.com/jWkrKauS in an test.ahk and ran it, but nothing happen. – JinSnow Oct 06 '19 at 15:20
  • 2
    @JinSnow This is a library to interact with tray icons (btw, I'm using a newer version that you can find on https://pastebin.com/1iSj6yUn). To simulate a click on a tray icon, you need to call the `TrayIcon_Button` function (check its signature, there are several arguments available - left click / right click...) in your `test.ahk` file. – Otiel Oct 06 '19 at 20:11
  • Thanks! Runing you last code, with this at the end of my script does work `TrayIcon_Button("dimmer.exe","R",false, 1)` it even find it with a wrong index number! (Adding a msgbox at the end doesn't work because it opens and closes the menu very quickly) – JinSnow Oct 07 '19 at 08:04
  • @Otiel This library rocks! – skygate Jul 13 '20 at 03:57
  • As of Oct 2020, the TrayIcon.ahk library from FanaticGuru's post works with both Windows 7 and Windows 10 (version 1803) with no further modifications. Pretty nice. – JonathanDavidArndt Oct 13 '20 at 01:03
1

Try the official method of running the AHK script as admin by adding this code to the beginning:

if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • I tried your suggestion, but it made no difference. The problem doesn't seem to be with the macro not having permission. – SimCard Aug 07 '15 at 21:00
0

Perhaps ... there is ... "an easier way" ???

"Back in the day" I used to use Sean's TrayIcons ...

I haven't felt like "deciphering" all the new/intricate scripts that (have) followed Sean's ...

So, I finally "focused" on some Trial&Error based on AHK Boards tmplinshi's Post

Following tmplinshi's guide, I derived (that) I needed message code 40022 posted to (hidden) window (class) NirSoft_VolumouseMsg100-x64

So, the (simple/direct) coded-solution became/is:

DetectHiddenWindows ON
PostMessage, 0x111, 40022, , , ahk_class NirSoft_VolumouseMsg100-x64 ahk_pid %VolMpid%
DetectHiddenWindows OFF

I encountered one (little) "wrinkle" in that the above would not work for some (unknown) reason IF the script was displaying SplashText when doing the PostMessage ⁉️

I (have) found that tmplinshi's method can replace WinMenuSelectItem as well

AENwww
  • 1
  • 1