Possible to modify context menus for applications?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Possible to modify context menus for applications?

09 Apr 2018, 15:41

Is it possible for AutoHotkey to modify the context menu within an application? Specifically, I am wanting to add an "AHK Macros" menu option to the context menu in several different applications such as Microsoft Word, Lotus Notes, the various web browsers, etcetera that contains a list of scripts specific to each application.

I know I can work around the problem by making a stand-alone menu (IE: Right Click for standard context menu, Alt+Right Click for my menu), but I am wanting to share this with some computer neophytes and do not want to confuse them by adding non-standard interactions. :lol:
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Possible to modify context menus for applications?

09 Apr 2018, 15:55

Try this... Run the script, and then right-click inside a Window that normally has a context-menu. There will be a new item.

https://autohotkey.com/boards/viewtopic ... 45#p170145

Code: Select all

#Persistent
SetWinEventHook(0x6, 0x6, 0, RegisterCallback("HookProc", "F"), 0, 0, 0)  ; EVENT_SYSTEM_MENUPOPUPSTART := 0x6

HookProc(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
   static MN_GETHMENU := 0x1E1, info := {}, funcObj := Func("OnMenuClick").Bind(info)
   
   if !info.pos
      Hotkey, ~LButton, % funcObj, On
   
   SendMessage, MN_GETHMENU,,,, ahk_id %hwnd%
   hMenu := ErrorLevel
   
   itemCount := DllCall("GetMenuItemCount", Ptr, hMenu)
   info.pos := itemCount, info.hWnd := hwnd, info.hMenu := hMenu
   DllCall("AppendMenu", Ptr, hMenu, UInt, 0, Ptr, 0, Str, "My Custom Item")
}

OnMenuClick(info)  {
   CoordMode, Mouse
   MouseGetPos, X, Y, hWnd
   if (hWnd != info.hWnd)
      Return
   
   POINT := X | Y << 32
   VarSetCapacity(RECT, 16, 0)
   DllCall("GetMenuItemRect", Ptr, 0, Ptr, info.hMenu, UInt, info.pos, Ptr, &RECT)
   if DllCall("PtInRect", Ptr, &RECT, Int64, POINT)
      MsgBox, My item was clicked!
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
   return DllCall( "SetWinEventHook", UInt, eventMin, UInt, eventMax
                                    , Ptr, hmodWinEventProc, Ptr, lpfnWinEventProc
                                    , UInt, idProcess, UInt, idThread
                                    , UInt, dwFlags, Ptr )
}
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Possible to modify context menus for applications?

09 Apr 2018, 16:55

Hi TheDewd,
That is very cool! I just tested it in W10 with AHK 1.1.28.02 U32. It works in KEDIT, Notepad, Total Commander, and all tray icons that I tried it with, but does not work in Chrome, Excel, Power PDF Advanced, and Word. Any thoughts on why it works in some programs and not others?

Also, every time I right-click on an AHK tray icon (not other tray icons), it adds another "My Custom Item" entry...looks like this now on my AHK hotkeys/hotstrings start-up script:
TheDewd custom context menu item.gif
TheDewd custom context menu item.gif (4.16 KiB) Viewed 2014 times
Any thoughts on that? Thanks, Joe
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Possible to modify context menus for applications?

09 Apr 2018, 17:05

TheDewd wrote:Try this... Run the script, and then right-click inside a Window that normally has a context-menu. There will be a new item.
Thank you for that script. It works perfectly in some applications like Windows File Explorer and Notepad.
Unfortunately, it does not work in other applications like Microsoft Word or Google Chrome. I suspect those applications do not use the traditional Windows UI elements for their context menus, so may not be something AHK can intercept & modify. :problem:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 341 guests