科技创新项目需要结题了,现在只有一个标注剪切的功能,同张老师商量了一下,张老师叫我再加上一两个功能,同时要做出有对话框、菜单或者工具条之类的UI界面。刚好碰巧当天晚上就看到有人在二次开发群里讲添加菜单和工具条
,呵呵,天助我也。转载过来参考参考。
原文地址:http://blog.csdn.net/habit2/archive/2005/07/29/438318.aspx
代码如下:
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;
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
void
addMenuThroughMfcCom()
{
TRY
{
IAcadApplication IAcad(acedGetAcadWinApp()->GetIDispatch(TRUE));
IAcadMenuBar IMenuBar(IAcad.GetMenuBar());
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
long numberOfMenus;
numberOfMenus = IMenuBar.GetCount();
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
IAcadMenuGroups IMenuGroups(IAcad.GetMenuGroups());
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
VARIANT index;
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
IAcadMenuGroup IMenuGroup(IMenuGroups.Item(index));
IAcadPopupMenus IPopUpMenus(IMenuGroup.GetMenus());
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
CString cstrMenuName = "灵宇断面(&L)";
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
VariantInit(&index);
V_VT(&index) = VT_BSTR;
V_BSTR(&index) = cstrMenuName.AllocSysString();
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
IDispatch* pDisp=NULL;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
//see if the menu is already there
TRY{pDisp = IPopUpMenus.Item(index); pDisp->AddRef();} CATCH(COleDispatchException,e){}END_CATCH;
VariantClear(&index);
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
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 ");
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 1;
IPopUpMenu.AddMenuItem(index, "纵断面成图(&Z)", "ZDM ");
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 2;
IPopUpMenu.AddSeparator(index);
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 3;
IPopUpMenu.AddMenuItem(index, "灵宇断面帮助(&B)", "DMHELP ");
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
pDisp = IPopUpMenu.m_lpDispatch;
pDisp->AddRef();
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
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;
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
也可以用相同的方法添加工具条,这样加入的工具条和CAD本身的完全相同。代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
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() );
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
IAcadMenuGroup acMenu;
CComVariant vt;
long cnt = acMenuGroups.GetCount();
for ( long i = 0; i < cnt; i++ )
{
vt = i;
acMenu = acMenuGroups.Item( vt );
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
CString cgrpName = acMenu.GetName();
if ( cgrpName.CompareNoCase( "Acad" ) == 0 )
{
break;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
IAcadToolbars acToolbars (acMenu.GetToolbars());
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
CString sToolbarName = "断面";
IAcadToolbar acToolbar;
cnt = acToolbars.GetCount();
for ( i = 0; i < cnt; i++ )
{
vt = i;
acToolbar = acToolbars.Item( vt );
if( acToolbar.GetName() == sToolbarName )
{
return;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
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 );
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
COleVariant flyOutButton;
IAcadToolbarItem acToolbarItem;
CString bmpFile;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
vt = 0;
acToolbarItem = acToolbar.AddToolbarButton( vt, "横断面","绘制横断面图", "hdm ", flyOutButton );
bmpFile.Format( "%s%s", path, "hdm.bmp" );
acToolbarItem.SetBitmaps ( bmpFile, bmpFile );
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
vt = 1;
acToolbarItem = acToolbar.AddToolbarButton( vt, "纵断面","绘制纵断面图", "zdm ", flyOutButton );
bmpFile.Format( "%s%s", path, "zdm.bmp" );
acToolbarItem.SetBitmaps ( bmpFile, bmpFile );
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
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;
}
}