-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextWriter.cs
48 lines (43 loc) · 1.22 KB
/
TextWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Target_Recognition_Console
{
class TextBoxWriter : TextWriter
{
TextBox textBox;
delegate void WriteFunc(string value);
WriteFunc write;
WriteFunc writeLine;
public TextBoxWriter(TextBox textBox)
{
this.textBox = textBox;
write = Write;
writeLine = WriteLine;
}
public override Encoding Encoding
{
// 使用UTF-16避免不必要的编码转换
get { return Encoding.Unicode; }
}
public override void Write(string value)
{
// 最低限度需要重写的方法
if (textBox.InvokeRequired)
textBox.BeginInvoke(write, value);
else
textBox.AppendText(value);
}
public override void WriteLine(string value)
{
// 为提高效率直接处理一行的输出
if (textBox.InvokeRequired)
textBox.BeginInvoke(writeLine, value);
else
{
textBox.AppendText(value);
textBox.AppendText(this.NewLine);
}
}
}
}