在Windows 10中使用AutoHotKey右键单击托盘图标

6
在Windows 7中,我有一个AutoHotKey脚本,可以自动右键单击系统托盘图标。
#Include %A_Scriptdir%\TrayIcon.ahk
TrayIcon_Button("CCC.exe", "R")

这段代码使用了来自FanaticGuru的帖子的TrayIcon.ahk库。

在Windows 7上可以正常工作,但在Windows 10上无法正常工作。

有没有办法在Windows 10上通过AutoHotKey脚本右键单击TrayIcon?

以下是该库中的TrayIcon_Button函数。由于库文件相对较长,因此我选择仅展示该函数。

; ----------------------------------------------------------------------------------------------------------------------
; 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
}
3个回答

6

我在Windows 10上进行了测试。尽管对于可见的图标运作良好,但对于隐藏在溢出窗口下的图标无法正常工作。

为快速解决问题,请更新TrayIcon_GetInfo()中的这三行代码。

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

将它们替换为

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

更新: 对于已经升级到Windows 1607的用户,它又出问题了 :)

要使其在Windows 10 1607中重新工作,请首先遵循上述最后的规则。然后用以下内容替换:

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

使用

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
}

注意:我认为这些更改都不具备向后兼容性。


无法在Windows 10 1709上使用。有什么办法可以修复吗?也许从库的最新版本开始? - Otiel
@Otiel 这就是我喜欢 Windows 更新的原因。我打算在这个周末更新 Windows。希望到周一之前我能够找到一个解决方案。 - user2286243
@Otiel 怎么运行它?我把 pastebin.com/jWkrKauS 复制到了 test.ahk 中并运行了它,但是什么也没有发生。 - JinSnow
2
@JinSnow 这是一个与托盘图标交互的库(顺便说一下,我正在使用一个新版本,你可以在 https://pastebin.com/1iSj6yUn 找到它)。要模拟单击托盘图标,您需要在您的 test.ahk 文件中调用 TrayIcon_Button 函数(请检查其签名,有几个参数可用 - 左键单击 / 右键单击...)。 - Otiel
谢谢!在我的脚本末尾加上TrayIcon_Button("dimmer.exe","R",false, 1)确实可以运行您的最后一段代码,即使它使用了错误的索引号也能找到它!(在末尾添加msgbox无法解决问题,因为菜单会非常快地打开和关闭) - JinSnow
显示剩余5条评论

1

尝试使用官方的管理员方式运行AHK脚本,只需在开头添加以下代码:

if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

我尝试了你的建议,但没有任何改变。问题似乎不在于宏没有权限。 - SimCard

0
也许...有...“更简单的方法”吗?
“早些时候”,我曾经使用Sean的TrayIcons...
我不想“破解”所有新的/复杂的脚本,这些脚本是在Sean之后出现的...
所以,最终我基于AHK Boards tmplinshi's Post进行了一些试错。
按照tmplinshi的指南,我得出结论,我需要将消息代码40022发布到(隐藏)窗口(类)NirSoft_VolumouseMsg100-x64。
因此,(简单/直接)编码解决方案变成了/是:
DetectHiddenWindows ON
PostMessage, 0x111, 40022, , , ahk_class NirSoft_VolumouseMsg100-x64 ahk_pid %VolMpid%
DetectHiddenWindows OFF

在编写代码时,我遇到了一个小问题,即如果脚本在执行PostMessage命令时同时显示SplashText,则无法正常工作,原因不明⁉️

我发现,tmplinshi的方法同样可以代替WinMenuSelectItem。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接