by JoeWinograd » 09 Jan 2023, 03:54
lexikos wrote:you can show a script's tray menu by sending it the same notification message that the tray would send: AHK_NOTIFYICON (1028) with lParam = WM_RBUTTONUP (0x205)
Hi lexikos,
After commenting earlier that this works perfectly and is a great solution, I discovered (when I tried to integrate it into my big script) that my testing for it was flawed. So I wrote a test script for only three colors to show the problem I'm having.
The first parameter is Color, which can be Blue, Green, or Red...or Master, which assigns the hotkeys. The second parameter is Debug, which can be 0 or 1.
By running with
Debug=1, I determined that:
(1) The hotkeys are firing. For example:
- blue hotkey fires.png (3.4 KiB) Viewed 9186 times
(2) The
GetColorPID function works correctly (PIDs returned by it match the PIDs in the Details tab of Task Manager). For example:
- GetColorPID works.png (6.76 KiB) Viewed 9186 times
(3)
AHK_NOTIFYICON gets actual/physical right and left clicks on the tray icons.
However, the hotkeys for the three colors (
!#b,
!#g,
!#r in the posted code) do not result in opening the context for the icons. It would seem that the
SendMessage command is not working, but I think it is coded correctly.
The code is below and the four supporting icons (PNG files for Blue, Green, Red, Master) are in the attached ZIP file (so is the script). I hope you can see what's wrong. Thanks very much, Joe
Code: Select all
#SingleInstance Off ; allow multiple instances to run concurrently
; get params
Color:=A_Args[1] ; Blue, Green, Red, Master
Debug:=A_Args[2] ; 0 or 1
DetectHiddenWindows,On
CurrentPID:=DllCall("GetCurrentProcessId")
Modifier:="!#"
TrayIconFile:=A_ScriptDir . "\" . Color . ".png" ; for testing, only 3 colors: Blue.png Green.png Red.png - plus Master.png
If (!FileExist(TrayIconFile))
{
MsgBox,262160,Fatal Error,TrayIconFile does not exist:`n%TrayIconFile%
ExitApp
}
Menu,Tray,Icon,%TrayIconFile%
If (Color="Master") ; Master assigns the hotkeys
{
; for testing, only 3 hotkeys
HotkeyBlue:=Modifier . "b"
Hotkey,%HotkeyBlue%,RightClickBlue,On
HotkeyGreen:=Modifier . "g"
Hotkey,%HotkeyGreen%,RightClickGreen,On
HotkeyRed:=Modifier . "r"
Hotkey,%HotkeyRed%,RightClickRed,On
}
OnMessage(0x404,"AHK_NOTIFYICON")
AHK_NOTIFYICON(wParam,lParam)
{
global
DetectHiddenWindows,On
If ((Debug) and ((lParam=0x202) or (lParam=0x205)))
MsgBox,,Enter AHK_NOTIFYICON,wParam=%wParam%`nlParam=%lParam%
If (lParam=0x202) ; WM_LBUTTONUP=0x0202
Menu,Tray,Show ; show context menu on left click, too
If (lParam=0x205) ; WM_RBUTTONUP=0x0205
Menu,Tray,Show ; show context menu on right click - this should actually result in showing menu twice because system should automatically show it
Return
}
Return ; end auto-execute
RightClickBlue:
PID:=GetColorPID("Blue")
If (PID=-1)
{
MsgBox Blue not running
Return
}
If (Debug)
MsgBox % "right-click Blue currpid=" . CurrentPID . "`nBluePID=" . PID
SendMessage,0x404,,0x205,,ahk_id %PID% ; WM_RBUTTONUP=0x0205
Return
RightClickGreen:
PID:=GetColorPID("Green")
If (PID=-1)
{
MsgBox Green not running
Return
}
If (Debug)
MsgBox % "right-click Green currpid=" . CurrentPID . "`nGreenPID=" . PID
SendMessage,0x404,,0x205,,ahk_id %PID% ; WM_RBUTTONUP=0x0205
Return
RightClickRed:
PID:=GetColorPID("Red")
If (PID=-1)
{
MsgBox Red not running
Return
}
If (Debug)
MsgBox % "right-click Red currpid=" . CurrentPID . "`nRedPID=" . PID
SendMessage,0x404,,0x205,,ahk_id %PID% ; WM_RBUTTONUP=0x0205
Return
GetColorPID(Color)
{
global
DetectHiddenWindows,On
For Process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
{
CmdLine:=Process.CommandLine
CmdPID:=Process.ProcessId
If (InStr(CmdLine,A_ScriptName) and InStr(CmdLine,Color))
{
If (Debug)
MsgBox,,Color Running Info - %Color%,CmdLine=%CmdLine%`nScriptName=%A_ScriptName%`nCmdPID=%CmdPID%`nCurrentPID=%CurrentPID%
Return CmdPID ; as soon as it finds process
}
}
Return -1 ; not found after looking through all processes
}
- Attachments
-
- RightClick.zip
- (14.02 KiB) Downloaded 1011 times
[quote=lexikos]you can show a script's tray menu by sending it the same notification message that the tray would send: AHK_NOTIFYICON (1028) with lParam = WM_RBUTTONUP (0x205)[/quote]Hi lexikos,
After commenting earlier that this works perfectly and is a great solution, I discovered (when I tried to integrate it into my big script) that my testing for it was flawed. So I wrote a test script for only three colors to show the problem I'm having.
The first parameter is Color, which can be Blue, Green, or Red...or Master, which assigns the hotkeys. The second parameter is Debug, which can be 0 or 1.
By running with [b]Debug=1[/b], I determined that:
(1) The hotkeys are firing. For example:
[attachment=2]blue hotkey fires.png[/attachment]
(2) The [c]GetColorPID[/c] function works correctly (PIDs returned by it match the PIDs in the Details tab of Task Manager). For example:
[attachment=1]GetColorPID works.png[/attachment]
(3) [c]AHK_NOTIFYICON[/c] gets actual/physical right and left clicks on the tray icons.
However, the hotkeys for the three colors ([c]!#b[/c], [c]!#g[/c], [c]!#r[/c] in the posted code) do not result in opening the context for the icons. It would seem that the [c]SendMessage[/c] command is not working, but I think it is coded correctly.
The code is below and the four supporting icons (PNG files for Blue, Green, Red, Master) are in the attached ZIP file (so is the script). I hope you can see what's wrong. Thanks very much, Joe
[code]
#SingleInstance Off ; allow multiple instances to run concurrently
; get params
Color:=A_Args[1] ; Blue, Green, Red, Master
Debug:=A_Args[2] ; 0 or 1
DetectHiddenWindows,On
CurrentPID:=DllCall("GetCurrentProcessId")
Modifier:="!#"
TrayIconFile:=A_ScriptDir . "\" . Color . ".png" ; for testing, only 3 colors: Blue.png Green.png Red.png - plus Master.png
If (!FileExist(TrayIconFile))
{
MsgBox,262160,Fatal Error,TrayIconFile does not exist:`n%TrayIconFile%
ExitApp
}
Menu,Tray,Icon,%TrayIconFile%
If (Color="Master") ; Master assigns the hotkeys
{
; for testing, only 3 hotkeys
HotkeyBlue:=Modifier . "b"
Hotkey,%HotkeyBlue%,RightClickBlue,On
HotkeyGreen:=Modifier . "g"
Hotkey,%HotkeyGreen%,RightClickGreen,On
HotkeyRed:=Modifier . "r"
Hotkey,%HotkeyRed%,RightClickRed,On
}
OnMessage(0x404,"AHK_NOTIFYICON")
AHK_NOTIFYICON(wParam,lParam)
{
global
DetectHiddenWindows,On
If ((Debug) and ((lParam=0x202) or (lParam=0x205)))
MsgBox,,Enter AHK_NOTIFYICON,wParam=%wParam%`nlParam=%lParam%
If (lParam=0x202) ; WM_LBUTTONUP=0x0202
Menu,Tray,Show ; show context menu on left click, too
If (lParam=0x205) ; WM_RBUTTONUP=0x0205
Menu,Tray,Show ; show context menu on right click - this should actually result in showing menu twice because system should automatically show it
Return
}
Return ; end auto-execute
RightClickBlue:
PID:=GetColorPID("Blue")
If (PID=-1)
{
MsgBox Blue not running
Return
}
If (Debug)
MsgBox % "right-click Blue currpid=" . CurrentPID . "`nBluePID=" . PID
SendMessage,0x404,,0x205,,ahk_id %PID% ; WM_RBUTTONUP=0x0205
Return
RightClickGreen:
PID:=GetColorPID("Green")
If (PID=-1)
{
MsgBox Green not running
Return
}
If (Debug)
MsgBox % "right-click Green currpid=" . CurrentPID . "`nGreenPID=" . PID
SendMessage,0x404,,0x205,,ahk_id %PID% ; WM_RBUTTONUP=0x0205
Return
RightClickRed:
PID:=GetColorPID("Red")
If (PID=-1)
{
MsgBox Red not running
Return
}
If (Debug)
MsgBox % "right-click Red currpid=" . CurrentPID . "`nRedPID=" . PID
SendMessage,0x404,,0x205,,ahk_id %PID% ; WM_RBUTTONUP=0x0205
Return
GetColorPID(Color)
{
global
DetectHiddenWindows,On
For Process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
{
CmdLine:=Process.CommandLine
CmdPID:=Process.ProcessId
If (InStr(CmdLine,A_ScriptName) and InStr(CmdLine,Color))
{
If (Debug)
MsgBox,,Color Running Info - %Color%,CmdLine=%CmdLine%`nScriptName=%A_ScriptName%`nCmdPID=%CmdPID%`nCurrentPID=%CurrentPID%
Return CmdPID ; as soon as it finds process
}
}
Return -1 ; not found after looking through all processes
}
[/code]