天气与日历 切换到窄版

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

ObjectArx示例代码,在文件菜单下增加一个自己的菜单

[复制链接]

该用户从未签到

主题

0

回帖

2912

积分

管理员

积分
2912
发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
//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);

 

 

 

 

ObjectArx示例代码,在文件菜单下增加一个自己的菜单
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-1 10:34 , Processed in 0.117796 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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