tangxu
2025-01-13 4f7cb65b079d88d5a829688b24d26d5145c5df47
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
namespace WccPackTool.Controls;
 
public class ConsoleWriteLine : Control
{
    private MessageCollection items;
 
 
    #region 公有属性
 
    [Category("WenAuto"), Description("是否显示序号"), DefaultValue(true)]
    public bool ShowLine { get; set; } = true;
    #endregion
 
 
    public ConsoleWriteLine()
    {
        base.SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
        base.UpdateStyles();
 
        this.Controls.Add(vScrollBar);
        vScrollBar.ValueChanged += VScrollBar_ValueChanged;
        this.MouseWheel += ConsoleWriteLine_MouseWheel;
        this.MouseEnter += ConsoleWriteLine_MouseEnter;
    }
 
    private void ConsoleWriteLine_MouseEnter(object sender, EventArgs e)
    {
        this.Select();
    }
    private void ConsoleWriteLine_MouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta < 0)
        {
            var v = vScrollBar.Value + 1;
            if (vScrollBar.Maximum >= v)
                vScrollBar.Value++;
        }
        else
        {
            var v = vScrollBar.Value - 1;
            if (vScrollBar.Minimum <= v)
                vScrollBar.Value--;
        }
    }
 
 
    private void VScrollBar_ValueChanged(object sender, EventArgs e)
    {
        this.Invalidate(true);
    }
 
    private void SetVS()
    {
        var count = (int)Math.Floor((float)this.Height / 20);
        if (this.Items.Count >= count)
        {
            this.vScrollBar.Maximum = this.Items.Count - count;
            this.vScrollBar.Value = this.Items.Count - count;
        }
        else
        {
            this.vScrollBar.Maximum = 0;
            this.vScrollBar.Value = 0;
        }
    }
 
    private VScrollBar vScrollBar = new VScrollBar() { Dock = DockStyle.Right, Maximum = 0, Value = 0, LargeChange = 1 };
 
    public MessageCollection Items => items ??= new MessageCollection(this);
 
 
    public static StringFormat StringConters { get; set; }
    = new StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap)
    {
        LineAlignment = StringAlignment.Center,
        Trimming = StringTrimming.EllipsisCharacter
    };
 
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g = e.Graphics;
        g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高,即消除锯齿
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.CompositingQuality = CompositingQuality.HighQuality;
 
 
        g.DrawRectangle(Pens.Black, new Rectangle(0, 0, this.Width - vScrollBar.Width - 1, this.Height - 1));
 
 
        var count = (int)Math.Ceiling((float)this.Height / 20);
 
        var cuowei = count * 20 - this.Height;
 
 
        if (this.vScrollBar.Value == 0)
        {
            cuowei = 0;
        }
 
        if (count > this.Items.Count)
        {
            count = this.Items.Count;
        }
 
 
        for (int i = 0; i < count; i++)
        {
            var line = (this.vScrollBar.Value > 0 ? vScrollBar.Value - 1 : 0) + i;
            var item = this.Items[line];
 
            var rec = new RectangleF(3, i * 20 - cuowei, this.Width - vScrollBar.Width - 6, 20);
 
 
            var text = $"{(ShowLine ? line + 1 + ". " : "")}{item.DateTime:HH:mm:ss} {item.Text}";
            g.DrawString(text, Font, new SolidBrush(item.Color), rec, StringConters);
        }
    }
 
 
    public class MessageCollection : List<MessageItem>
    {
        private ConsoleWriteLine owner;
 
        public MessageCollection(ConsoleWriteLine owner)
        {
            this.owner = owner;
        }
 
        public void Add(string text) => this.Add(text, Color.Black);
        public void Add(string text, Color color)
        {
            this.Add(new MessageItem(text, color));
            owner.Invalidate(true);
            owner.SetVS();
        }
        public new void Clear()
        {
            base.Clear();
            owner.Invalidate(true);
            owner.SetVS();
        }
    }
 
    public class MessageItem
    {
        public MessageItem(string text, Color color)
        {
            Text = text;
            Color = color;
        }
        public DateTime DateTime { get; set; } = DateTime.Now;
        public string Text { get; set; } = "";
        public Color Color { get; set; } = Color.Black;
    }
}