方法: カスタム コンテキスト メニューを RichTextBox に配置する
この例では、カスタム コンテキスト メニューを RichTextBox に配置する方法を示します。
RichTextBox のカスタム コンテキスト メニューを実装する場合、コンテキスト メニューの配置の処理を行う必要があります。 既定では、RichTextBox の中央でカスタム コンテキスト メニューが開きます。
ContextMenuOpening イベントのリスナーを追加する
既定の配置時の動作をオーバーライドするには、ContextMenuOpening イベントのリスナーを追加します。 次の例では、プログラムによってこれを実行する方法を説明します。
richTextBox.ContextMenuOpening += new ContextMenuEventHandler(richTextBox_ContextMenuOpening);
AddHandler richTextBox.ContextMenuOpening, AddressOf richTextBox_ContextMenuOpening
ContextMenuOpening イベント リスナーの実装
次の例は、対応する ContextMenuOpening イベント リスナーの実装を示しています。
// This method is intended to listen for the ContextMenuOpening event from a RichTextBox.
// It will position the custom context menu at the end of the current selection.
void richTextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
// Sender must be RichTextBox.
RichTextBox rtb = sender as RichTextBox;
if (rtb == null) return;
ContextMenu contextMenu = rtb.ContextMenu;
contextMenu.PlacementTarget = rtb;
// This uses HorizontalOffset and VerticalOffset properties to position the menu,
// relative to the upper left corner of the parent element (RichTextBox in this case).
contextMenu.Placement = PlacementMode.RelativePoint;
// Compute horizontal and vertical offsets to place the menu relative to selection end.
TextPointer position = rtb.Selection.End;
if (position == null) return;
Rect positionRect = position.GetCharacterRect(LogicalDirection.Forward);
contextMenu.HorizontalOffset = positionRect.X;
contextMenu.VerticalOffset = positionRect.Y;
// Finally, mark the event has handled.
contextMenu.IsOpen = true;
e.Handled = true;
}
' This method is intended to listen for the ContextMenuOpening event from a RichTextBox.
' It will position the custom context menu at the end of the current selection.
Private Sub richTextBox_ContextMenuOpening(ByVal sender As Object, ByVal e As ContextMenuEventArgs)
' Sender must be RichTextBox.
Dim rtb As RichTextBox = TryCast(sender, RichTextBox)
If rtb Is Nothing Then
Return
End If
Dim contextMenu As ContextMenu = rtb.ContextMenu
contextMenu.PlacementTarget = rtb
' This uses HorizontalOffset and VerticalOffset properties to position the menu,
' relative to the upper left corner of the parent element (RichTextBox in this case).
contextMenu.Placement = PlacementMode.RelativePoint
' Compute horizontal and vertical offsets to place the menu relative to selection end.
Dim position As TextPointer = rtb.Selection.End
If position Is Nothing Then
Return
End If
Dim positionRect As Rect = position.GetCharacterRect(LogicalDirection.Forward)
contextMenu.HorizontalOffset = positionRect.X
contextMenu.VerticalOffset = positionRect.Y
' Finally, mark the event has handled.
contextMenu.IsOpen = True
e.Handled = True
End Sub
関連項目
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示