Date: 02-16-2022
Return to Index
created by gbSnippets
'Sometimes you need to modify text formatting on the fly, particularly assigning
'a color and using strikeout on selected text. Here's how to do it.
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'BOTH the cf.dwEffects and cf.dwMask must be modified to create strikeout before
'sending the message to the RichEdit control to make the changes. Here's a function
'that changes color and sets strikeout on selected text.
Function setRichTextColor( ByVal NewColor As Long) As Long
Local cf As CHARFORMAT
cf.cbSize = Len(cf) 'Length of structure
cf.dwMask = %CFM_COLOR Or %CFM_Strikeout 'Set mask to colors only
cf.dwEffects = %CFE_StrikeOut
cf.crTextColor = NewColor 'Set the new color value
SendMessage(hRichEdit, %EM_SETCHARFORMAT, %SCF_SELECTION, VarPTR(cf))
End Function
'Compilable Example: (Jose Includes)
#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
SetRichTextColor %RED
End If
End Function
Function setRichTextColor( ByVal NewColor As Long) As Long
' setRichTextColor sets the textcolor for selected text in a Richedit control.
' &HFF - read, &HFF0000 - blue, &H008000 - dark green, &H0 is black, etc.
Local cf As CHARFORMAT
cf.cbSize = Len(cf) 'Length of structure
cf.dwMask = %CFM_COLOR Or %CFM_Strikeout 'Set mask to colors only
cf.dwEffects = %CFE_StrikeOut
cf.crTextColor = NewColor 'Set the new color value
SendMessage(hRichEdit, %EM_SETCHARFORMAT, %SCF_SELECTION, VarPTR(cf))
End Function
'gbs_00352
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm