1 out of 3 rated this helpful - Rate this topic

IDocHostUIHandler::ShowContextMenu Method

Enables MSHTML to display a shortcut menu.

Syntax


HRESULT ShowContextMenu(
    DWORD dwID,
    POINT *ppt,
    IUnknown *pcmdtReserved,
    IDispatch *pdispReserved
);

Parameters

dwID
[in] A DWORD that specifies the identifier of the shortcut menu to be displayed. These values are defined in Mshtmhst.h.
CONTEXT_MENU_DEFAULT
The default shortcut menu for a Web page.
CONTEXT_MENU_IMAGE
Shortcut menu for images.
CONTEXT_MENU_CONTROL
Shortcut menu for scrollbars and select elements.
CONTEXT_MENU_TABLE
Not used.
CONTEXT_MENU_TEXTSELECT
Shortcut menu for selected text.
CONTEXT_MENU_ANCHOR
Shortcut menu for hyperlinks.
CONTEXT_MENU_UNKNOWN
Not used.
CONTEXT_MENU_VSCROLL
Shortcut menu for vertical scroll bar.
CONTEXT_MENU_HSCROLL
Shortcut menu for horizontal scroll bar.
CONTEXT_MENU_MEDIA
Windows Internet Explorer 9 and later. Shortcut menu for media element controls.
ppt
[in] A pointer to a POINT structure containing the screen coordinates for the menu.
pcmdtReserved
[in] A pointer to the IUnknown of an IOleCommandTarget interface used to query command status and execute commands on this object.
pdispReserved
[in] A pointer to an IDispatch interface of the object at the screen coordinates specified in ppt. This enables a host to pass particular objects, such as anchor tags and images, to provide more specific context.

Return Value

Returns one of the following values.

S_OK Host displayed its UI. MSHTML will not attempt to display its UI.
S_FALSE Host did not display its UI. MSHTML will display its UI.
DOCHOST_E_UNKNOWN Menu identifier is unknown. MSHTML might attempt an alternative identifier from a previous version.

Remarks

In Microsoft Internet Explorer 4.0, the pdispReserved parameter supplied no information. In Internet Explorer 5 and later, the parameter contains the pointer to an IDispatch interface.

Examples

The following example shows how to prevent all shortcut menus except the text selection and control menus.


HRESULT CMyUIHandler::ShowContextMenu(
    DWORD dwID,
    POINT *ppt,
    IUnknown *pcmdtReserved,
    IDispatch *pdispReserved)
{
    // Do not show context menus by default with S_OK.
    HRESULT hr(S_OK);

    // If text select or control menu, then return S_FALSE to show menu.
    if( dwID == CONTEXT_MENU_TEXTSELECT || 
        dwID == CONTEXT_MENU_CONTROL)
        hr = S_FALSE;

    return hr;
}

The following code example shows how to use the pdispReserved parameter to access the interface of the object targeted by the context menu. The code queries for an IHTMLElement interface, and displays the value of its tagName property as debug output.


if (pdispReserved)
{
    IHTMLElement *pElem;
    HRESULT hr = pdispReserved->QueryInterface(
                IID_IHTMLElement, 
                (void**)&pElem);
    if (SUCCEEDED (hr))
    {
        BSTR bstr;
        pElem->get_tagName(&bstr);
        USES_CONVERSION;
        ATLTRACE("TagName:%s\n", OLE2T(bstr));
        SysFreeString(bstr);
        pElem->Release();
    }
} 

See Also

About the Browser: Controlling the Context Menus
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
About the dwID code
The dwID code in your msdn may be out of date. Refer to the defines in mshtmhst.h instead.
dwID incorrectly documented
The dwID argument does not appear to be documented correctly. If I select some text and right-click, dwID comes in as 0x04, which is the value of CONTEXT_MENU_TEXTSELECT documented in mshtmhst.h. However, this documentation says the value should be 0x10 which is incorrect.

Edit: The docs above have now been fixed.
Example result
To the example: in bstr is always "BODY" in CHtmlView::ShowContextMenu. Is value of pdispReserved in CHtmlView version correct?