Date: 02-16-2022
Return to Index
created by gbSnippets
'A RichEdit control has no built-in cut/copy/paste context menu, as does
'the textbox control. The %WM_ContextMenu message can be used to
'create a custom context menu and is available in the dialog Callback.
'Subclassing is Not required.
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'Use this code in the Callback function.
Case %WM_ContextMenu
x = Lo(Integer,CB.lParam) : y = Hi(Integer, CB.lParam) 'WM_ContextMenu returns xy coordinates of mouse
TrackPopupMenu hContext, %TPM_LEFTALIGN, x, y, 0, hDlg, ByVal 0 'put context menu where mouse is
Function = 0 : Exit Function
'And create a popup menu like this:
Sub CreateCustomPopUpMenu
Menu New Popup To hContext
Menu Add String, hContext, "Jump", 500, %MF_Enabled
Menu Add String, hContext, "Shout", 501, %MF_Enabled
Menu Add String, hContext, "Sing", 502, %MF_Enabled
Menu Add String, hContext, "Dance", 503, %MF_Enabled
End Sub
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
'The %WM_ContextMenu is used to display a popup menu over the RichEdit control.
'Note that the popup menu can be placed anywhere on the screen, so coordinates
'must be provided to place it within the dialog (usually by the mouse).
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "RichEdit.inc"
Global hDlg as Dword, hRichEdit as Dword, hContext as Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,150, %WS_OverlappedWindow To hDlg
LoadLibrary("riched32.dll") : InitCommonControls
Control Add "RichEdit", hDlg, 500, "",20,40,160,100, 1345393092 'style& is the integer value of common styles
Control Handle hDlg, 500 To hRichEdit : Control Set Text hDlg, 500, "This is sample" + $CrLf + "text for the" + $CrLf + "RichEdit control."
CreateCustomPopupMenu
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local x as Long, y as Long
Select Case CB.Msg
Case %WM_INITDIALOG
PostMessage GetDlgItem(hDlg, 500), %EM_EXSETSEL, 0,0 'removes selection on startup
Case %WM_ContextMenu
x = Lo(Integer,CB.lParam) : y = Hi(Integer, CB.lParam) 'WM_ContextMenu returns xy coordinates of mouse
TrackPopupMenu hContext, %TPM_LEFTALIGN, x, y, 0, hDlg, ByVal 0 'put context menu where mouse is
Function = 0 : Exit Function
Case %WM_Command
Select Case CB.Ctl
Case 700 : MsgBox "Jump Selected"
Case 701 : MsgBox "Shout Selected"
Case 702 : MsgBox "Sing Selected"
Case 703 : MsgBox "Dance Selected"
End Select
End Select
End Function
Sub CreateCustomPopUpMenu
Menu New Popup To hContext
Menu Add String, hContext, "Jump", 700, %MF_Enabled
Menu Add String, hContext, "Shout", 701, %MF_Enabled
Menu Add String, hContext, "Sing", 702, %MF_Enabled
Menu Add String, hContext, "Dance", 703, %MF_Enabled
End Sub
'gbs_00224
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm