【转载】在ARX中通过COM在ACAD中添加菜单和工具条

科技创新项目需要结题了,现在只有一个标注剪切的功能,同张老师商量了一下,张老师叫我再加上一两个功能,同时要做出有对话框、菜单或者工具条之类的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;
}


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;
 }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值