Date: 02-16-2022
Return to Index
created by gbSnippets
'This code shows how to select various combination of text - all text, the
'current line, the current line, line M, or lines M-N. Line numbers start at 0.
'Both DDT and direct API solutions are provided.
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'Because of size, only a single copy of the primary code is shown, as part
'of the compilable example below. All code is shown as Functions.
'But, in summary, here are the basic statements used. These can be used
'individually to get information, whereas the Functions in the compilable example
'below return several pieces of information with each call.
'DDT
Control Send hDlg, %ID_RichEdit, %EM_LineIndex, n&, 0 To P.cpmax 'position of 1st char in line#
Control Send hDlg, %ID_RichEdit, %EM_LineLength, iCharPos&, 0 to iLineLength& 'length of line starting at that char position
Control Send hDlg, %ID_RichEdit, %EM_EXLineFromChar, 0,-1 To iCurrentLine& 'current line# / 1st line of selection
Control Send hDlg, %ID_RichEdit, %EM_EXSetSel, 0, VarPTR(P) To iResult& 'select specified text (P.cpmin-P.cpmax)
'API
SendMessage(hRichEdit, %EM_LineIndex, iCurrentLine&, 0) 'position of 1st char in line#
SendMessage(hRichEdit, %EM_LineLength, iCharPos&, 0) 'length of line starting at that char position
SendMessage(hRichEdit, %EM_EXLineFromChar, 0,-1) 'current line# / 1st line of selection
SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P)) 'select specified text (P.cpmin-P.cpmax)
'Compilable Example: (Jose Includes)
'This example shows how to select various combination of text - all text, the
'current line, the current line, line M, or lines M-N. Line numbers start at 0.
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "RichEdit.inc"
Global hDlg as DWord, hRichEdit as DWord
%ID_RichEdit = 500
Function PBMain() As Long
Local style&, buf$
buf$ = "This is sample" + $CrLf + "text for the" + $CrLf + "edit control."
style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
Dialog New Pixels, 0, "Test Code",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 30,10,140,20
LoadLibrary("riched32.dll") : InitCommonControls
Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,20,40,160,100, style&, %WS_EX_ClientEdge
Control Handle hDlg, %ID_RichEdit To hRichEdit
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
SelectText_All_DDT : MsgBox "All"
SelectText_CurrentLine_DDT : MsgBox "Current Line"
SelectText_LinesMN_DDT(1,1): MsgBox "Line 1"
SelectText_LinesMN_DDT(0,1): MsgBox "Lines 0,1"
SelectText_CharsMN_DDT(10,20): MsgBox "Chars 10,20"
SelectText_CurrentLine_API : MsgBox "Current Line"
SelectText_LinesMN_API(1,1): MsgBox "Line 1"
SelectText_LinesMN_API(0,1): MsgBox "Lines 0,1"
SelectText_CharsMN_API(10,20): MsgBox "Chars 10,20"
End If
End Function
'All - DDT
Function SelectText_All_DDT() As Long
Local iResult&, P As CharRange
P.cpmin = 0 : P.cpmax = -1
Control Send hDlg, %ID_RichEdit, %EM_EXSetSel, 0, VarPTR(P) To iResult&
Function = iResult&
End Function
'All - API
Function SelectText_All_API() As Long
Local P As CharRange
P.cpmin = 0 : P.cpmax = -1
Function = SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P))
End Function
'Current Line - DDT
Function SelectText_CurrentLine_DDT() as Long
Local iCurrentLine&, iCharPos&, iLineLength&, P as CharRange, iResult&
Control Send hDlg, %ID_RichEdit, %EM_EXLineFromChar, 0,-1 To iCurrentLine& 'current line / 1st line of selection
Control Send hDlg, %ID_RichEdit, %EM_LineIndex, iCurrentLine&, 0 To iCharPos& 'position of 1st char in line iResult&
Control Send hDlg, %ID_RichEdit, %EM_LineLength, iCharPos&, 0 to iLineLength& 'length of line starting at that char position
P.cpmin = iCharPos& : P.cpmax = P.cpmin + iLineLength&
Control Send hDlg, %ID_RichEdit, %EM_EXSetSel, 0, VarPTR(P) TO iResult&
Function = iResult&
End Function
'Current Line - API
Function SelectText_CurrentLine_API() as Long
Local iCurrentLine&, iCharPos&, iLineLength&, P as CharRange
iCurrentLine& = SendMessage(hRichEdit, %EM_EXLineFromChar, 0,-1) 'current line / 1st line of selection
iCharPos& = SendMessage(hRichEdit, %EM_LineIndex, iCurrentLine&, 0) 'position of 1st char in line iResult&
iLineLength& = SendMessage(hRichEdit, %EM_LineLength, iCharPos&, 0) 'length of line starting at that char position
P.cpmin = iCharPos& : P.cpmax = P.cpmin + iLineLength&
Function = SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P))
End Function
'Lines M-N - DDT
Function SelectText_LinesMN_DDT(ByVal m&, ByVal n&) as Long
Local iLineLength&, P as CharRange, iResult&
Control Send hDlg, %ID_RichEdit, %EM_LineIndex, m&, 0 To P.cpmin 'position of 1st char in start line
Control Send hDlg, %ID_RichEdit, %EM_LineIndex, n&, 0 To P.cpmax 'position of 1st char in ending line
Control Send hDlg, %ID_RichEdit, %EM_LineLength, P.cpmax, 0 TO iLineLength& 'length of last line
P.cpmax = P.cpmax + iLineLength&
Control Send hDlg, %ID_RichEdit, %EM_EXSetSel, 0, VarPTR(P) To iResult&
Function = iResult&
End Function
'Lines M-N - API
Function SelectText_LinesMN_API(ByVal m&, ByVal n&) as Long
Local iLineLength&, P as CharRange
P.cpmin = SendMessage(hRichEdit, %EM_LineIndex, m&, 0) 'position of 1st char in start line
P.cpmax = SendMessage(hRichEdit, %EM_LineIndex, n&, 0) 'position of 1st char in ending line
iLineLength& = SendMessage(hRichEdit, %EM_LineLength, P.cpmax, 0) 'length of last line
P.cpmax = P.cpmax + iLineLength&
Function = SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P))
End Function
'Chars M-N - DDT
Function SelectText_CharsMN_DDT(ByVal m&, ByVal n&) As Long
Local P as CharRange, iResult&
P.cpmin = m& : P.cpmax = n&
Control Send hDlg, %ID_RichEdit, %EM_EXSetSel, 0, VarPTR(P) To iResult&
Function = iResult&
End Function
'Chars M-N - API
Function SelectText_CharsMN_API(ByVal m&, ByVal n&) As Long
Local P as CharRange
P.cpmin = m& : P.cpmax = n&
Function = SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P)) 'select P.cpmin thru P.cpmax
End Function
'gbs_00223
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm