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 です。

戻り値

ShowForSelectionAsync 呼び出しが完了した後にユーザーによって呼び出されるコンテキスト メニュー コマンドを表す IUICommand オブジェクト。

コマンドが呼び出されない場合、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");
    }
});

さらに、 コンテキスト メニューのサンプル では、2 つのヘルパー関数 (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 に配置されます。 コンテキスト メニューが優先配置に収まらない場合は、選択内容をカバーしない別の配置が使用されます。 コンテキスト メニューが他の場所に収まらない場合は、選択範囲の一部または全部をカバーする配置が使用されます。

戻り値

ShowForSelectionAsync 呼び出しが完了した後にユーザーによって呼び出されるコンテキスト メニュー コマンドを表す IUICommand オブジェクト。

コマンドが呼び出されない場合、 ShowForSelectionAsyncnull を返します。

属性

コンテキスト メニューを表示するには、 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
    };
}

注釈

コンテキスト メニューのサンプルでコンテキスト メニューを作成およびカスタマイズする方法を示す完全なコード例を確認できます。

こちらもご覧ください

適用対象