ShowForSelectionAsync(Rect, Placement) | showForSelectionAsync(Rect, Placement) method
Collapse the table of content
Expand the table of content

PopupMenu.ShowForSelectionAsync(Rect, Placement) | showForSelectionAsync(Rect, Placement) method

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

Syntax


popupMenu.showForSelectionAsync(selection, preferredPlacement).done( /* Your success and error handlers */ );

Parameters

selection

Type: 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

Type: 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.

Return value

Type: IAsyncOperation<IUICommand>

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.

Remarks

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

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);


The coordinates of the selection rectangle must be scaled according to the dpi of the user's device. The Context menu sample uses two helper functions to populate the selectionRect with properly scaled client coordinates.


// 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
    };
}


In the example, the getSelectionRect function populates the Rect structure with scaled values obtained from the getclientCoordinates function. The coordinates returned by getclientCoordinates are scaled using a simple formula: scaledValue = currentValue * deviceDPI / 96.

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.UI.Popups
Windows::UI::Popups [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Popups
Windows::UI::Popups [C++]

Metadata

Windows.winmd

See also

Adding context menus
Context menu sample
Guidelines and checklist for context menus
Reference
IUICommand
oncontextmenu
PopupMenu
Rect
ShowForSelectionAsync(Rect)
UICommand

 

 

Show:
© 2016 Microsoft