修改数据结构增加 TextLine
Browse files- Editor.cs +32 -24
- MainWindow.xaml.cs +3 -3
- RichChar.cs +1 -2
- TextLine.cs +10 -0
Editor.cs
CHANGED
|
@@ -11,7 +11,7 @@ namespace WpfRichTextBox
|
|
| 11 |
{
|
| 12 |
public int _fontsize = 16;
|
| 13 |
|
| 14 |
-
public List<
|
| 15 |
|
| 16 |
public Editor()
|
| 17 |
{
|
|
@@ -22,50 +22,58 @@ namespace WpfRichTextBox
|
|
| 22 |
|
| 23 |
private void OnPasteHandler(object sender, DataObjectPastingEventArgs e)
|
| 24 |
{
|
| 25 |
-
|
| 26 |
InvalidateVisual();
|
| 27 |
}
|
| 28 |
|
| 29 |
private void Editor_TextChanged(object sender, TextChangedEventArgs e)
|
| 30 |
{
|
| 31 |
-
|
| 32 |
InvalidateVisual();
|
| 33 |
}
|
| 34 |
|
| 35 |
-
private void
|
| 36 |
{
|
| 37 |
-
|
| 38 |
var text = new TextRange(Document.ContentStart, Document.ContentEnd).Text;
|
| 39 |
-
|
| 40 |
-
int
|
| 41 |
-
for (int i = 0; i < count; i++)
|
| 42 |
{
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
{
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
protected override void OnRender(DrawingContext drawingContext)
|
| 53 |
{
|
| 54 |
base.OnRender(drawingContext);
|
| 55 |
-
foreach (var
|
| 56 |
{
|
| 57 |
-
|
| 58 |
{
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
}
|
| 70 |
}
|
| 71 |
}
|
|
|
|
| 11 |
{
|
| 12 |
public int _fontsize = 16;
|
| 13 |
|
| 14 |
+
public List<TextLine> TextLines { get; private set; } = new List<TextLine>();
|
| 15 |
|
| 16 |
public Editor()
|
| 17 |
{
|
|
|
|
| 22 |
|
| 23 |
private void OnPasteHandler(object sender, DataObjectPastingEventArgs e)
|
| 24 |
{
|
| 25 |
+
SyncTextLines();
|
| 26 |
InvalidateVisual();
|
| 27 |
}
|
| 28 |
|
| 29 |
private void Editor_TextChanged(object sender, TextChangedEventArgs e)
|
| 30 |
{
|
| 31 |
+
SyncTextLines();
|
| 32 |
InvalidateVisual();
|
| 33 |
}
|
| 34 |
|
| 35 |
+
private void SyncTextLines()
|
| 36 |
{
|
| 37 |
+
TextLines.Clear();
|
| 38 |
var text = new TextRange(Document.ContentStart, Document.ContentEnd).Text;
|
| 39 |
+
var lines = text.Split('\n');
|
| 40 |
+
for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
|
|
|
|
| 41 |
{
|
| 42 |
+
var line = lines[lineIndex];
|
| 43 |
+
var textLine = new TextLine();
|
| 44 |
+
for (int i = 0; i < line.Length; i++)
|
| 45 |
{
|
| 46 |
+
textLine.RichChars.Add(new RichChar
|
| 47 |
+
{
|
| 48 |
+
Character = line[i],
|
| 49 |
+
Position = new Point(i * _fontsize, lineIndex * _fontsize),
|
| 50 |
+
Color = Colors.Black
|
| 51 |
+
});
|
| 52 |
+
}
|
| 53 |
+
TextLines.Add(textLine);
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
protected override void OnRender(DrawingContext drawingContext)
|
| 58 |
{
|
| 59 |
base.OnRender(drawingContext);
|
| 60 |
+
foreach (var textLine in TextLines)
|
| 61 |
{
|
| 62 |
+
foreach (var richChar in textLine.RichChars)
|
| 63 |
{
|
| 64 |
+
if (richChar.Position.X >= 0 && richChar.Position.X + _fontsize <= ActualWidth && richChar.Position.Y >= 0 && richChar.Position.Y + _fontsize <= ActualHeight)
|
| 65 |
+
{
|
| 66 |
+
var formattedText = new FormattedText(
|
| 67 |
+
richChar.Character.ToString(),
|
| 68 |
+
System.Globalization.CultureInfo.CurrentCulture,
|
| 69 |
+
FlowDirection.LeftToRight,
|
| 70 |
+
new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch),
|
| 71 |
+
_fontsize,
|
| 72 |
+
new SolidColorBrush(richChar.Color),
|
| 73 |
+
VisualTreeHelper.GetDpi(this).PixelsPerDip);
|
| 74 |
|
| 75 |
+
drawingContext.DrawText(formattedText, richChar.Position);
|
| 76 |
+
}
|
| 77 |
}
|
| 78 |
}
|
| 79 |
}
|
MainWindow.xaml.cs
CHANGED
|
@@ -9,10 +9,10 @@ namespace WpfRichTextBox
|
|
| 9 |
InitializeComponent();
|
| 10 |
|
| 11 |
new System.Windows.Documents.TextRange(customRichTextBox.Document.ContentStart, customRichTextBox.Document.ContentEnd).Text = "Hello World!";
|
| 12 |
-
if (customRichTextBox.RichChars.Count > 0)
|
| 13 |
{
|
| 14 |
-
customRichTextBox.RichChars[0].Color = System.Windows.Media.Colors.Red;
|
| 15 |
-
customRichTextBox.RichChars[0].Position = new System.Windows.Point(50, 50);
|
| 16 |
customRichTextBox.InvalidateVisual();
|
| 17 |
}
|
| 18 |
}
|
|
|
|
| 9 |
InitializeComponent();
|
| 10 |
|
| 11 |
new System.Windows.Documents.TextRange(customRichTextBox.Document.ContentStart, customRichTextBox.Document.ContentEnd).Text = "Hello World!";
|
| 12 |
+
if (customRichTextBox.TextLines.Count > 0 && customRichTextBox.TextLines[0].RichChars.Count > 0)
|
| 13 |
{
|
| 14 |
+
customRichTextBox.TextLines[0].RichChars[0].Color = System.Windows.Media.Colors.Red;
|
| 15 |
+
customRichTextBox.TextLines[0].RichChars[0].Position = new System.Windows.Point(50, 50);
|
| 16 |
customRichTextBox.InvalidateVisual();
|
| 17 |
}
|
| 18 |
}
|
RichChar.cs
CHANGED
|
@@ -7,8 +7,7 @@ namespace WpfRichTextBox
|
|
| 7 |
public class RichChar
|
| 8 |
{
|
| 9 |
public char Character { get; set; }
|
| 10 |
-
public Point Position { get; set; }
|
| 11 |
public Color Color { get; set; }
|
| 12 |
-
// 可扩展其他属性,如字体、大小等
|
| 13 |
}
|
| 14 |
}
|
|
|
|
| 7 |
public class RichChar
|
| 8 |
{
|
| 9 |
public char Character { get; set; }
|
| 10 |
+
public Point Position { get; set; }
|
| 11 |
public Color Color { get; set; }
|
|
|
|
| 12 |
}
|
| 13 |
}
|
TextLine.cs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System.Collections.Generic;
|
| 2 |
+
|
| 3 |
+
namespace WpfRichTextBox
|
| 4 |
+
{
|
| 5 |
+
public class TextLine
|
| 6 |
+
{
|
| 7 |
+
public List<RichChar> RichChars { get; set; } = new List<RichChar>();
|
| 8 |
+
// 可扩展:如行号、行属性等
|
| 9 |
+
}
|
| 10 |
+
}
|