Right-click Context Menu
by Richard Russell, March 2014
When the user right-clicks in a window a WM_CONTEXTMENU message is generated, which may be intercepted using ON SYS (so long as a *SYS 1 command has been issued). On receipt of this message you should typically display a popup menu containing commands relevant to the context, for example if the user right-clicks in an editing pane it would often be appropriate to display the same menu as results from clicking on Edit in the menu bar. Typical code to achieve that would be as follows:
X% = (lpar% << 16) >> 16 Y% = lpar% >> 16 IF wpar% = hEdit% THEN dummy% = FNtrackpopupmenu(EditMenu%, 0, X%, Y%)
Here wpar% and lpar% correspond to the @wparam% and @lparam% values from the original ON SYS interrupt (typically via an event queue), hEdit% is the window handle of the editing pane and EditMenu% is the handle of the popup edit menu.
The WM_CONTEXTMENU message is also generated when the user presses the Menu key (usually to be found to the right of the space bar). If the message is generated by this means, lpar%, which normally contains the screen coordinates at which the popup menu should be displayed, instead contains -1. In this case you should use an alternative method of determining the location of the context menu, for example in the case of an editing pane it would be appropriate to display the menu at the position of the caret (text cursor) or current selection:
IF lpar% = -1 THEN SYS "SendMessage", hEdit%, EM_GETSEL, ^start%, ^finish% SYS "SendMessage", hEdit%, EM_POSFROMCHAR, ^X%, finish% SYS "ClientToScreen", hEdit%, ^X% ELSE X% = (lpar% << 16) >> 16 Y% = lpar% >> 16 ENDIF IF wpar% = hEdit% THEN dummy% = FNtrackpopupmenu(EditMenu%, 0, X%, Y%)
In the absence of a menu key the keyboard shortcut Shift+F10 may be used, however in the BB4W 'main window' this keypress is intercepted and instead results in the code 170 (&AA) being inserted into the keyboard buffer. If you want Shift+F10 to bring up a context menu you can either respond to that character when it is received by GET or INKEY, or you can create (or add to) a keyboard accelerator in order to translate it to a WM_COMMAND message which can be handled by ON SYS:
nsc% = 1 DIM accel{(nsc%-1) fVirt&, pad&, key{l&,h&}, cmd{l&,h&}} accel{(0)}.fVirt& = FSHIFT + FVIRTKEY accel{(0)}.key.l& = VK_F10 accel{(0)}.cmd.l& = contextID% SYS "CreateAcceleratorTable", accel{(0)}, nsc% TO haccel% @haccel% = haccel% @hwacc% = @hwnd%
Here contextID% is a the command code which will be generated.