代码如下:
extern "C" AcRx::AppRetCode
acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
{
switch (msg) {
case AcRx::kInitAppMsg:
// Comment out the following line if your
// application should be locked into memory
acrxDynamicLinker->unlockApplication(pkt);
acrxDynamicLinker->registerAppMDIAware(pkt);
InitApplication();
addMenuThroughMfcCom();
break;
case AcRx::kUnloadAppMsg:
UnloadApplication();
break;
}
return AcRx::kRetOK;
}
void addMenuThroughMfcCom()
{
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 = "灵宇断面(&L)";
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;
VariantClear(&index);
if (pDisp==NULL) {
//create it
IAcadPopupMenu IPopUpMenu(IPopUpMenus.Add(cstrMenuName));
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
IPopUpMenu.AddMenuItem(index, "横断面成图(&H)", "HDM ");
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 1;
IPopUpMenu.AddMenuItem(index, "纵断面成图(&Z)", "ZDM ");
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 2;
IPopUpMenu.AddSeparator(index);
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 3;
IPopUpMenu.AddMenuItem(index, "灵宇断面帮助(&B)", "DMHELP ");
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;
}
也可以用相同的方法添加工具条,这样加入的工具条和CAD本身的完全相同。代码如下:
void AddDMToolbar()
{
IAcadApplication acad;
IDispatch *pDisp = acedGetAcadWinApp()->
GetIDispatch(TRUE); //AddRef is called on the pointer
acad.AttachDispatch(pDisp); // does not call AddRef()
IAcadMenuGroups acMenuGroups ( acad.GetMenuGroups() );
IAcadMenuGroup acMenu;
CComVariant vt;
long cnt = acMenuGroups.GetCount();
for ( long i = 0; i < cnt; i++ )
{
vt = i;
acMenu = acMenuGroups.Item( vt );
CString cgrpName = acMenu.GetName();
if ( cgrpName.CompareNoCase( "Acad" ) == 0 )
{
break;
}
}
IAcadToolbars acToolbars (acMenu.GetToolbars());
CString sToolbarName = "断面";
IAcadToolbar acToolbar;
cnt = acToolbars.GetCount();
for ( i = 0; i < cnt; i++ )
{
vt = i;
acToolbar = acToolbars.Item( vt );
if( acToolbar.GetName() == sToolbarName )
{
return;
}
}
CString appFileName = acedGetAppName(); //取出完整的应用程序名称,含路径
char dir[_MAX_DIR], drive[_MAX_DRIVE], path[_MAX_PATH];
_splitpath(appFileName, drive, dir, NULL, NULL);
_makepath(path, drive, dir, NULL, NULL);
try
{
acToolbar = acToolbars.Add ( sToolbarName );
acToolbar.SetVisible( true );
acToolbar.Dock( 0 );
COleVariant flyOutButton;
IAcadToolbarItem acToolbarItem;
CString bmpFile;
vt = 0;
acToolbarItem = acToolbar.AddToolbarButton( vt, "横断面","绘制横断面图", "hdm ", flyOutButton );
bmpFile.Format( "%s%s", path, "hdm.bmp" );
acToolbarItem.SetBitmaps ( bmpFile, bmpFile );
vt = 1;
acToolbarItem = acToolbar.AddToolbarButton( vt, "纵断面","绘制纵断面图", "zdm ", flyOutButton );
bmpFile.Format( "%s%s", path, "zdm.bmp" );
acToolbarItem.SetBitmaps ( bmpFile, bmpFile );
vt = 2;
acToolbarItem = acToolbar.AddToolbarButton( vt, "帮助","显示软件帮助信息", "dmhelp ", flyOutButton );
bmpFile.Format( "%s%s", path, "help.bmp" );
acToolbarItem.SetBitmaps ( bmpFile, bmpFile );
}
catch(...)
{
//acutPrintf("\n创建断面工具条失败!");
//acutPrintf("\n请与作者联系");
return;
}
}