Reputation: 305
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
}
Upvotes: 6
Views: 4706
Reputation: 1
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 😎
Upvotes: 0
Reputation:
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.
Upvotes: 6
Reputation: 73576
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
}
Upvotes: 1