I'm trying to add items to the context menu that is present when the user right clicks. I found the AcEdUIContext class and it looks to be what I need but it seems like a lot of overhead just to add a single text item to the menu. Is there a simpler way to add text items to the menu?
I was struggling to get it into my solution properly so I went looking for examples online as there were none in the 2014/2016 SDK but I was unable to find any out in the wild. I did find that there was an example in the 2007 SDK, which I was able to download. I attempted to convert it over to the newer SDKs using both 32 bit and 64 bit releases but I am getting error LNK1104: cannot open file 'acad.lib' I've tried using relative paths, absolute paths, and even defining acad.lib in the additional dependencies as "C:\ObjectARX 2016\lib-x64\acad.lib" I did this both with and without quotation marks. I have attached the modified sample projects. The 2007 SDK is available at http://download.autodesk.com/esd/objectarx/2007/Arx_All.exe
Derive your menu class from AcEdUIContext and implement these functions:
//------------------------------------------------------------------------------ MyMenu::MyMenu(MyTool* pTool) //------------------------------------------------------------------------------ { HINSTANCE hOldResHandle = AfxGetResourceHandle(); AfxSetResourceHandle(pApp->GetResourceHandle()); m_cMenu = new CMenu; BOOL res = m_cMenu->LoadMenu(IDR_MYMENU_POPUP); VERIFY(res); if (!res) { delete m_cMenu; m_cMenu = NULL; } AfxSetResourceHandle(hOldResHandle); m_pTool = pTool; } //------------------------------------------------------------------------------ MyMenu::~MyMenu() //------------------------------------------------------------------------------ { if (m_cMenu) { delete m_cMenu; } } //------------------------------------------------------------------------------ void* MyMenu::getMenuContext(const AcRxClass* classtype, const AcDbObjectIdArray& selectedObjs) //------------------------------------------------------------------------------ { m_hMenu = m_cMenu->GetSubMenu(0)->GetSafeHmenu(); return &m_hMenu; } //------------------------------------------------------------------------------ void MyMenu::onCommand(Adesk::UInt32 cmdId) //------------------------------------------------------------------------------ { m_pTool->OnCommand(cmdId); } //------------------------------------------------------------------------------ void MyMenu::OnUpdateMenu() //------------------------------------------------------------------------------ { }
Then use acedAddObjectContextMenu and acedRemoveObjectContextMenu , something like:
m_pContextMenu = new MyMenu(this); if ((m_pContextMenu->isValid() == false) || (acedAddObjectContextMenu(AcDbBlockReference::desc(), m_pContextMenu, pApp->GetAppPointer()) == false)) { ASSERT(0); delete m_pContextMenu; m_pContextMenu = NULL; } .... if (m_pContextMenu != NULL) { BOOL b = acedRemoveObjectContextMenu(AcDbBlockReference::desc(), m_pContextMenu); delete m_pContextMenu; m_pContextMenu = NULL; }
Also be aware of acedAddDefaultContextMenu and acedRemoveDefaultContextMenu if you don't want to attach the menu to a specific class.
Also remember, if your are just wanting to operate a pre-existing tool or function from the menu you can do that by specifying Shortcut Menus in your CUI file.
I figured out the error I was getting with the example file. I was making changes to the release config but then building in debug. The attached sample will work for anyone who wants to use it, just switch the build type to release.
Still wondering if there is an easier way to add a single menu item without all the MFC overhead?
This appears to be what is being done in the example file? I'm not an expert in MFC so maybe I'm missing something that's different in your code?
No, you are right - I almost certainly got my code from that example too. If it still seems like a lot of trouble though, it is easier to use the CUI menu configuration - especially to run a function or command you have already written.
You'll probably want to make sure your command can use the PICKFIRST selection set though, so that it can run straightaway (provided it needs to operate on an object of course)
https://forums.autodesk.com/t5/objectarx/adding-items-to-the-right-click-context-menu/td-p/6883964
文章评论