Autohotkey 中文论坛

完整版: 如何获取某个菜单的内容
你目前正在浏览的是简化版. 请点击这里浏览完整版
记得在yonken的博客中,有个获取菜单内容的脚本,但是对于特定的菜单,如何获取其内容呢?例如,在excel中,如何获取窗口下拉菜单的内容。
主要是用到了 API 函数 GetMenu ,GetMenuItemCount 等相关的函数
想具体了解 还是 查一下 MSDN 吧
比如:
如何 不用点击或者快捷键的方式,而是采用control 或者 sendmessage 方式 运行 “火狐的 右键-》查看页面信息”或者直接调用出 查看页面信息。
谢谢!
#SingleInstance, Force
SetBatchLInes, -1

WinWait, ahk_class #32768
SendMessage, 0x1E1, 0, 0 ; MN_GETHMENU
hMenu := ErrorLevel
sContents := GetMenu(hMenu)
WinWaitClose

MsgBox, % sContents


GetMenu(hMenu)
{
Loop, % DllCall("GetMenuItemCount", "Uint", hMenu)
{
idx := A_Index - 1
idn := DllCall("GetMenuItemID", "Uint", hMenu, "int", idx)
nSize++ := DllCall("GetMenuString", "Uint", hMenu, "int", idx, "Uint", 0, "int", 0, "Uint", 0x400)
VarSetCapacity(sString, nSize)
DllCall("GetMenuString", "Uint", hMenu, "int", idx, "str", sString, "int", nSize, "Uint", 0x400) ;MF_BYPOSITION
If !sString
sString := "---------------------------------------"
sContents .= idx . " : " . idn . A_Tab . A_Tab . sString . "`n"
If (idn = -1) && (hSubMenu := DllCall("GetSubMenu", "Uint", hMenu, "int", idx))
sContents .= GetMenu(hSubMenu)
}
Return sContents
}
楼上的帖子单独保存成ahk文件可以取得第一个弹出菜单的GetMenuItemID等等信息,很齐全!
; #NoEnv ;I have no idea why this is required, but it is

;Iterates through a menu

Run, Notepad.exe

WinWait, Untitled - Notepad
WinWaitActive

;stores the unique ID (HWND) for the window (used later)
WinGet, WinHWND, ID

;stores the menu bar
hMenuBar := DllCall("GetMenu", "uint", WinExist())


;sub menu to iterate
SubMenuIndex := 1 ;the file menu

;the menu (really a submenu of the menu bar) to iterate
;-1 converts position from one-based to zero-based index
hMenu := DllCall("GetSubMenu", "uint", hMenuBar, "int", SubMenuIndex - 1)


MenuItemIndex := 2 ;Open

;retrieves the menu item handle for the desired item
hMenuItem := DllCall("GetMenuItemID", "uint", hMenu, "int", MenuItemIndex - 1)


;"Select" File -> Open
PostMessage, 0x111, %hMenuItem% ,0,, ahk_id %WinHWND% ; 0x111 is WM_COMMAND

return
楼上的帖子实例为打开notepad,并打开“打开”菜单。其他应用,可以使用4楼的代码获得序号等配合楼上的代码实现自己的使用!
参考网址