UCBottle.cs
14.0 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-05
//
// ***********************************************************************
// <copyright file="UCBottle.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace HZH_Controls.Controls
{
/// <summary>
/// Class UCBottle.
/// Implements the <see cref="System.Windows.Forms.UserControl" />
/// </summary>
/// <seealso cref="System.Windows.Forms.UserControl" />
public class UCBottle : UserControl
{
//瓶身区域
/// <summary>
/// The m working rect
/// </summary>
Rectangle m_workingRect;
/// <summary>
/// The title
/// </summary>
string title = "瓶子1";
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
[Description("标题"), Category("自定义")]
public string Title
{
get { return title; }
set
{
title = value;
ResetWorkingRect();
Refresh();
}
}
/// <summary>
/// The bottle color
/// </summary>
private Color bottleColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the bottle.
/// </summary>
/// <value>The color of the bottle.</value>
[Description("瓶子颜色"), Category("自定义")]
public Color BottleColor
{
get { return bottleColor; }
set
{
bottleColor = value;
Refresh();
}
}
/// <summary>
/// The bottle mouth color
/// </summary>
private Color bottleMouthColor = Color.FromArgb(105, 105, 105);
/// <summary>
/// Gets or sets the color of the bottle mouth.
/// </summary>
/// <value>The color of the bottle mouth.</value>
[Description("瓶口颜色"), Category("自定义")]
public Color BottleMouthColor
{
get { return bottleMouthColor; }
set { bottleMouthColor = value; }
}
/// <summary>
/// The liquid color
/// </summary>
private Color liquidColor = Color.FromArgb(3, 169, 243);
/// <summary>
/// Gets or sets the color of the liquid.
/// </summary>
/// <value>The color of the liquid.</value>
[Description("液体颜色"), Category("自定义")]
public Color LiquidColor
{
get { return liquidColor; }
set
{
liquidColor = value;
Refresh();
}
}
/// <summary>
/// 获取或设置控件显示的文字的字体。
/// </summary>
/// <value>The font.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
[Description("文字字体"), Category("自定义")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
ResetWorkingRect();
Refresh();
}
}
/// <summary>
/// 获取或设置控件的前景色。
/// </summary>
/// <value>The color of the fore.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
[Description("文字颜色"), Category("自定义")]
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
Refresh();
}
}
/// <summary>
/// The maximum value
/// </summary>
private decimal maxValue = 100;
/// <summary>
/// Gets or sets the maximum value.
/// </summary>
/// <value>The maximum value.</value>
[Description("最大值"), Category("自定义")]
public decimal MaxValue
{
get { return maxValue; }
set
{
if (value < m_value)
return;
maxValue = value;
Refresh();
}
}
/// <summary>
/// The m value
/// </summary>
private decimal m_value = 50;
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
[Description("值"), Category("自定义")]
public decimal Value
{
get { return m_value; }
set
{
if (value <= 0 || value > maxValue)
return;
m_value = value;
Refresh();
}
}
/// <summary>
/// The m no
/// </summary>
private string m_NO;
/// <summary>
/// Gets or sets the NO.
/// </summary>
/// <value>The no.</value>
[Description("编号"), Category("自定义")]
public string NO
{
get { return m_NO; }
set
{
m_NO = value;
Refresh();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="UCBottle" /> class.
/// </summary>
public UCBottle()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.SizeChanged += UCBottle_SizeChanged;
this.Size = new Size(100, 150);
}
/// <summary>
/// Handles the SizeChanged event of the UCBottle control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void UCBottle_SizeChanged(object sender, EventArgs e)
{
ResetWorkingRect();
}
/// <summary>
/// Resets the working rect.
/// </summary>
private void ResetWorkingRect()
{
var g = this.CreateGraphics();
var size = g.MeasureString(title, Font);
m_workingRect = new Rectangle(0, (int)size.Height + 10, this.Width, this.Height - ((int)size.Height + 10) - 15);
}
/// <summary>
/// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
/// </summary>
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
//写文字
var size = g.MeasureString(title, Font);
g.DrawString(title, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, 2));
//画空瓶子
GraphicsPath pathPS = new GraphicsPath();
Point[] psPS = new Point[]
{
new Point(m_workingRect.Left, m_workingRect.Top),
new Point(m_workingRect.Right - 1, m_workingRect.Top),
new Point(m_workingRect.Right - 1, m_workingRect.Bottom - 15),
new Point(m_workingRect.Right - 1 - m_workingRect.Width / 4, m_workingRect.Bottom),
new Point(m_workingRect.Left + m_workingRect.Width / 4, m_workingRect.Bottom),
new Point(m_workingRect.Left, m_workingRect.Bottom - 15),
};
pathPS.AddLines(psPS);
pathPS.CloseAllFigures();
g.FillPath(new SolidBrush(bottleColor), pathPS);
//画液体
decimal decYTHeight = (m_value / maxValue) * m_workingRect.Height;
GraphicsPath pathYT = new GraphicsPath();
Rectangle rectYT = Rectangle.Empty;
if (decYTHeight < 15)
{
PointF[] psYT = new PointF[]
{
new PointF((float)(m_workingRect.Left+(15-decYTHeight))+3,(float)(m_workingRect.Bottom-decYTHeight)),
new PointF((float)(m_workingRect.Right-(15-decYTHeight))-3,(float)(m_workingRect.Bottom-decYTHeight)),
new PointF(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
new PointF(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom),
};
pathYT.AddLines(psYT);
pathYT.CloseAllFigures();
rectYT = new Rectangle((m_workingRect.Left + (15 - (int)decYTHeight)) + 3, m_workingRect.Bottom - (int)decYTHeight - 5, m_workingRect.Width - (int)(15 - decYTHeight) * 2 - 8, 10);
}
else
{
PointF[] psYT = new PointF[]
{
new PointF(m_workingRect.Left,(float)(m_workingRect.Bottom-decYTHeight)),
new PointF(m_workingRect.Right-1,(float)(m_workingRect.Bottom-decYTHeight)),
new PointF(m_workingRect.Right-1,m_workingRect.Bottom-15),
new PointF(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
new PointF(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom),
new PointF(m_workingRect.Left,m_workingRect.Bottom-15),
};
pathYT.AddLines(psYT);
pathYT.CloseAllFigures();
rectYT = new Rectangle(m_workingRect.Left, m_workingRect.Bottom - (int)decYTHeight - 5, m_workingRect.Width, 10);
}
g.FillPath(new SolidBrush(liquidColor), pathYT);
g.FillPath(new SolidBrush(Color.FromArgb(50, bottleMouthColor)), pathYT);
//画液体面
g.FillEllipse(new SolidBrush(liquidColor), rectYT);
g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), rectYT);
//画高亮
int intCount = m_workingRect.Width / 2 / 4;
int intSplit = (255 - 100) / intCount;
for (int i = 0; i < intCount; i++)
{
int _penWidth = m_workingRect.Width / 2 - 4 * i;
if (_penWidth <= 0)
_penWidth = 1;
g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(10, Color.White)), _penWidth), new Point(m_workingRect.Width / 2, m_workingRect.Top), new Point(m_workingRect.Width / 2, m_workingRect.Bottom - 15));
if (_penWidth == 1)
break;
}
//画瓶底
g.FillEllipse(new SolidBrush(bottleColor), new RectangleF(m_workingRect.Left, m_workingRect.Top - 5, m_workingRect.Width - 2, 10));
g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), new RectangleF(m_workingRect.Left, m_workingRect.Top - 5, m_workingRect.Width - 2, 10));
//画瓶口
g.FillRectangle(new SolidBrush(bottleMouthColor), new Rectangle(m_workingRect.Left + m_workingRect.Width / 4, m_workingRect.Bottom, m_workingRect.Width / 2, 15));
//画瓶颈阴影
GraphicsPath pathPJ = new GraphicsPath();
Point[] psPJ = new Point[]
{
new Point(m_workingRect.Left, m_workingRect.Bottom-15),
new Point(m_workingRect.Right-1, m_workingRect.Bottom-15),
new Point(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Bottom),
new Point(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Bottom)
};
pathPJ.AddLines(psPJ);
pathPJ.CloseAllFigures();
g.FillPath(new SolidBrush(Color.FromArgb(50, bottleMouthColor)), pathPJ);
//写编号
if (!string.IsNullOrEmpty(m_NO))
{
var nosize = g.MeasureString(m_NO, Font);
g.DrawString(m_NO, Font, new SolidBrush(ForeColor), new PointF((this.Width - nosize.Width) / 2, m_workingRect.Top + 10));
}
}
}
}