chenn
2025-04-11 e98de937b28d42493de5dea6365c853d6b412d3c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/antdui/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620
 
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
 
namespace AntdUI
{
    /// <summary>
    /// Battery 电量
    /// </summary>
    /// <remarks>展示设备电量。</remarks>
    [Description("Battery 电量")]
    [ToolboxItem(true)]
    [DefaultProperty("Value")]
    public class Battery : IControl
    {
        public Battery()
        {
            base.BackColor = Color.Transparent;
        }
 
        #region 属性
 
        Color? back;
        /// <summary>
        /// 背景颜色
        /// </summary>
        [Description("背景颜色"), Category("外观"), DefaultValue(null)]
        [Editor(typeof(Design.ColorEditor), typeof(UITypeEditor))]
        public new Color? BackColor
        {
            get => back;
            set
            {
                if (back == value) return;
                back = value;
                Invalidate();
            }
        }
 
        Color? fore;
        /// <summary>
        /// 文字颜色
        /// </summary>
        [Description("文字颜色"), Category("外观"), DefaultValue(null)]
        [Editor(typeof(Design.ColorEditor), typeof(UITypeEditor))]
        public new Color? ForeColor
        {
            get => fore;
            set
            {
                if (fore == value) fore = value;
                fore = value;
                Invalidate();
            }
        }
 
        int radius = 4;
        /// <summary>
        /// 圆角
        /// </summary>
        [Description("圆角"), Category("外观"), DefaultValue(4)]
        public int Radius
        {
            get => radius;
            set
            {
                if (radius == value) return;
                radius = value;
                Invalidate();
            }
        }
 
        int dotsize = 8;
        /// <summary>
        /// 点大小
        /// </summary>
        [Description("点大小"), Category("外观"), DefaultValue(8)]
        public int DotSize
        {
            get => dotsize;
            set
            {
                if (dotsize == value) return;
                dotsize = value;
                Invalidate();
            }
        }
 
        #region 进度条
 
        int _value = 0;
        /// <summary>
        /// 进度条
        /// </summary>
        [Description("进度条"), Category("数据"), DefaultValue(0)]
        public int Value
        {
            get => _value;
            set
            {
                if (_value == value) return;
                if (value < 0) value = 0;
                else if (value > 100) value = 100;
                _value = value;
                Invalidate();
            }
        }
 
        [Description("显示"), Category("外观"), DefaultValue(true)]
        public bool ShowText { get; set; } = true;
 
        #endregion
 
        Color fillfully = Color.FromArgb(0, 210, 121);
        [Description("满电颜色"), Category("外观"), DefaultValue(typeof(Color), "0, 210, 121")]
        public Color FillFully
        {
            get => fillfully;
            set
            {
                if (fillfully == value) return;
                fillfully = value;
                Invalidate();
            }
        }
 
        [Description("警告电量颜色"), Category("外观"), DefaultValue(typeof(Color), "250, 173, 20")]
        public Color FillWarn { get; set; } = Color.FromArgb(250, 173, 20);
 
        [Description("危险电量颜色"), Category("外观"), DefaultValue(typeof(Color), "255, 77, 79")]
        public Color FillDanger { get; set; } = Color.FromArgb(255, 77, 79);
 
        #endregion
 
        #region 渲染
 
        protected override void OnPaint(PaintEventArgs e)
        {
            var _rect = ClientRectangle;
            var g = e.Graphics.High();
            var size = g.MeasureString("100%", Font);
            var rect = new RectangleF((_rect.Width - size.Width) / 2F, (_rect.Height - size.Height) / 2F, size.Width, size.Height);
            float _radius = radius * Config.Dpi;
            using (var path_pain = rect.RoundPath(_radius))
            {
                if (_value >= 100)
                {
                    using (var brush = new SolidBrush(fillfully))
                    {
                        g.FillPath(brush, path_pain);
                        if (dotsize > 0)
                        {
                            float _dotsize = dotsize * Config.Dpi;
                            using (var path = new RectangleF(rect.Right, rect.Top + (rect.Height - _dotsize) / 2F, _dotsize / 2F, _dotsize).RoundPath(_radius / 2, false, true, true, false))
                            {
                                g.FillPath(brush, path);
                            }
                        }
                    }
                    if (ShowText)
                    {
                        using (var brush = new SolidBrush(fore ?? Style.Db.Text))
                        {
                            g.DrawStr("100%", Font, brush, rect, c);
                        }
                    }
                }
                else
                {
                    using (var brush = new SolidBrush(back ?? Style.Db.FillSecondary))
                    {
                        g.FillPath(brush, path_pain);
                        if (dotsize > 0)
                        {
                            float _dotsize = dotsize * Config.Dpi;
                            using (var path = new RectangleF(rect.Right, rect.Top + (rect.Height - _dotsize) / 2F, _dotsize / 2F, _dotsize).RoundPath(_radius / 2, false, true, true, false))
                            {
                                g.FillPath(brush, path);
                            }
                        }
                    }
                    if (_value > 0)
                    {
                        using (var bmp = new Bitmap(_rect.Width, _rect.Height))
                        {
                            using (var g2 = Graphics.FromImage(bmp).High())
                            {
                                Color _color;
                                if (_value > 30) _color = fillfully;
                                else if (_value > 20) _color = FillWarn;
                                else _color = FillDanger;
                                using (var brush = new SolidBrush(_color))
                                {
                                    g2.FillPath(brush, path_pain);
                                }
                                var _w = rect.Width * (_value / 100F);
                                g2.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                                g2.FillRectangle(Brushes.Transparent, new RectangleF(rect.X + _w, 0, rect.Width, bmp.Height));
                            }
                            g.DrawImage(bmp, _rect);
                        }
                    }
                    if (ShowText)
                    {
                        using (var brush = new SolidBrush(fore ?? Style.Db.Text))
                        {
                            g.DrawStr(_value + "%", Font, brush, rect, c);
                        }
                    }
                }
            }
            base.OnPaint(e);
        }
 
        StringFormat c = Helper.SF();
 
        #endregion
    }
}