简体   繁体   中英

Windows 7 - custom window menu item: GetMsgProc hook not invoked

I'll get right to the point: a custom menu item that I added to the system menu of every window through Windows global hooks doesn't cause GetMsgProc to be invoked when the user clicks it.

Here is how I add the menu items:

void HookCore::AppendTasksMenu(HWND windowHandle)
{
    // make the popup menu and set up the appearance flags for the sub-items
    m_mnuMoveToTasks = CreatePopupMenu();
    UINT tasksAppearanceFlags = MF_STRING | MF_ENABLED;

    //TODO: need to make proper iterator for MenuItemList
    list<MenuItemInfo*>::iterator iter;
    for (iter = m_menuItems->Begin(); iter != m_menuItems->End(); iter++)
    {
        // check if we are adding a separator
        if ((*iter)->GetSpecial() == MenuItemInfo::SEPARATOR)
        {
            AppendMenu(m_mnuMoveToTasks, MF_SEPARATOR, (*iter)->GetItemId(), NULL);
        }
        else
        {
            AppendMenu(m_mnuMoveToTasks,
                ((*iter)->IsChecked() ? tasksAppearanceFlags | MF_CHECKED : tasksAppearanceFlags),
                (*iter)->GetItemId(), (*iter)->GetItemName().c_str());
        }
    }

    // get the system menu and set up the appearance flag for our new system sub-menu
    HMENU mnuSystem = GetSystemMenu(windowHandle, FALSE);
    UINT itemAppearanceFlags =  MF_STRING | MF_ENABLED | MF_POPUP;

    AppendMenu(mnuSystem, MF_SEPARATOR, ID_MOVE_TO_TASK_SEP, NULL);
    // append the sub-menu we just created
    AppendMenu(mnuSystem, itemAppearanceFlags, (UINT_PTR)m_mnuMoveToTasks, MOVE_TO_TASK);
}

This creates a new sub-menu in the system menu, and the sub-menu contains my additional items. The item IDs start with 1001 , and increment by 1 for each new item. The items can be checked or unchecked.

When a user clicks one of my items I'm expecting to get a WM_SYSCOMMAND message through GetMsgProc , but it never gets called. GetMsgProc does get called however a few times during the initialization with other messages. I do see the WM_SYSCOMMAND message in CallWndProcRetProc though, but it doesn't contain the correct item ID. I was expecting to get the item ID from the low-order word of wParam (as specified here: [http://www.codeproject.com/KB/dialog/AOTop.aspx]), but instead it just contains SC_MOUSEMENU .

Here is how I assign the GetMsgProc hook:

myhookdata[GET_MSG_HOOK].nType = WH_GETMESSAGE;
myhookdata[GET_MSG_HOOK].hkprc = GetMsgProc;
myhookdata[GET_MSG_HOOK].hhook = SetWindowsHookEx(
    myhookdata[GET_MSG_HOOK].nType,
    myhookdata[GET_MSG_HOOK].hkprc,
    AppHandle, 0);

Any ideas? Are my item IDs wrong? What is the right way to get the ID of the clicked item?

Thanks!

Update:

Based on the suggestions below, I tried catching the 'WM_SYSCOMMAND' and 'WM_COMMAND' messages in CallWndProc, CallWndProcRetProc, GetMsgProc, and SysMsgProc. The item selection message wasn't delivered.

I also tried subclassing the window to which the menu belongs, and my WndProc never got the item selection message, although other messages like 'WM_MENUSELECT' and 'WM_UNINITMENUPOPUP' were delivered.

Any pointers where else to check?

Update 2:

So when I subclass/unsubclass the window, I do it in my CallWndProc hook. I subclass when I get the WM_INITMENUPOPUP message, and I unsubclass when I get the WM_MENUSELECT message for menu closing (when lParam equals NULL and HIWORD(wParam) equals 0xFFFF ).

I click on the system menu (at which point the WM_INITPOPUPMENU gets raised), move the mouse cursor into my sub-menu that contains the custom items, and then click on one of the items. I log every message I get in my new WndProc during this process. Here is the list of messages I get in my WndProc during this test:

WM_INITMENUPOPUP
147 (0x0093) - what is this message?
148 (0x0094) [9 times] - what is this message?
WM_NCMOUSELEAVE
WM_ENTERIDLE [2 times]
WM_NOTIFY [2 times]
WM_ENTERIDLE [2 times]
WM_NOTIFY
WM_ENTERIDLE [11 times]
WM_MENUSELECT
WM_ENTERIDLE [5 times]
WM_MENUSELECT
WM_ENTERIDLE [6 times]
WM_MENUSELECT
WM_ENTERIDLE [7 times]
WM_MENUSELECT
WM_ENTERIDLE [8 times]
WM_NOTIFY
WM_ENTERIDLE [5 times]
WM_NOTIFY
WM_ENTERIDLE
WM_NOTIFY
WM_ENTERIDLE
WM_UNINITMENUPOPUP
WM_CAPTURECHANGED

The message I'm expecting to see when the user clicks the item is either WM_COMMAND or WM_SYSCOMMAND . I don't have much experience with windows messages or working with the Windows API. Is one of those two the right message to look for? Neither message is there, yet it should be, right? Is there something I'm missing?

Ok, I figured it out. I'm not sure why, but I don't see the correct message in any of the hooks. I do, however, see it in WndProc if I subclass the window. In the 2nd Update above I said that I don't see the correct message when subclassing - I was unsubclassing too soon.

So, this is how I subclass:

if (oldWndProc == -1)
{
    oldWndProc = SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)NewWndProc);
    currentSubclassedHandle = hwnd;
}

And here is how I unsubclass:

if (oldWndProc != -1)
{
    SetWindowLongPtr(currentSubclassedHandle, GWL_WNDPROC, (LONG)oldWndProc);
    oldWndProc = -1;
    currentSubclassedHandle = NULL;
}

The message I get is WM_SYSCOMMAND , where the wParam is the custom menu item ID. This message gets delivered to the NewWndProc a little bit after the last message that I posted in the 2nd Update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM