【问题标题】:IContextMenu::QueryContextMenu being called, but IContextMenu::InvokeCommand doesn'tIContextMenu::QueryContextMenu 被调用,但 IContextMenu::InvokeCommand 没有
【发布时间】:2011-12-04 11:35:05
【问题描述】:

在我的IContextMenu COM 服务器实现中,QueryContextMenu 被调用(可以通过日志记录看到)但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*/);
    }
}

知道为什么吗?

【问题讨论】:

  • 不知道。你的代码是什么样的?
  • 我的超能力告诉我,您返回的是 S_OK 而不是所需的 MAKE_HRESULT,或者您添加的菜单项 ID 错误。
  • @DavidHeffernan:添加了代码。
  • @RaymondChen:这里的直觉不好 :-) 会很高兴进行更多尝试。

标签: c++ windows com atl com-server


【解决方案1】:

您在插入菜单时忘记使用idCmdFirst

globals.menuItemInfo.wID = idCmdFirst;
globals.menuItemInfo.fMask |= MIIM_ID;

(所以我是对的:您添加的菜单项 ID 错误。)

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

【讨论】:

  • 我错过了那个地方.. 我在此感谢您的通灵能力 ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多