调用IContextMenu :: QueryContextMenu,但IContextMenu :: InvokeCommand没有

Tar*_*Tar 1 c++ windows com atl com-server

在我的IContextMenuCOM服务器实现中,QueryContextMenu调用了gets(可以通过日志记录查看),但InvokeCommand没有.这是QueryContextMenu:

HRESULT ContexMenuImp::QueryContextMenu(HMENU hmenu,UINT indexMenu,UINT idCmdFirst,
    UINT idCmdLast,UINT uFlags)
{
    if (uFlags & CMF_DEFAULTONLY) {
        // shouldn't handle this situation:
        LOG("IContextMenu::QueryContextMenu:    (...,CMF_DEFAULTONLY)");
        return MAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,0);
    }else if (InsertMenuItem(hmenu,indexMenu,TRUE,&globals.menuItemInfo) == FALSE){
        // error occurred:
        LOG("IContextMenu::QueryContextMenu:    Error: %d",GetLastError());
        return MAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,0);
    } else{
        // the desired situation: add item to the menu:
        LOG("IContextMenu::QueryContextMenu(hMenu,indexMenu:%u,idCmdFirst:%u,idCmdLast:%u,0x%x):    All set...",
            indexMenu,idCmdFirst,idCmdLast,uFlags);
        return MAKE_HRESULT(SEVERITY_SUCCESS,FACILITY_NULL,1/*handle only a single item*/);
    }
}
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?

Ray*_*hen 7

您忘记idCmdFirst在插入菜单时遵守.

globals.menuItemInfo.wID = idCmdFirst;
globals.menuItemInfo.fMask |= MIIM_ID;
Run Code Online (Sandbox Code Playgroud)

(所以我说得对:你添加了错误ID的菜单项.)

请注意,由于每个上下文菜单可能具有不同的ID,因此不应使用全局.

  • 我错过了那个地方..我特此感谢你的心理技巧;-) (2认同)