This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
developer:sdksamples:insertpluginmenu [2020/08/14] 127.0.0.1 external edit |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Add a Custom Menu to Rhino' | ||
| - | ====== C++ ====== | ||
| - | > **Summary: | ||
| - | |||
| - | =====Question===== | ||
| - | I am trying to add a submenu to Rhino' | ||
| - | |||
| - | =====Answer===== | ||
| - | To insert a menu item, or a submenu, into Rhino' | ||
| - | |||
| - | - Use CRhinoApp:: | ||
| - | - Use CRhinoPlugIn:: | ||
| - | |||
| - | =====Example===== | ||
| - | The following example command demonstrates how to add and remove a custom menu from Rhino' | ||
| - | |||
| - | |||
| - | <code c++> | ||
| - | //////////////////////////////////////////////////////////////// | ||
| - | // cmdMyMenu.cpp | ||
| - | |||
| - | #include " | ||
| - | #include " | ||
| - | #include " | ||
| - | |||
| - | //////////////////////////////////////////////////////////////// | ||
| - | //////////////////////////////////////////////////////////////// | ||
| - | // | ||
| - | // BEGIN MyMenu command | ||
| - | // | ||
| - | |||
| - | class CCommandMyMenu : public CRhinoCommand | ||
| - | { | ||
| - | public: | ||
| - | CCommandMyMenu() {} | ||
| - | ~CCommandMyMenu() {} | ||
| - | UUID CommandUUID() | ||
| - | { | ||
| - | static const GUID MyMenuCommand_UUID = | ||
| - | { <TODO: add your command uuid here> }; | ||
| - | return MyMenuCommand_UUID; | ||
| - | } | ||
| - | const wchar_t* EnglishCommandName() { return L" | ||
| - | const wchar_t* LocalCommandName() { return L" | ||
| - | CRhinoCommand:: | ||
| - | |||
| - | BOOL LoadMyMenu(); | ||
| - | BOOL UnloadMyMenu(); | ||
| - | |||
| - | private: | ||
| - | CMenu m_menu; | ||
| - | }; | ||
| - | |||
| - | // The one and only CCommandMyMenu object | ||
| - | static class CCommandMyMenu theMyMenuCommand; | ||
| - | |||
| - | CRhinoCommand:: | ||
| - | { | ||
| - | bool bVisible = ( m_menu.GetSafeHmenu() ) ? true : false; | ||
| - | |||
| - | ON_wString prompt; | ||
| - | prompt.Format( L"%s is %s. New value", | ||
| - | EnglishCommandName(), | ||
| - | bVisible ? L" | ||
| - | ); | ||
| - | |||
| - | CRhinoGetOption go; | ||
| - | go.SetCommandPrompt( prompt ); | ||
| - | int s_opt = go.AddCommandOption( RHCMDOPTNAME(L" | ||
| - | int h_opt = go.AddCommandOption( RHCMDOPTNAME(L" | ||
| - | int t_opt = go.AddCommandOption( RHCMDOPTNAME(L" | ||
| - | go.GetOption(); | ||
| - | if( go.CommandResult() != success ) | ||
| - | return go.CommandResult(); | ||
| - | |||
| - | const CRhinoCommandOption* opt = go.Option(); | ||
| - | if( 0 == opt ) | ||
| - | return failure; | ||
| - | |||
| - | if( opt-> | ||
| - | { | ||
| - | if( false == bVisible ) | ||
| - | LoadMyMenu(); | ||
| - | } | ||
| - | else if( opt-> | ||
| - | { | ||
| - | if( true == bVisible ) | ||
| - | UnloadMyMenu(); | ||
| - | } | ||
| - | else | ||
| - | { | ||
| - | if( true == bVisible ) | ||
| - | UnloadMyMenu(); | ||
| - | else | ||
| - | LoadMyMenu(); | ||
| - | } | ||
| - | |||
| - | return success; | ||
| - | } | ||
| - | |||
| - | BOOL CCommandMyMenu:: | ||
| - | { | ||
| - | // Switch the module state so resources are read | ||
| - | // from our plug-in (DLL), not Rhino. | ||
| - | AFX_MANAGE_STATE( AfxGetStaticModuleState() ); | ||
| - | |||
| - | // Try to load our menu resource from our plug-in. | ||
| - | // Note, m_my_menu is a CMenu member variable. | ||
| - | if( 0 == m_menu.GetSafeHmenu() ) | ||
| - | { | ||
| - | if( !m_menu.LoadMenu(IDR_MY_MENU) ) | ||
| - | return FALSE; | ||
| - | } | ||
| - | |||
| - | // Find a location in Rhino' | ||
| - | // menu item. For this example, we will insert our | ||
| - | // menu on the " | ||
| - | // item. | ||
| - | HMENU hParent = 0; | ||
| - | int index = 0; | ||
| - | //if( !RhinoApp().FindRhinoMenuItem(L"& | ||
| - | if( !RhinoApp().FindRhinoMenuItem(L" | ||
| - | { | ||
| - | m_menu.DestroyMenu(); | ||
| - | return FALSE; | ||
| - | } | ||
| - | |||
| - | // Create and initialize a MENUITEMINFO struct. | ||
| - | MENUITEMINFO mi; | ||
| - | memset( &mi, 0, sizeof(mi) ); | ||
| - | mi.cbSize = sizeof(mi); | ||
| - | |||
| - | // Fill in our menu info | ||
| - | mi.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU; | ||
| - | mi.wID = MF_POPUP; | ||
| - | mi.fType = MFT_STRING; | ||
| - | |||
| - | ON_wString wstr = L" | ||
| - | mi.dwTypeData = wstr.Array(); | ||
| - | |||
| - | mi.fState = MFS_ENABLED; | ||
| - | mi.hSubMenu = m_menu.GetSafeHmenu(); | ||
| - | mi.hSubMenu = :: | ||
| - | mi.wID = IDR_MY_MENU; | ||
| - | |||
| - | // Add our menu to Rhino' | ||
| - | BOOL rc = MyTestPlugIn().InsertPlugInItemToRhinoMenu( hParent, index + 1, &mi ); | ||
| - | if( !rc ) | ||
| - | m_menu.DestroyMenu(); | ||
| - | |||
| - | return rc; | ||
| - | } | ||
| - | |||
| - | BOOL CCommandMyMenu:: | ||
| - | { | ||
| - | BOOL rc = FALSE; | ||
| - | |||
| - | // Find our menu item in Rhino' | ||
| - | HMENU hParent = 0; | ||
| - | int index = 0; | ||
| - | if( RhinoApp().FindRhinoMenuItem(L" | ||
| - | { | ||
| - | // Remove our menu item. | ||
| - | if( :: | ||
| - | { | ||
| - | // Redraw Rhino' | ||
| - | DrawMenuBar( RhinoApp().MainWnd() ); | ||
| - | m_menu.DestroyMenu(); | ||
| - | rc = TRUE; | ||
| - | } | ||
| - | } | ||
| - | |||
| - | return rc; | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // END MyMenu command | ||
| - | // | ||
| - | //////////////////////////////////////////////////////////////// | ||
| - | //////////////////////////////////////////////////////////////// | ||
| - | |||
| - | </ | ||
| - | |||
| - | |||
| - | {{tag> | ||