这还付费啊。。。 需要导入acad.tlb来导入Com接口 void CreateMenuItem() { TRY { IAcadApplication IAcad(acedGetAcadWinApp()->GetIDispatch(TRUE)); IAcadMenuBar IMenuBar(IAcad.GetMenuBar()); long numberOfMenus; numberOfMenus = IMenuBar.GetCount(); IAcadMenuGroups IMenuGroups(IAcad.GetMenuGroups()); VARIANT index; VariantInit(&index); V_VT(&index) = VT_I4; V_I4(&index) = 0; IAcadMenuGroup IMenuGroup(IMenuGroups.Item(index)); IAcadPopupMenus IPopUpMenus(IMenuGroup.GetMenus()); CString cstrMenuName = _T("MyMenu"); VariantInit(&index); V_VT(&index) = VT_BSTR; V_BSTR(&index) = cstrMenuName.AllocSysString(); IDispatch* pDisp=NULL; //see if the menu is already there TRY { pDisp = IPopUpMenus.Item(index); pDisp->AddRef(); } CATCH(COleDispatchException,e) {} END_CATCH; int iIndex = 0; if (pDisp==NULL) { //create it IAcadPopupMenu IPopUpMenu(IPopUpMenus.Add(cstrMenuName)); VariantInit(&index); V_VT(&index) = VT_I4; V_I4(&index) = iIndex++; IPopUpMenu.AddMenuItem(index, _T("测试(&T)"), _T("_TestCmd ")); pDisp = IPopUpMenu.m_lpDispatch; pDisp->AddRef(); }
IAcadPopupMenu IPopUpMenu(pDisp); if (!IPopUpMenu.GetOnMenuBar()) { VariantInit(&index); V_VT(&index) = VT_I4; V_I4(&index) = numberOfMenus - 2;; IPopUpMenu.InsertInMenuBar(index); } else { VariantInit(&index); V_VT(&index) = VT_BSTR; V_BSTR(&index) = cstrMenuName.AllocSysString(); IPopUpMenus.RemoveMenuFromMenuBar(index); VariantClear(&index); } pDisp->Release(); } CATCH(COleDispatchException,e) { e->ReportError(); e->Delete(); } END_CATCH; } |