这段代码实现了 动态管理窗口菜单栏 的功能。通过 AutoHotkey 和 Windows API 的结合,可以对窗口菜单栏进行以下操作:
- 移除菜单栏中的选项(
F1
)。 - 动态添加菜单栏选项(简易版和高级版)(
F2
和F3
)。 - 支持嵌套子菜单,如在菜单项中创建子菜单。
F1:: WinGet, hHwnd, id, ahk_class AHKEditor hMenu := DllCall("GetMenu", "uint", hHwnd) ; 点进菜单栏某项,再按F1删除 DllCall("User32\DeleteMenu", "ptr", hMenu, "uint", 0, "uint", 0, "int") Return ; 这是添加最外部的菜单栏选项【简易限制版】 F2::AppendMenu("ahk_class AHKEditor", 1000, "testing") AppendMenu(WinTitle, ItemID, ItemName) { hWndTarget := WinExist(WinTitle) hMenu := DllCall("GetMenu", "Ptr", hWndTarget, "Ptr") DllCall("AppendMenu", "Ptr", hMenu, "UInt", 0x0, "UPtr", ItemID, "Str", ItemName) DllCall("DrawMenuBar", "Ptr", hWndTarget) } ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=35527&hilit=AppendMenu ; 这是添加最外部的菜单栏选项【完整高级版】 F3:: #SingleInstance Force Run, Notepad,,, pid WinWait, % (WinTitle := "ahk_pid " pid) hWnd := getHwndForPid(pid) MyMenu := AddMenuPopup(hWnd, "&Foo") AddMenuItem(hWnd, MyMenu, 1001, "Apple") AddMenuItem(hWnd, MyMenu, 1002, "Banana") MySubMenu := DllCall("CreateMenu") MF_POPUP = 16 DllCall("AppendMenu", "Ptr", MyMenu, "UInt", MF_POPUP, "UPtr", MySubMenu, "Str", "MySubMenu") DllCall("AppendMenu", "Ptr", MySubMenu, "UInt", 0, "UPtr", 1003, "Str", "Apple2") DllCall("AppendMenu", "Ptr", MySubMenu, "UInt", 0, "UPtr", 1004, "Str", "Banana2") Return AddMenuPopup(hWnd, MenuName) { MF_POPUP = 16 hItem := DllCall("CreateMenu") hMenu := DllCall("GetMenu", "Ptr", hWnd, "Ptr") DllCall("AppendMenu", "Ptr", hMenu, "UInt", MF_POPUP, "UPtr", hItem, "Str", MenuName) Return hItem } AddMenuItem(hWnd, MenuName, ItemID, ItemName) { MF_BYPOSITION = 0x400 MF_SEPARATOR = 0x800 MF_STRING = 0x000 MF_POPUP = 16 hMenu := DllCall("GetMenu", "UInt", hWnd) DllCall("InsertMenu", "UInt", MenuName, "UInt", 0x0, "UInt", MF_BYPOSITION | MF_STRING, "UInt", ItemID, "Str", ItemName) DllCall("SetMenu", "Ptr", MenuName, "Ptr", hMenu) DllCall("DrawMenuBar", "Ptr", hWnd) } getHwndForPid(pid) { pidStr := "ahk_pid " . pid WinGet, hWnd, ID, %pidStr% return hWnd }
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)