Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get the ID of the menu item under the mouse cursor
#1
The custom menu is displayed next to the system shell menu.

When the F11 hotkey is pressed or the right mouse button is pressed,
I need to obtain the ID of the menu item under the mouse cursor so that I can output the text of the menu item.

Function Menu_Edit
Trigger !v"" "#32768" /QM     Help - how to add the trigger to the macro
Code:
Copy      Help
out
int h=TriggerWindow

;if(MenuGetString(hwnd 1 &_s)) out _s ;;Why is there no output?

rep
,0.1
,ifk(F11)
,,int mh=win(mouse); out mh
,,int iid=1 ;;How to get the ID of the menu item under the mouse cursor?
,,Acc a.Find(h "MENUITEM" "" "" 0x1001 3 iid)
,,out a.Name
,,key Z           ;; Esc
,,break

Macro Macro51
Trigger !v"" "#32768" /EXPLORER     Help - how to add the trigger to the macro
Code:
Copy      Help
int hwnd=TriggerWindow

ARRAY(int) a; win("" "#32768" "explorer" 0 0 0 a); if(a.len>1) ret

SetTimer(0 0 100 &sub._Timer)
;
_s=
;1 open docx
;2 open other
_i=ShowMenu(_s 0 0 0 8)
out _i


#sub _Timer v
function u1 u2 timer u3
if !IsWindowVisible(a[0]) && !(RealGetKeyState(1) or RealGetKeyState(2))
,KillTimer 0 timer
,EndMenu
#2
A C++ function from QM source code.
 
Code:
Copy      Help
//Returns id of popup menu item from point.
//Returns 0 if fails, eg if the point is not in a menu.
//p must be DPI physical, not logical.
int GetMenuItemFromPointSimple(HWND hwnd, POINT p, DWORD timeout, OUT HMENU* pHmenu/*=0*/)
{
    HMENU hm=dlg::HmenuFromHwnd(hwnd, timeout); if(!hm) return 0;
    if(call.PhysicalToLogicalPoint_AllOS && !call.PhysicalToLogicalPoint_AllOS(hwnd, &p)) return 0; //unlike most other API, MenuItemFromPoint still uses logical coord on Win10
    int i=MenuItemFromPoint(0, hm, p); if(i==-1) return 0;
    i=GetMenuItemID(hm, i); if(i==-1 || i==0) return 0;
    if(pHmenu) *pHmenu=hm;
    return i;
    //info: can use GetMenuBarInfo/OBJID_CLIENT instead of MN_GETHMENU, but it also sends MN_GETHMENU, and does not give more useful info, eg owner window handle (hwndMenu is 0).
}

HMENU HmenuFromHwnd(HWND w, DWORD timeout)
{
    if(timeout==0) return (HMENU)SEND(w, MN_GETHMENU, 0, 0); //SendMessage
    HMENU hm;
    if(!SendT(w, MN_GETHMENU, 0, 0, timeout, &hm)) return 0; //SendMessageTimeout
    return hm;
}

    BOOL (WINAPI* PhysicalToLogicalPoint_AllOS)(HWND hWnd, LPPOINT lpPoint); //XP: 0, Vista-8.0: PhysicalToLogicalPoint, 8.1+: PhysicalToLogicalPointForPerMonitorDPI
#3
Function GetMenuItemIdFromMouse
Code:
Copy      Help
;/
function# [str&itemText] [int&popupMenuHandle]

;Gets id of menu item from mouse.
;The id can be used with QM function men().
;Works only with standard menus.
;Returns 0 if fails, eg if mouse is not on a menu. Also if it opens a submenu (no id).


POINT p; xm p
int h=WindowFromPoint(p.x p.y)
if(GetClassLongW(h GCW_ATOM)!=32768) ret

int hm=SendMessage(h MN_GETHMENU 0 0); if(!hm) ret

dll user32 #PhysicalToLogicalPoint hWnd POINT*lpPoint
dll user32 #PhysicalToLogicalPointForPerMonitorDPI hWnd POINT*lpPoint
if(_winver<0x603) PhysicalToLogicalPoint h &p; else PhysicalToLogicalPointForPerMonitorDPI h &p

int i=MenuItemFromPoint(0 hm p); if(i=-1) ret
i=GetMenuItemID(hm i); if(i=-1) ret

if(&popupMenuHandle) popupMenuHandle=hm
if(&itemText) MenuGetString hm i &itemText

ret i


Forum Jump:


Users browsing this thread: 1 Guest(s)