|
[code]
// Enables the menu to be loaded/unloaded with the same command.
static bool bIsMenuLoaded = false;
void
demo()
{
AutoCAD::IAcadApplication *pAcad;
AutoCAD::IAcadMenuBar *pMenuBar;
AutoCAD::IAcadMenuGroups *pMenuGroups;
AutoCAD::IAcadMenuGroup *pMenuGroup;
AutoCAD::IAcadPopupMenus *pPopUpMenus;
AutoCAD::IAcadPopupMenu *pPopUpMenu;
AutoCAD::IAcadPopupMenuItem *pPopUpMenuItem;
HRESULT hr = NOERROR;
LPUNKNOWN pUnk = NULL;
LPDISPATCH pAcadDisp = acedGetIDispatch(TRUE);
if(pAcadDisp==NULL)
return;
hr = pAcadDisp->QueryInterface(AutoCAD::IID_IAcadApplication,(void**)&pAcad);
pAcadDisp->Release();
if (FAILED(hr))
return;
pAcad->put_Visible(true);
pAcad->get_MenuBar(&pMenuBar);
pAcad->get_MenuGroups(&pMenuGroups);
pAcad->Release();
long numberOfMenus;
pMenuBar->get_Count(&numberOfMenus);
pMenuBar->Release();
VARIANT index;
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
pMenuGroups->Item(index, &pMenuGroup);
pMenuGroups->Release();
pMenuGroup->get_Menus(&pPopUpMenus);
pMenuGroup->Release();
if (!bIsMenuLoaded) {
// add three menus
HRESULT hr = pPopUpMenus->Add(
_T("myDemoMenu1"), &pPopUpMenu);
hr = pPopUpMenus->Add(
_T("myDemoMenu2"), &pPopUpMenu);
hr = pPopUpMenus->Add(
_T("myDemoMenu3"), &pPopUpMenu);
if (hr == S_OK) {
acutPrintf(_T("\nMenu is created."));
} else {
acutPrintf(_T("\nMenu not created."));
}
bIsMenuLoaded = true;
}
else // remove the menu
{
long count = 0;
pPopUpMenus->get_Count(&count);
acutPrintf(_T("\n Before remove, count is: %d"),count) ;
long indexOfMyMenu = -1;
AutoCAD::IAcadPopupMenu* eachMenu = NULL;
for (long i=0; i< count; i++)
{
BSTR np;
pPopUpMenus->Item(_variant_t(i),&eachMenu);
eachMenu->get_Name(&np);
//remove the first menu in the group
if (_tcscmp(np, _T("myDemoMenu1"))==0)
{
indexOfMyMenu = i;
break;
}
}
// remove
if(indexOfMyMenu > -1)
{
HRESULT hr = pPopUpMenus->RemoveMenuFromMenuBar(_variant_t(indexOfMyMenu));
assert (hr == S_OK);
}
pPopUpMenus->get_Count(&count);
// the count is same to before removing.
acutPrintf(_T("\n After remove, count is: %d"),count) ;
bIsMenuLoaded = false;
}
pPopUpMenus->Release();
}[/code] |
|