Share via


PopupMenu.ShowForSelectionAsync 方法

定义

重载

ShowForSelectionAsync(Rect)

显示指定选定内容上方的上下文菜单。

ShowForSelectionAsync(Rect, Placement)

显示相对于指定选定内容的首选放置中的上下文菜单。

ShowForSelectionAsync(Rect)

显示指定选定内容上方的上下文菜单。

public:
 virtual IAsyncOperation<IUICommand ^> ^ ShowForSelectionAsync(Rect selection) = ShowForSelectionAsync;
/// [Windows.Foundation.Metadata.Overload("ShowAsyncWithRect")]
IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect const& selection);
[Windows.Foundation.Metadata.Overload("ShowAsyncWithRect")]
public IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect selection);
function showForSelectionAsync(selection)
Public Function ShowForSelectionAsync (selection As Rect) As IAsyncOperation(Of IUICommand)

参数

selection
Rect

相对于窗口,所选矩形) 的 DIP 中 (坐标。 上下文菜单直接放置在此矩形的上方并居中,因此不会涵盖所选内容。

注意

对于 VB、C# 和 C++,此窗口是与调用上下文菜单的线程关联的 CoreWindow

返回

一个 IUICommand 对象,该对象表示在 ShowForSelectionAsync 调用完成后由用户调用的上下文菜单命令。

如果未调用任何命令,ShowForSelectionAsync 将返回 null

属性

示例

必须先为 oncontextmenu 事件添加事件侦听器,然后才能显示上下文菜单。 例如, 上下文菜单示例 侦听特定 HTML 元素上的 事件,然后调用 函数 scenario1AttachmentHandler

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

此外, 上下文菜单示例 使用两个帮助程序函数 (getSelectionRectgetclientCoordinates) 来设置选择矩形的坐标。

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

注解

可以在 上下文菜单示例中查看演示如何创建和自定义上下文菜单的完整代码示例。

另请参阅

适用于

ShowForSelectionAsync(Rect, Placement)

显示相对于指定选定内容的首选放置中的上下文菜单。

public:
 virtual IAsyncOperation<IUICommand ^> ^ ShowForSelectionAsync(Rect selection, Placement preferredPlacement) = ShowForSelectionAsync;
/// [Windows.Foundation.Metadata.Overload("ShowAsyncWithRectAndPlacement")]
IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect const& selection, Placement const& preferredPlacement);
[Windows.Foundation.Metadata.Overload("ShowAsyncWithRectAndPlacement")]
public IAsyncOperation<IUICommand> ShowForSelectionAsync(Rect selection, Placement preferredPlacement);
function showForSelectionAsync(selection, preferredPlacement)
Public Function ShowForSelectionAsync (selection As Rect, preferredPlacement As Placement) As IAsyncOperation(Of IUICommand)

参数

selection
Rect

相对于窗口,所选矩形) 的 DIP 中 (坐标。

注意

对于 VB、C# 和 C++,此窗口是与调用上下文菜单的线程关联的 CoreWindow

preferredPlacement
Placement

上下文菜单相对于选择矩形的首选位置。

如果菜单适合窗口且未覆盖所选内容,则上下文菜单将定位在 preferredPlacement 中。 如果上下文菜单不适合首选位置,则使用不涵盖所选内容的另一个放置。 如果上下文菜单不适合其他任何位置,则使用部分或全部覆盖所选内容的放置。

返回

一个 IUICommand 对象,该对象表示在 ShowForSelectionAsync 调用完成后由用户调用的上下文菜单命令。

如果未调用任何命令, ShowForSelectionAsync 将返回 null

属性

示例

必须先为 oncontextmenu 事件添加事件侦听器,然后才能显示上下文菜单。 例如, 上下文菜单示例 侦听特定 HTML 元素上的 事件,然后调用 函数 scenario1AttachmentHandler

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

注解

可以在 上下文菜单示例中查看演示如何创建和自定义上下文菜单的完整代码示例。

另请参阅

适用于