PopupMenu.ShowForSelectionAsync PopupMenu.ShowForSelectionAsync PopupMenu.ShowForSelectionAsync PopupMenu.ShowForSelectionAsync PopupMenu.ShowForSelectionAsync Method

Definition

Overloads

ShowForSelectionAsync(Rect) ShowForSelectionAsync(Rect) ShowForSelectionAsync(Rect) ShowForSelectionAsync(Rect) ShowForSelectionAsync(Rect)

Shows the context menu above the specified selection.

ShowForSelectionAsync(Rect, Placement) ShowForSelectionAsync(Rect, Placement) ShowForSelectionAsync(Rect, Placement) ShowForSelectionAsync(Rect, Placement) ShowForSelectionAsync(Rect, Placement)

Shows the context menu in the preferred placement relative to the specified selection.

ShowForSelectionAsync(Rect) ShowForSelectionAsync(Rect) ShowForSelectionAsync(Rect) ShowForSelectionAsync(Rect) ShowForSelectionAsync(Rect)

Shows the context menu above the specified selection.

public : IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect selection)
IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect selection) const;
public IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect selection)
Public Function ShowForSelectionAsync(selection As Rect) As IAsyncOperation(Of IUICommand)
Windows.UI.Popups.PopupMenu.showForSelectionAsync(selection).done( /* Your success and error handlers */ );

Parameters

selection
Rect Rect Rect

The coordinates (in DIPs) of the selected rectangle, relative to the window. The context menu is placed directly above and centered on this rectangle such that selection is not covered.

Note

For VB, C#, and C++, this window is the CoreWindow associated with the thread that is calling the context menu.

Returns

A IUICommand object that represents the context menu command invoked by the user, after the ShowForSelectionAsync call completes.

If no command is invoked, ShowForSelectionAsync returns null.

Examples

Before you can show a context menu, you must add an event listener for the oncontextmenu event. For example, the Context menu sample listens for the event on specific HTML elements, and then calls the scenario1AttachmentHandler function.

document.getElementById("attachment").addEventListener("contextmenu", attachmentHandler, false);
// We don't want to obscure content, so pass in the position representing the selection area.
menu.showForSelectionAsync(clientToWinRTRect(document.selection.createRange().getBoundingClientRect())).then(function (invokedCommand) {
    if (invokedCommand !== null) {
        switch (invokedCommand.id) {
            case 1: // Copy
                var selectedText = window.getSelection();
                copyTextToClipboard(selectedText);
                var message = "'Copy' button clicked and '" + /*@static_cast(String)*/selectedText + "' copied to clipboard";
                WinJS.log && WinJS.log(message, "sample", "status");
                break;
            case 2: // Highlight
                // Add command handler code here.
                WinJS.log && WinJS.log("'Highlight' button clicked", "sample", "status");
                break;
            case 3: // Look up
                // Add command handler code here.
                WinJS.log && WinJS.log("'Look up' button clicked", "sample", "status");
                break;
            default:
                break;
        }
    } else {
        // The command is null if no command was invoked.
        WinJS.log && WinJS.log("Context menu dismissed", "sample", "status");
    }
});

Additionally, the Context menu sample uses two helper functions (getSelectionRect and getclientCoordinates) to set the coordinates for the selection rectangle.

// Converts from client to WinRT coordinates, which take scale factor into consideration.
function clientToWinRTRect(rect) {
    var zoomFactor = document.documentElement.msContentZoomFactor;
    return {
        x: (rect.left + document.documentElement.scrollLeft - window.pageXOffset) * zoomFactor,
        y: (rect.top + document.documentElement.scrollTop - window.pageYOffset) * zoomFactor,
        width: rect.width * zoomFactor,
        height: rect.height * zoomFactor
    };
}

Remarks

You can see complete code examples that demonstrate how to create and customize context menu in the Context menu sample on the sample home page.

See also

ShowForSelectionAsync(Rect, Placement) ShowForSelectionAsync(Rect, Placement) ShowForSelectionAsync(Rect, Placement) ShowForSelectionAsync(Rect, Placement) ShowForSelectionAsync(Rect, Placement)

Shows the context menu in the preferred placement relative to the specified selection.

public : IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect selection, Placement preferredPlacement)
IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect selection, Placement preferredPlacement) const;
public IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect selection, Placement preferredPlacement)
Public Function ShowForSelectionAsync(selection As Rect, preferredPlacement As Placement) As IAsyncOperation(Of IUICommand)
Windows.UI.Popups.PopupMenu.showForSelectionAsync(selection, preferredPlacement).done( /* Your success and error handlers */ );

Parameters

selection
Rect Rect Rect

The coordinates (in DIPs) of the selected rectangle, relative to the window.

Note

For VB, C#, and C++, this window is the CoreWindow associated with the thread that is calling the context menu.

preferredPlacement
Placement Placement Placement

The preferred placement of the context menu relative to the selection rectangle.

The context menu is positioned in the preferredPlacement if the menu fits in the window and does not cover the selection. If the context menu does not fit in the preferred placement, another placement that does not cover the selection is used. If the context menu does not fit anywhere else, a placement that partially or wholly covers the selection is used.

Returns

A IUICommand object that represents the context menu command invoked by the user, after the ShowForSelectionAsync call completes.

If no command is invoked, ShowForSelectionAsync returns null.

Examples

Before you can show a context menu, you must add an event listener for the oncontextmenu event. For example, the Context menu sample listens for the event on specific HTML elements, and then calls the scenario1AttachmentHandler function.

document.getElementById("attachment").addEventListener("contextmenu", attachmentHandler, false);
// Converts from client to WinRT coordinates, which take scale factor into consideration.
function clientToWinRTRect(rect) {
    var zoomFactor = document.documentElement.msContentZoomFactor;
    return {
        x: (rect.left + document.documentElement.scrollLeft - window.pageXOffset) * zoomFactor,
        y: (rect.top + document.documentElement.scrollTop - window.pageYOffset) * zoomFactor,
        width: rect.width * zoomFactor,
        height: rect.height * zoomFactor
    };
}

Remarks

You can see complete code examples that demonstrate how to create and customize context menu in the Context menu sample on the sample home page.

See also