右键单击Windows 10中带有AutoHotKey的托盘图标

Sim*_*ard 6 autohotkey system-tray windows-10

在Windows 7中,我有一个AutoHotKey脚本,可以自动右键单击托盘图标.

#Include %A_Scriptdir%\TrayIcon.ahk
TrayIcon_Button("CCC.exe", "R")
Run Code Online (Sandbox Code Playgroud)

其中使用了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
}
Run Code Online (Sandbox Code Playgroud)

小智 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
Run Code Online (Sandbox Code Playgroud)

将它们替换为

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
Run Code Online (Sandbox Code Playgroud)

更新:对于升级到 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
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

注意:我认为这些更改中的任何一个都不向后兼容。

  • @JinSnow 这是一个与托盘图标交互的库(顺便说一句,我使用的是较新的版本,您可以在 https://pastebin.com/1iSj6yUn 上找到)。要模拟单击托盘图标,您需要在“test.ahk”文件中调用“TrayIcon_Button”函数(检查其签名,有多个可用参数 - 左键单击/右键单击...)。 (2认同)