天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 79|回复: 0

在ARX中通过COM在ACAD中添加菜单和工具条

[复制链接]

该用户从未签到

主题

0

回帖

2912

积分

管理员

积分
2912
发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
代码如下:

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

 

 

 

 

在ARX中通过COM在ACAD中添加菜单和工具条
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池|中国膜结构网_中国空间膜结构协会

GMT+8, 2024-11-1 10:26 , Processed in 0.140025 second(s), 28 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表