Date: 02-16-2022
Return to Index
created by gbSnippets
'When performing replacement of text, a common approach is to select the text
'and then use the SCI_REPLACESEL message to replace the text. However, this
'may cause unwanted display updates, including scroling and flickering.
'Scintilla offers a "target" concept, which allows the replacement of a range of text
'without requiring that the range be seleced.
'This snippet demonstrates that approach.
Searching can be performed within the target Range with SCI_SEARCHINTARGET, which uses a counted String to allow searching For null characters. It returns the position of the start of the matching Text Range Or -1 For failure, in which Case the target is Not moved. The flags used by SCI_SEARCHINTARGET such as SCFIND_MATCHCASE, SCFIND_WHOLEWORD, SCFIND_WORDSTART, AND SCFIND_REGEXP can be Set with SCI_SETSEARCHFLAGS. SCI_SEARCHINTARGET may be simpler For some clients to use than SCI_FINDTEXT, as that requires using a pointer to a structure.
'Primary Code:
'The range of the target text must first be set
SendMessage hSci, %SCI_SetTargetStart, iStart, 0
SendMessage hSci, %SCI_SetTargetStart, iEnd, 0
'Then, replace the target text with this
txt = "New Text" + Chr$(0)
SendMessage hSci, %SCI_ReplaceTarget, iEnd - iStart + 1, StrPTR(txt)
'Compilable Example: (Jose Includes)
'This snippet simply replaces the 1st five characters with "12345".
#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 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, "Replace", 10,10,70,20, %WS_Child Or %WS_Visible
Control Add Button, hDlg, %ID_BtnB, "Remove", 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
txt = "Select Case var$ 'first line" + $CrLf + "End Select 'last line" + Chr$(0)
Select Case CB.Msg
Case %WM_InitDialog
InitializeScintilla
PostMessage hSci, %SCI_SetSel, 0,0 'unselect initially
Case %WM_Command
Select Case CB.Ctl
Case %ID_BtnA : TestA
Case %ID_BtnB : TestB
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 = "abcde fghij" + $CrLf + "lklmn opqrst" + Chr$(0)
SendMessage hSci, %SCI_SetText, 0, StrPTR(txt)
SendMessage hSci, %SCI_SetMarginWidthN, 0, 20
End Sub
Sub TestA
'replace
Control Set Focus hDlg, %ID_Sci
Local iStart, iEnd, iReturn As Long
Local txt As String
iStart = 0
iEnd = 5
txt = "12345" + Chr$(0)
SendMessage hSci, %SCI_SetTargetStart, iStart, 0
SendMessage hSci, %SCI_SetTargetEnd, iEnd, 0
iReturn = SendMessage( hSci, %SCI_ReplaceTarget, -1, StrPTR(txt))
SendMessage hSci, %SCI_GoToPos, iEnd + 1, 0
End Sub
Sub TestB
'delete
Control Set Focus hDlg, %ID_Sci
Local iStart, iEnd, iReturn As Long
Local txt As String
iStart = 0
iEnd = 5
txt = "" + Chr$(0)
SendMessage hSci, %SCI_SetTargetStart, iStart, 0
SendMessage hSci, %SCI_SetTargetEnd, iEnd, 0
iReturn = SendMessage( hSci, %SCI_ReplaceTarget, -1, StrPTR(txt))
End Sub
'gbs_00671
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm