Shortcut Menus
Shows a menu when the user clicks a RichTextBox with the right mouse button: the control’s own menu of editing commands, or a menu that the program makes.
RichTextBox1.AutoVerbMenu = True ' Undo, Redo, Cut, Copy, Paste and the like
Asking for a menu
When the user asks for a shortcut menu, with the right mouse button, with Shift+F10 or with the menu key, the control raises ContextMenu first. A handler that sets its Handled to True shows a menu of its own, or none, and the control does nothing more. Otherwise the control raises OLEGetContextMenu, in which the program can give it a menu to show.
With AutoVerbMenu set to True the control shows a menu of its own instead of raising OLEGetContextMenu: Undo, Redo, Cut, Copy, Paste, Paste as plain text, Paste Special and Delete, each available when the command can be done now. The names are in the language of Windows, or in English for a language that the package does not have. The control carries out the command that the user picks.
With AutoVerbMenu False, the default, and no menu given in OLEGetContextMenu, the control shows no menu.
A menu of the program’s own
The handler of OLEGetContextMenu makes a popup menu with the functions of Windows, and sets hMenu to it. The control shows it and destroys it afterwards. When the user picks an item, the control raises OLEContextMenuClick with the item’s identifier.
Private Sub RichTextBox1_OLEGetContextMenu(ByVal SelType As Integer, ByVal LpOleObject As LongPtr, ByVal SelStart As Long, ByVal SelEnd As Long, hMenu As LongPtr)
If SelEnd > SelStart Then ' only for a selection
hMenu = RtbCreatePopupMenu()
RtbAppendMenu hMenu, 0, 1, StrPtr("Upper case")
RtbAppendMenu hMenu, 0, 2, StrPtr("Lower case")
End If
End Sub
Private Sub RichTextBox1_OLEContextMenuClick(ByVal ID As Long)
Select Case ID
Case 1: RichTextBox1.SelText = UCase$(RichTextBox1.SelText)
Case 2: RichTextBox1.SelText = LCase$(RichTextBox1.SelText)
End Select
End Sub
RtbCreatePopupMenu and RtbAppendMenu in this sample are the Windows functions CreatePopupMenu and AppendMenuW, declared by the program.
Properties
AutoVerbMenu
Whether the control shows its own menu of editing commands. Boolean. Default: False. See Asking for a menu.
Events
ContextMenu
Raised when the user asks for a shortcut menu, before the control shows a menu or raises OLEGetContextMenu.
Syntax: object_ContextMenu( Handled As Boolean, ByVal X As Single, ByVal Y As Single )
- Handled
- False when the event is raised. A handler that sets it to True keeps the control from showing a menu.
- X, Y
- The place of the click, measured from the top left corner of the box in the scale units of the control’s container. Both are -1 when the user asked with the keyboard.
OLEContextMenuClick
Raised when the user picks an item of the menu that a handler of OLEGetContextMenu gave the control.
Syntax: object_OLEContextMenuClick( ByVal ID As Long )
- ID
- The identifier that the program gave the item.
OLEGetContextMenu
Raised when the control needs a shortcut menu, while AutoVerbMenu is False.
Syntax: object_OLEGetContextMenu( ByVal SelType As Integer, ByVal LpOleObject As LongPtr, ByVal SelStart As Long, ByVal SelEnd As Long, hMenu As LongPtr )
- SelType
- What the selection holds, a sum of the members of RtfSelTypeConstants.
- LpOleObject
- The address of the IOleObject interface of the selected OLE object, or 0 when no object is selected.
- SelStart, SelEnd
- The place of the first selected character and the place after the last, as in SelChange.
- hMenu
- 0 when the event is raised. A handler sets it to the handle of a popup menu for the control to show; with 0 the control shows none.
See Also
- RichTextBox control
- Clipboard and Undo – the commands of the control’s own menu