-aggiunto colorazione testo richtext

This commit is contained in:
Demetrio Cassarino
2025-02-24 09:54:22 +01:00
parent f70eba5633
commit dff00ca1e7
2 changed files with 180 additions and 10 deletions
+179 -9
View File
@@ -1,6 +1,7 @@
Imports EgtUILib
Imports EgtWPFLib5
Imports System.IO
Imports System.Runtime.CompilerServices
Imports System.Text
Public Class ScriptWindowVM
@@ -374,22 +375,191 @@ Public Class ScriptWindowVM
m_bJustifyIsChecked = textRange.GetPropertyValue(FlowDocument.TextAlignmentProperty).Equals(TextAlignment.Justify)
End Sub
Public Sub InsertText(ByVal rtb As RichTextBox, ByVal content As String)
Public Sub InsertText(ByVal sRichTextBox As RichTextBox, ByVal content As String)
If Not String.IsNullOrEmpty(content) Then
rtb?.BeginChange()
sRichTextBox?.BeginChange()
If Not String.IsNullOrEmpty(rtb.Selection.Text) Then
rtb.Selection.Text = String.Empty
If Not String.IsNullOrEmpty(sRichTextBox.Selection.Text) Then
sRichTextBox.Selection.Text = String.Empty
End If
Dim tp = rtb.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward)
rtb.CaretPosition.InsertTextInRun(content)
rtb.CaretPosition = tp
rtb.EndChange()
Keyboard.Focus(rtb)
Dim textPointer As TextPointer = sRichTextBox.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward)
sRichTextBox.CaretPosition.InsertTextInRun(content)
sRichTextBox.CaretPosition = textPointer
sRichTextBox.EndChange()
Keyboard.Focus(sRichTextBox)
End If
End Sub
Public Sub TextSearchAndColor(rtb As RichTextBox, find As String, Color As SolidColorBrush, fontWeight As FontWeight, fontStyle As FontStyle, fontSize As Integer)
Dim searchRange As New TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd)
While (FindTextInRange(searchRange, find) IsNot Nothing)
Dim found As TextRange = FindTextInRange(searchRange, find)
found.ApplyPropertyValue(TextElement.ForegroundProperty, Color)
' Seleziono la font weight
SelectFontWeight(fontWeight, found)
' Seleziono lo stile
SelectFontStyle(fontStyle, found)
' Seleziono la dimensione del testo
SelectFontSize(fontSize, found)
searchRange = New TextRange(found.End, rtb.Document.ContentEnd)
End While
End Sub
Public Function FindTextInRange(searchRange As TextRange, searchText As String) As TextRange
Dim result As TextRange = Nothing
Dim offset As Integer = searchRange.Text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase)
If offset >= 0 Then
Dim start As TextPointer = GetTextPositionAtOffset(searchRange.Start, offset)
result = New TextRange(start, GetTextPositionAtOffset(start, searchText.Length))
End If
Return result
End Function
Public Function GetTextPositionAtOffset(ByVal position As TextPointer, ByVal offset As Integer) As TextPointer
Dim current As TextPointer = position
While current IsNot Nothing
position = current
Dim adjacent = position.GetAdjacentElement(LogicalDirection.Forward)
Dim context = position.GetPointerContext(LogicalDirection.Forward)
Select Case context
Case TextPointerContext.Text
Dim count As Integer = position.GetTextRunLength(LogicalDirection.Forward)
If offset <= count Then
Return position.GetPositionAtOffset(offset)
End If
offset -= count
Case TextPointerContext.ElementStart
If TypeOf adjacent Is InlineUIContainer Then offset -= 1
Case TextPointerContext.ElementEnd
If TypeOf adjacent Is Paragraph Then offset -= 2
End Select
current = position.GetNextContextPosition(LogicalDirection.Forward)
End While
Return position
End Function
Private Sub SelectFontWeight(fontWeight As FontWeight, found As TextRange)
Select Case fontWeight
Case FontWeights.Thin
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Thin)
Case FontWeights.Medium
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Medium)
Case FontWeights.Regular
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular)
Case FontWeights.Normal
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal)
Case FontWeights.Heavy
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Heavy)
Case FontWeights.Light
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Light)
Case FontWeights.ExtraLight
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.ExtraLight)
Case FontWeights.UltraLight
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.UltraLight)
Case FontWeights.Black
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Black)
Case FontWeights.ExtraBlack
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.ExtraBlack)
Case FontWeights.UltraBlack
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.UltraBlack)
Case FontWeights.Bold
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold)
Case FontWeights.DemiBold
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.DemiBold)
Case FontWeights.SemiBold
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.SemiBold)
Case FontWeights.ExtraBold
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.ExtraBold)
Case FontWeights.UltraBold
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.UltraBold)
Case Else
found.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal)
End Select
End Sub
Private Sub SelectFontStyle(fontStyle As FontStyle, found As TextRange)
Select Case fontStyle
Case FontStyles.Normal
found.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal)
Case FontStyles.Italic
found.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic)
Case FontStyles.Oblique
found.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Oblique)
Case Else
found.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal)
End Select
End Sub
Private Sub SelectFontSize(fontSize As Integer, found As TextRange)
Select Case fontSize
Case FontSizeList(0) ' 8
found.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(0)) * (96 / 72)) ' 8
Case FontSizeList(1) ' 10
found.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(1)) * (96 / 72)) ' 10
Case FontSizeList(2) ' 12
found.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(2)) * (96 / 72)) ' 12
Case FontSizeList(3) ' 14
found.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(3)) * (96 / 72)) ' 14
Case FontSizeList(4) ' 16
found.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(4)) * (96 / 72)) ' 16
Case FontSizeList(5) ' 18
found.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(5)) * (96 / 72)) ' 18
Case FontSizeList(6) ' 20
found.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(6)) * (96 / 72)) ' 20
Case Else
found.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(2)) * (96 / 72)) ' 12
End Select
End Sub
Public Sub ResetStyleRichText()
Map.refScriptWindowV.sNameFile_RichTxBx.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black)
Map.refScriptWindowV.sNameFile_RichTxBx.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal)
Map.refScriptWindowV.sNameFile_RichTxBx.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal)
Map.refScriptWindowV.sNameFile_RichTxBx.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, Convert.ToDouble(FontSizeList(2)) * (96 / 72))
End Sub
Public Sub ChangeTextColor()
Dim sNameRichTxBx As New TextRange(Map.refScriptWindowV.sNameFile_RichTxBx.Document.ContentStart, Map.refScriptWindowV.sNameFile_RichTxBx.Document.ContentEnd)
Dim sNameRichTxBxArray As String() = sNameRichTxBx.Text.Split(vbCrLf)
Dim sNameRichTxBxList As New List(Of String)
For Each NameRichTxBx As String In sNameRichTxBxArray
Dim NameRichTxBxTrim As String = NameRichTxBx.Trim(vbLf)
sNameRichTxBxList.Add(NameRichTxBxTrim)
Next
If Not IsNothing(Map.refScriptWindowVM) Then
'If sNameRichTxBx.Text.Contains("local") Or sNameRichTxBx.Text.Contains("require") Then
' Map.refScriptWindowVM.TextSearchAndColor(sNameFile_RichTxBx, "local", Brushes.Blue, FontWeights.Bold, FontStyles.Italic, 16)
' Map.refScriptWindowVM.TextSearchAndColor(sNameFile_RichTxBx, "require", Brushes.Green, FontWeights.Normal, FontStyles.Normal, 12)
'End If
'If sNameRichTxBxList.Contains("--") AndAlso sNameRichTxBx.Text.StartsWith("--") Then
' Map.refScriptWindowVM.TextSearchAndColor(sNameFile_RichTxBx, sNameRichTxBx.Text, Brushes.Yellow, FontWeights.Normal, FontStyles.Normal, 12)
'End If
For Each sNameRichTxBxItem As String In sNameRichTxBxList
Dim stmpNameRichTxBxItem As String() = sNameRichTxBxItem.Split(" ")
For Each tmpNameRichTxBxItem As String In stmpNameRichTxBxItem
If tmpNameRichTxBxItem.Contains("--") Then
TextSearchAndColor(Map.refScriptWindowV.sNameFile_RichTxBx, tmpNameRichTxBxItem, Brushes.Yellow, FontWeights.Normal, FontStyles.Normal, 10)
ElseIf tmpNameRichTxBxItem.Equals("local") Then
TextSearchAndColor(Map.refScriptWindowV.sNameFile_RichTxBx, tmpNameRichTxBxItem, Brushes.Blue, FontWeights.Bold, FontStyles.Italic, 16)
ElseIf tmpNameRichTxBxItem.Equals("require") Or tmpNameRichTxBxItem.Equals("require(") Or tmpNameRichTxBxItem.Equals("require (") Or tmpNameRichTxBxItem.Equals("require('") Then
TextSearchAndColor(Map.refScriptWindowV.sNameFile_RichTxBx, tmpNameRichTxBxItem, Brushes.Green, FontWeights.Normal, FontStyles.Normal, 12)
Else
If Not IsNothing(Map.refScriptWindowVM) Then Map.refScriptWindowVM.ResetStyleRichText()
End If
Next
Next
End If
End Sub
#End Region ' Methods
#Region "COMMANDS"