Share via


Working with Context Menus: Implementation Details

To add context menu items using IExtendContextMenu

  1. Implement the IExtendContextMenu interface and its two methods, AddMenuItems and Command.

    To add context menu items to the context menu of a scope item, the snap-in's IComponentData implementation should implement and expose the IExtendContextMenu interface.

    To add context menu items to the context menu of a result item, the snap-in's IComponent implementation responsible for that result item and its result pane should implement and expose the IExtendContextMenu interface.

    Also, if the user selects a scope item and then displays its context menu, MMC will give both the snap-in's IComponentData and IComponent (that owns the current view) implementations the opportunity to add menu items. MMC calls the IExtendContextMenu::AddMenuItems method implemented by the snap-in's IComponent to allow the snap-in to add menu items to the View menu. MMC calls the IExtendContextMenu::AddMenuItems method implemented by the snap-in's IComponentData to allow the snap-in to add menu items to all other menus. Only the snap-in's IComponent implementation can add items to the View menu.

    If the user displays a scope item's context menu without first selecting the scope item, MMC will only give the snap-in's IComponentData implementation the opportunity to add menu items to all menus except the View menu. Consequently, the View menu only appears for a scope item if the user first selects an item.

  2. In the snap-in's implementation of AddMenuItems:

    • MMC specifies in the pInsertionAllowed parameter passed in the call to AddMenuItems the insertion points at which the snap-in can add context menu items. The snap-in should check the pInsertionAllowed flags for permission before attempting to add menu items at the MMC-defined insertion points. For example, a snap-in should not add menu items to CCM_INSERTIONPOINTID_PRIMARY_NEW or CCM_INSERTIONPOINTID_3RDPARTY_NEW unless the CCM_INSERTIONALLOWED_NEW flag is set.

      A primary snap-in is permitted to reset any of the insertion flags in its AddMenuItems method as a way of restricting the kinds of menu items that a third-party extension can add. For example, the primary snap-in can clear the CCM_INSERTIONALLOWED_NEW flag to prevent extensions from adding their own New menu items.

      The primary snap-in should not attempt to set bits in pInsertionAllowed that were originally cleared. This is because future versions of MMC may use bits that are not currently defined. Third-party extensions should not attempt to change pInsertionAllowed at all.

    • For each item that the snap-in must add at an MMC-defined insertion point or at another insertion point determined by the snap-in, fill a CONTEXTMENUITEM structure. For more information, see CONTEXTMENUITEM.

    • For each item, add the item (its CONTEXTMENUITEM structure) to the context menu of the selected scope or result item by calling AddItem on the IContextMenuCallback interface passed in the piCallback parameter in the call to the snap-in's IExtendContextMenu::AddMenuItems method.

  3. In the snap-in's implementation of Command:

    • MMC calls Command with the same command identifier (lCommandID) that the snap-in assigned to the item in its CONTEXTMENUITEM structure when it added the item. The snap-in can process the command and then return S_OK.
    • MMC reserves negative-valued command IDs for predefined menu command IDs that it sends to a snap-in's Command method. The –1 command ID is the MMCC_STANDARD_VIEW_SELECT enumerator value defined in mmc.h. This is sent to Command when the user clicks a standard view command on the View menu (Large, Small, List, or Detail). This notifies the snap-in that the user is switching away from a custom view (OCX, HTML). After getting an MMCC_STANDARD_VIEW_SELECT command, the snap-in should request a standard view the next time its IComponent::GetResultViewType method is called and not request a custom view until one of its custom view menu items is selected. If the snap-in only uses standard views or only uses custom views, it can ignore the MMCC_STANDARD_VIEW_SELECT command.

Working with Context Menus

Working with Context Menus: Interfaces