Professional UI Solutions
Site Map   /  Register
 
 

Forum

Please Log In to post a new message or reply to an existing one. If you are not registered, please register.

NOTE: Some forums may be read-only if you are not currently subscribed to our technical support services.

Forums » Prof-UIS Tech Support » Inserting menu items at runtime Collapse All
Subject Author Date
rajneesh May 3, 2003 - 5:28 AM

Hi,
I am inserting menu items in CExtMenuControlBar in my CMainFrame derived from CMDIFrameWnd at run time. To handle these menu command i have override the OnCommand virtual method of CMDIFrameWnd. The problem is that the menu items which i insert at run time are disabled and the menu bitmaps are not visible. The following code snipplet describes what i am doing.
Please suggest some way to enable the menu item and show the menu bitmap.

class CMainFrame : public CMDIFrameWnd
{
    CExtMenuControlBar m_wndMenuBar;
    UINT g_nNextCommandID;

    BOOL InsertCustomMenu(LPCTSTR MenuTitle, LPCTSTR MenuItemTitle, HBITMAP MenuBitmap)
    {
        CMenu *pWindowMenu=m_wndMenuBar.GetMenu();
        CMenu *pMainMenu=new CMenu;

        if(pWindowMenu && pMainMenu->CreatePopupMenu())
        {
            pMainMenu->AppendMenu(MF_STRING, g_nNextCommand, MenuItemTitle))
            ::SetMenuItemBitmaps(pMainMenu->m_hMenu, g_nNextCommand, MF_BYCOMMAND, MenuBitmap, NULL);

            pWindowMenu->AppendMenu(MF_POPUP, (UINT)pMainMenu->m_hMenu, MenuTitle);

            g_nNextCommand++;
            m_wndMenuBar.UpdateMenuBar();

            return TRUE;
        }

        return FALSE;
    }

    virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam)
    {
        TRACE("COMAND : %d", wParam);
        return CMDIFrameWnd::OnCommand(wParam, lParam);
    }
}

Customer Support Team May 3, 2003 - 9:39 AM

Dear Rajneesh,

The technique you described cannot be used for inserting new menu items.

Instead you should override the CExtPopupMenuWnd::g_nMsgPrepareMenu registered message in the frame window so that you can modify menus. This message is generated each time any popup menu appears on the screen. Please add this line to the frame’s message map:

ON_REGISTERED_MESSAGE(CExtPopupMenuWnd::g_nMsgPrepareMenu,OnExtMenuPrepare)

You also should add this method to your frame window:

LRESULT CMainFrame::OnExtMenuPrepare(WPARAM wParam, LPARAM lParam)

{

lParam;

CExtPopupMenuWnd::MsgPrepareMenuData_t * pData =

reinterpret_cast< CExtPopupMenuWnd::MsgPrepareMenuData_t * >( wParam );

ASSERT( pData != NULL );

CExtPopupMenuWnd * pPopup = pData->m_pPopup;

ASSERT( pPopup != NULL );

// HERE IS YOUR CODE TO MODIFY pPopup (insert or remove anything)

}

My suggestion is to add some commands to the menu in resources which will be replaced with other commands in the CMainFrame::OnExtMenuPrepare() method. If you don’t need to replace these commands, you may simply remove them.

For example, if we have added an ID_MY_REPLACE_CMD command to the File menu, you should use the following code in the CMainFrame::OnExtMenuPrepare() method:

INT nReplacePos = pPopup->ItemFindPosForCmdID(ID_MY_REPLACE_CMD);

if( nReplacePos >= 0 )

{

// USER SHOULD NOT SEE THIS COMMAND

VERIFY( pPopup->ItemRemove(nReplacePos) );

// YOU ALSO MAY INSERT SOME COMMANDS TO THIS POSITION

VERIFY( pPopup->ItemInsert( (UINT)CExtPopupMenuWnd::TYPE_SEPARATOR, nReplacePos) );

VERIFY( pPopup->ItemInsert( ID_SOME_COMMAND, nReplacePos) );

}

Please note, all commands should be registered in the command manager.The DRAWCLI sample carefully uses this technique to handle OLE menus and insert color-popup submenus.

If you encounter any difficulties, please send us your sample application to support@fossware.com so we can give you a solution.