Is it possible to right-click on a tray context menu item to get a context menu for that item?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Is it possible to right-click on a tray context menu item to get a context menu for that item?

04 May 2019, 15:15

Hi Folks,
Here's a working script that has three items in the tray context menu:

Code: Select all

#Persistent
Menu,Tray,NoStandard
Menu,Tray,Add,&Hello,ContextMenu
Menu,Tray,Add,&World,ContextMenu
Menu,Tray,Add,E&xit,ContextMenu
Return
ContextMenu:
If (A_ThisMenuItem="&Hello")
{
  MsgBox Hello
  Return
}
If (A_ThisMenuItem="&World")
{
  MsgBox World
  Return
}
If (A_ThisMenuItem="E&xit")
  ExitApp
When I right-click on the tray icon it shows the three menu choices: Hello, World, Exit. What I'd like to be able to do is right-click on one of those and have a context menu appear for it. In other words, a left-click on, say, the Hello item would work as usual (executing its left-click code), but a right-click on it would bring up another context menu with its own left-click choices. Is this possible? Btw, I know how to do submenus for a menu...this is different from that. Thanks much, Joe
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is it possible to right-click on a tray context menu item to get a context menu for that item?

05 May 2019, 00:10

This worked to detect right-clicks on menu items, but only if used from a separate script.

Code: Select all

~RButton:: ;get text of menu item that was right-clicked on
;SoundBeep
MouseGetPos,,, hWnd
WinGetClass, vWinClass, % "ahk_id " hWnd
if !(vWinClass = "#32768")
	return
SendMessage, 0x1E1, 0, 0,, % "ahk_id " hWnd ;MN_GETHMENU := 0x1E1
hMenu := ErrorLevel
vIsMatch := 0
Loop, % DllCall("user32\GetMenuItemCount", "Ptr",hMenu)
{
	;MF_BYPOSITION := 0x400
	vIndex := A_Index-1
	vState := DllCall("user32\GetMenuState", "Ptr",hMenu, "UInt",vIndex, "UInt",0x400, "UInt")
	if (vState & 0x80) ;MF_HILITE := 0x80
	{
		vIsMatch := 1
		break
	}
}
if vIsMatch
{
	vChars := 1 + DllCall("user32\GetMenuString", "Ptr",hMenu, "UInt",vIndex, "Ptr",0, "Int",0, "UInt",0x400)
	VarSetCapacity(vText, vChars << !!A_IsUnicode)
	DllCall("user32\GetMenuString", "Ptr",hMenu, "UInt",vIndex, "Str",vText, "Int",vChars, "UInt",0x400) ;MF_BYPOSITION := 0x400
	MsgBox, % vText
}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Is it possible to right-click on a tray context menu item to get a context menu for that item?

05 May 2019, 19:38

Hi jeeswg,
Thanks for the reply...and the code...much appreciated! Some of it is beyond me, but I'll study it and play with it. However, I must be able to call it from the same program. If that's not doable, I'll just make another right-click option at the root level of the context menu that does what I want, rather than a right-click on the open context menu item. Regards, Joe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, DiegoSouto, mikeyww, Rohwedder, Swiftly9767 and 371 guests