|
//GMenu.h
#pragma once
#if _MSC_VER >= 1600
#import “acax19enu.tlb” no_implementation raw_interfaces_only named_guids //访问菜单时,需要导入该类型库
#elif _MSC_VER >= 1500
#import “acax18enu.tlb” no_implementation raw_interfaces_only named_guids //访问菜单时,需要导入该类型库
#elif _MSC_VER >= 1400
#import “acax17enu.tlb” no_implementation raw_interfaces_only named_guids //访问菜单时,需要导入该类型库
#elif _MSC_VER >= 1300
#import “acax16enu.tlb” no_implementation raw_interfaces_only named_guids //访问菜单时,需要导入该类型库
#endif
namespace GMenu
{
/*!
@brief 添加菜单
*/
bool AddMenu();
};
//GMenu.cpp
#include “StdAfx.h”
#include “GMenu.h”
namespace GMenu
{
struct PopupMenuInfo
{
CString strName;
CString strTagString;
};
bool GetInfo(PopupMenuInfo& info,AutoCAD::IAcadPopupMenu* pPopUpMenu)
{
BSTR name = ::SysAllocString(L””);
pPopUpMenu->get_Name(&name);
info.strName=name;
::SysFreeString(name);
BSTR tagString = ::SysAllocString(L””);
pPopUpMenu->get_TagString(&tagString);
info.strTagString=tagString;
::SysFreeString(tagString);
return true;
}
struct PopupMenuItemInfo
{
CString strCaption;
CString strTagString;
};
bool GetInfo(PopupMenuItemInfo& info,AutoCAD::IAcadPopupMenuItem* pPopUpMenuItem)
{
BSTR caption = ::SysAllocString(L””);
pPopUpMenuItem->get_Caption(&caption);
info.strCaption=caption;
::SysFreeString(caption);
BSTR tagString = ::SysAllocString(L””);
pPopUpMenuItem->get_TagString(&tagString);
info.strTagString=tagString;
::SysFreeString(tagString);
return true;
}
int EnumFileMenu(AutoCAD::IAcadPopupMenu* pPopUpMenu)
{
bool bFind=false;
long count;
HRESULT hr=pPopUpMenu->get_Count(&count);
if(FAILED(hr))
{
return -1;
}
VARIANT index;
VariantInit(&index);
for(int j=0;j<count;j++)
{
AutoCAD::IAcadPopupMenuItem* pPopUpMenuItem;
V_VT(&index) = VT_I4;
V_I4(&index) = j;
hr=pPopUpMenu->Item(index,&pPopUpMenuItem);
if(FAILED(hr))
{
continue;
}
PopupMenuItemInfo info;
GetInfo(info,pPopUpMenuItem);
if(info.strTagString.CompareNoCase(_T(“ID_MnDrawing”))==0)
{
//绘图实用程序
bFind=true;
}
else
{
AutoCAD::AcMenuItemType itemType;
pPopUpMenuItem->get_Type(&itemType);
if(itemType==AutoCAD::acMenuSeparator&&bFind)
{
//绘图实用程序菜单找到之后,再找到分隔符
pPopUpMenuItem->Release();
return j;
}
}
pPopUpMenuItem->Release();
}
return -1;
}
bool AddMenu()
{
AutoCAD::IAcadApplication* pAcad;
AutoCAD::IAcadMenuBar* pMenuBar;
AutoCAD::IAcadMenuGroups* pMenuGroups;
HRESULT hr = NOERROR;
LPUNKNOWN pUnk = NULL;
LPDISPATCH pAcadDisp = NULL;
pAcadDisp = acedGetAcadWinApp()->GetIDispatch(TRUE);
if (pAcadDisp == NULL)
{
return false;
}
hr = pAcadDisp->QueryInterface(AutoCAD::IID_IAcadApplication,(void**)&pAcad);
pAcadDisp->Release();
if (FAILED(hr))
{
return false;
}
pAcad->put_Visible(true);
pAcad->get_MenuBar(&pMenuBar);
//得到整个CAD的菜单
pAcad->get_MenuGroups(&pMenuGroups);
pAcad->Release();
long numberOfMenus;
pMenuBar->get_Count(&numberOfMenus);
pMenuBar->Release();
for(int i=0;i<numberOfMenus;i++)
{
//遍历顶级菜单,如”文件”
AutoCAD::IAcadMenuGroup* pMenuGroup;
VARIANT index;
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = i;
hr=pMenuGroups->Item(index, &pMenuGroup);
if(FAILED(hr))
{
continue;
}
if(pMenuGroup==NULL)
{
continue;
}
BSTR name = ::SysAllocString(L””);
pMenuGroup->get_Name(&name);
CString strName=name;
::SysFreeString(name);
if(strName.CompareNoCase(_T(“ACAD”))==0)
{
AutoCAD::IAcadPopupMenus* pPopUpMenus;
hr=pMenuGroup->get_Menus(&pPopUpMenus);
if(FAILED(hr))
{
//释放顶级菜单
pMenuGroup->Release();
break;
}
long count;
pPopUpMenus->get_Count(&count);
for(int j=0;j<count;j++)
{
AutoCAD::IAcadPopupMenu* pPopUpMenu;
V_I4(&index) = j;
hr=pPopUpMenus->Item(index,&pPopUpMenu);
if(FAILED(hr))
{
continue;
}
PopupMenuInfo info;
GetInfo(info,pPopUpMenu);
if(info.strTagString.CompareNoCase(_T(“ID_MnFile”))==0)
{
//找到文件菜单
int nIndex=EnumFileMenu(pPopUpMenu);
if(nIndex>0)
{
//增加我们的菜单
V_I4(&index) =nIndex;
WCHAR wstrMenuItemName[256]=L”图纸比较”;
WCHAR wstrMenuItemMacro[256]=L”tzbj “;
AutoCAD::IAcadPopupMenuItem* pMenuItem;
pPopUpMenu->AddMenuItem(index, wstrMenuItemName, wstrMenuItemMacro, &pMenuItem);
OutputDebugStringX(_T(“增加图纸比较菜单:%d”),nIndex);
}
}
pPopUpMenu->Release();
}
pPopUpMenus->Release();
//释放顶级菜单
pMenuGroup->Release();
break;
}
//释放顶级菜单
pMenuGroup->Release();
OutputDebugStringX(_T(“顶级菜单:%s”),strName);
}
//释放整个菜单
pMenuGroups->Release();
return true;
}
}
//acrxEntryPoint.cpp
#define MENU_TIMER 45//注意请不要用我一样的ID,否则有可能会重复
#include “GMenu.h”
void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT_PTR nTimerId, DWORD dwTime)
{
KillTimer(hWnd, nTimerId);
if (!GMenu::AddMenu())
{
SetTimer(acedGetAcadFrame()->GetSafeHwnd(), MENU_TIMER, 1000, TimerProc);
}
}
在初始化的时候调用
SetTimer(NULL, MENU_TIMER, 1000, TimerProc); |
|