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 General Discussion » Context menu - enable/disable menu items Collapse All
Subject Author Date
Bacsó Gergely Dec 30, 2005 - 7:48 AM


Hi,


I have the following code for displaying context menu:


 


CExtPopupMenuWnd* pWndMenu = new CExtPopupMenuWnd;


pWndMenu->UpdateFromMenu(m_hWnd, &m_Menu, false);


pWndMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y);


 


How can I enable/disable the menu items here without using the ON_UPDATE_COMMAND_UI message routing?


And is it true that I don’t have to delete pWndMenu?

Technical Support Dec 31, 2005 - 8:37 AM

First of all we should note that the CExtPopupMenuWnd::TrackPopupMenu() method uses its own menu tracking flags which are a set of TPMX_*** constants defined in the ExtPopupMenuWnd.h file. These constants do not intersect with Win32 TPM_*** flags. So, please do not forget to fix this issue in your code.

It is possible to use popup menu without MFC’s command updating mechanism. Just specify true for the bNoRefToCmdMngr parameter when calling the CExtPopupMenuWnd::UpdateFromMenu() method. The constructed popup menu will contain the menu items which have no reference to the command manager nor use the MFC’s command updating mechanism. After that you can traverse menu items and change their states. Here is the list of most often used methods of the CExtPopupMenuWnd class when coding a task like yours:

virtual INT ItemGetCount() const;
virtual UINT ItemGetCmdID( // menu_item_type_t values can be returned
    INT nPos
    ) const;
virtual CExtSafeString ItemGetText( INT nPos ) const;
virtual CExtSafeString ItemGetAccelText( INT nPos ) const;
virtual INT ItemFindPosForCmdID(
    UINT nCmdID,
    INT nPosStart = -1
    ) const;
virtual bool ItemSetPopupIcon(
    INT nPos,
    HICON hIcon = NULL // no icon by default
    );
virtual bool ItemSetPopupText(
    INT nPos,
    __EXT_MFC_SAFE_LPCTSTR sText // NULL if empty
    );
virtual bool ItemSetPopupAccelText(
    INT nPos,
    __EXT_MFC_SAFE_LPCTSTR sText // NULL if empty
    );
virtual CExtPopupMenuWnd * ItemGetPopup(
    INT nPos
    );
virtual const CExtPopupMenuWnd * ItemGetPopup(
    INT nPos
    ) const;
virtual bool ItemEnabledGet(
    INT nPos
    ) const;
virtual void ItemEnabledSet(
    INT nPos,
    bool bEnabled = true
    );
virtual bool ItemBoldGet(
    INT nPos
    ) const;
virtual void ItemBoldSet(
    INT nPos,
    bool bBold = true
    );
virtual bool ItemDefaultGet(
    INT nPos
    ) const;
virtual void ItemDefaultSet(
    INT nPos,
    bool bDefault = true
    );
INT ItemDefaultFind() const;
virtual INT ItemFindByText(
    __EXT_MFC_SAFE_LPCTSTR sText,
    INT nStartIdx = -1,
    BOOL bRestartAt0 = TRUE
    ) const;
You can also get a reference to the CExtPopupMenuWnd::MENUITEMDATA data structure which describes any menu item and invokes its methods directly:
CExtPopupMenuWnd::MENUITEMDATA & ItemGetInfo( INT nPos );
const CExtPopupMenuWnd::MENUITEMDATA & ItemGetInfo( INT nPos ) const;

Jayender S Dec 30, 2005 - 9:34 PM

Guess this should help u ..


// to enable the menu :


pPopup->EnableMenuItem(ID_INSERT, MF_BYCOMMAND|MF_ENABLED);


// to Disable the menu :


pPopup->EnableMenuItem(ID_DELETE, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);



pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x, pt.y,this);


Hope this helps.


thanks,


Jayender.