Date: 02-16-2022
Return to Index
created by gbSnippets
'By default, Scintilla provides a context menu with standard cut/copy/paste
'(and other) options. You can turn it off and use a context menu of your own.
'This does NOT require the use of sub-classing (as would overriding the
'context menu of a RichEdit or text control).
'Primary Code:
'Use these messages to enable/disable the default context menu:
SendMessage hSci, %SCI_UsePopup, 0, 0 'turn OFF context menu
SendMessage hSci, %SCI_UsePopup, 1, 0 'turn ON context menu
'Then, creating a custom context menu can be done with standard PowerBASIC
'statements, such as these statements:
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
'Use this to call up the custom context menu:
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
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "scintilla_gb.inc"
%ID_Sci = 1000 : %ID_BtnA = 1001 : %ID_BtnB = 1002
Global hDlg, hSci, hLib, hContext As DWord
Function PBMain() As Long
hLib = LoadLibrary("SCILEXER.DLL")
Dialog New Pixels, 0, "Scintilla Example",300,300,300,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %ID_BtnA, "Use Custom", 10,10,70,20, %WS_Child Or %WS_Visible
Control Add Button, hDlg, %ID_BtnB, "Use Default", 10,40,70,20, %WS_Child Or %WS_Visible
Control Add "Scintilla", hDlg, %ID_Sci, "", 100,10,180,130, %WS_Child Or %WS_Visible
Control Handle hDlg, %ID_Sci To hSci 'get handle to Scintilla window
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local txt As String, x,y As Long
txt = "Select Case var$ 'first line" + $CrLf + "End Select 'last line" + Chr$(0)
Select Case CB.Msg
Case %WM_InitDialog
CreateCustomPopupMenu
InitializeScintilla
PostMessage hSci, %SCI_SetSel, 0,0 'unselect initially
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
Case %WM_Command
Select Case CB.Ctl
Case %ID_BtnA : TestA
Case %ID_BtnB : TestB
Case 500 : ? "Jump"
Case 501 : ? "Shout"
Case 502 : ? "Sing"
Case 503 : ? "Dance"
End Select
Case %WM_Size
Control Set Size hDlg, %ID_Sci, Lo(Word, CB.lParam)-110, Hi(Word, CB.lParam)-20
Case %WM_Destroy
If hLib Then FreeLibrary hLib 'free the Scintilla library
End Select
End Function
Sub InitializeScintilla
Local txt As String
txt = "If x = 2 Then" + $CrLf + " 'do nothing" + $Crlf
txt = txt + "Else" + $crlf + " x = 0" + $crlf + "End If" + Chr$(0)
SendMessage hSci, %SCI_SetText, 0, StrPTR(txt)
SendMessage hSci, %SCI_SetMarginWidthN, 0, 20
Control Set Focus hDlg, %ID_Sci 'focus
End Sub
Sub TestA
SendMessage hSci, %SCI_UsePopup, 0, 0 'turn OFF context menu
End Sub
Sub TestB
SendMessage hSci, %SCI_UsePopup, 1, 0 'turn ON context menu
End Sub
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
'gbs_00633
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm