通过钩子获取右键菜单栏按钮ID这个为什么不行呢
LRESULT CALLBACK MouseProcEx(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
PMOUSEHOOKSTRUCT pInfo = (PMOUSEHOOKSTRUCT)lParam;
if (nCode == HC_ACTION && wParam == WM_RBUTTONDOWN) {
// 当右键按下时处理
HMENU hMenu = NULL;
hMenu = GetMenu((HWND)lParam); // 获取菜单句柄
if (hMenu != NULL) {
int count = GetMenuItemCount(hMenu);
for (int i = 0; i < count; i++) {
MENUITEMINFO info;
ZeroMemory(&info, sizeof(info));
info.cbSize = sizeof(info);
info.fMask = MIIM_TYPE | MIIM_SUBMENU;
if (GetMenuItemInfo(hMenu, i, TRUE, &info)) {
if (info.hSubMenu == NULL) {
// 这是一个普通的菜单项
int itemID = info.wID;
WCHAR buf2[60] = { 0 };
wsprintf(buf2, L"hookapp 普通的菜单项(%d)", itemID);
OutputDebugString(buf2);
}
else {
// 这是一个子菜单,需要递归处理
int subMenuCount = GetMenuItemCount(info.hSubMenu);
for (int j = 0; j < subMenuCount; j++) {
MENUITEMINFO subInfo;
ZeroMemory(&subInfo, sizeof(subInfo));
subInfo.cbSize = sizeof(subInfo);
subInfo.fMask = MIIM_TYPE | MIIM_SUBMENU;
if (GetMenuItemInfo(info.hSubMenu, j, TRUE, &subInfo)) {
if (subInfo.hSubMenu == NULL) {
int subItemID = subInfo.wID;
WCHAR buf2[60] = { 0 };
wsprintf(buf2, L"hookapp 子菜单项(%d)", subItemID);
OutputDebugString(buf2);
}
else {
// 递归处理子菜单项
// ...
}
}
}
}
}
}
}
else{
OutputDebugString(L"hookapp NOMenu)");
}
}
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}