|
| 1 | +using System.IO; |
| 2 | +using System.Text; |
| 3 | +using System.Windows.Controls; |
| 4 | +using System.Windows.Controls.Primitives; |
| 5 | +using System.Windows.Documents; |
| 6 | +using System.Windows.Media; |
| 7 | +using RszTool.App.ViewModels; |
| 8 | + |
| 9 | +namespace RszTool.App.Controls |
| 10 | +{ |
| 11 | + public class TextBoxWriter : TextWriter |
| 12 | + { |
| 13 | + protected TextBoxBase textBox; |
| 14 | + public delegate void WriteFunc(string value); |
| 15 | + |
| 16 | + public TextBoxWriter(TextBoxBase textBox) |
| 17 | + { |
| 18 | + this.textBox = textBox; |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// 使用UTF-16避免不必要的编码转换 |
| 23 | + /// </summary> |
| 24 | + public override Encoding Encoding |
| 25 | + { |
| 26 | + get { return Encoding.Unicode; } |
| 27 | + } |
| 28 | + |
| 29 | + public virtual void DoWrite(string? value) |
| 30 | + { |
| 31 | + textBox.AppendText(value); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// 最低限度需要重写的方法 |
| 36 | + /// </summary> |
| 37 | + /// <param name="value"></param> |
| 38 | + public override void Write(string? value) |
| 39 | + { |
| 40 | + if (!textBox.CheckAccess()) |
| 41 | + { |
| 42 | + textBox.Dispatcher.BeginInvoke((WriteFunc)Write, value); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + DoWrite(value); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// 其他直接调用WriteLine(value)的情况会调用此函数 |
| 52 | + /// </summary> |
| 53 | + /// <param name="value"></param> |
| 54 | + public override void WriteLine() |
| 55 | + { |
| 56 | + Write(NewLine); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// 为提高效率直接处理一行的输出 |
| 61 | + /// </summary> |
| 62 | + /// <param name="value"></param> |
| 63 | + public override void WriteLine(string? value) |
| 64 | + { |
| 65 | + if (!textBox.CheckAccess()) |
| 66 | + { |
| 67 | + textBox.Dispatcher.BeginInvoke((WriteFunc)WriteLine, value); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + DoWrite(value); |
| 72 | + DoWrite(NewLine); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + public class RichTextBoxWriter : TextBoxWriter |
| 79 | + { |
| 80 | + public RichTextBox RichTextBox => (textBox as RichTextBox)!; |
| 81 | + public Brush? SelectionColor { get; set; } |
| 82 | + public Brush? DarkSelectionColor { get; set; } |
| 83 | + |
| 84 | + public RichTextBoxWriter(RichTextBox textBox) : base(textBox) |
| 85 | + { |
| 86 | + } |
| 87 | + |
| 88 | + public override void DoWrite(string? value) |
| 89 | + { |
| 90 | + if (value == NewLine) |
| 91 | + { |
| 92 | + RichTextBox.AppendText(value); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + TextRange tr = new(RichTextBox.Document.ContentEnd, RichTextBox.Document.ContentEnd) |
| 97 | + { |
| 98 | + Text = value |
| 99 | + }; |
| 100 | + tr.ApplyPropertyValue(TextElement.ForegroundProperty, ThemeManager.Instance.IsDarkTheme ? DarkSelectionColor : SelectionColor); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments