不知是想使用代码的方式动态添加菜单还是使用mnu文件的方式添加菜单,如果使用代码的方式添加菜单需要使用AutoCAD的com接口来实现。参考下面的代码: void TdiFoundationApp::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("TDI"); 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("新建(&N)"), _T("_MyNEW ")); VariantInit(&index); V_VT(&index) = VT_I4; V_I4(&index) = iIndex++; IPopUpMenu.AddMenuItem(index, _T("检入(&I)"), _T("_MyCHECKIN ")); 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; } |