FlatLabel.cs
6.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Asa.Theme
{
/// <summary>
/// 标签控件
/// </summary>
public partial class FlatLabel : FlatBase
{
private bool _backBlue = false;
private int _borderWidth = 0;
private Rectangle _borderRect = new Rectangle();
private RectangleF _textRect = new RectangleF();
private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;
/// <summary>
/// 标签控件
/// </summary>
public FlatLabel()
{
InitializeComponent();
base.Font = new Font("宋体", 9f);
CalcSize();
Refresh();
}
/// <summary>
/// 显示的文本
/// </summary>
[Browsable(true), Category(""), Description("显示的文本"), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get => base.Text;
set
{
base.Text = value;
CalcSize();
Refresh();
}
}
/// <summary>
/// 显示的文本的对齐方式
/// </summary>
[Browsable(true), Category(""), Description("显示的文本的对齐方式"), DefaultValue(ContentAlignment.MiddleCenter)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public ContentAlignment TextAlign
{
set
{
_textAlign = value;
CalcSize();
Refresh();
}
get
{
return _textAlign;
}
}
/// <summary>
/// 项目字体
/// </summary>
[Browsable(true), Category(""), Description("项目字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
get => base.Font;
set
{
base.Font = value;
CalcSize();
Refresh();
}
}
/// <summary>
/// 蓝色的背景
/// </summary>
[Browsable(true), Category(""), Description("蓝色的背景"), DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool BackBlue
{
get => _backBlue;
set
{
_backBlue = value;
CalcSize();
Refresh();
}
}
/// <summary>
/// 边框宽度
/// </summary>
[Browsable(true), Category(""), Description("边框宽度"), DefaultValue(0)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int BorderWidth
{
get => _borderWidth;
set
{
_borderWidth = value;
if (_borderWidth < 0) _borderWidth = 0;
CalcSize();
Refresh();
}
}
/// <summary>
/// 计算大小
/// </summary>
protected override void CalcSize()
{
_borderRect = new Rectangle(_borderWidth / 2, _borderWidth / 2, Width - _borderWidth, Height - _borderWidth);
SizeF sf = CreateGraphics().MeasureString(Text, Font);
switch (TextAlign)
{
case ContentAlignment.TopLeft:
_textRect = new RectangleF(_borderWidth, _borderWidth, sf.Width, sf.Height);
break;
case ContentAlignment.TopCenter:
_textRect = new RectangleF((Width - sf.Width) / 2f, _borderWidth, sf.Width, sf.Height);
break;
case ContentAlignment.TopRight:
_textRect = new RectangleF(Width - _borderWidth - sf.Width, _borderWidth, sf.Width, sf.Height);
break;
case ContentAlignment.MiddleLeft:
_textRect = new RectangleF(_borderWidth, (Height - sf.Height) / 2f, sf.Width, sf.Height);
break;
case ContentAlignment.MiddleCenter:
_textRect = new RectangleF((Width - sf.Width) / 2f, (Height - sf.Height) / 2f, sf.Width, sf.Height);
break;
case ContentAlignment.MiddleRight:
_textRect = new RectangleF(Width - _borderWidth - sf.Width, (Height - sf.Height) / 2f, sf.Width, sf.Height);
break;
case ContentAlignment.BottomLeft:
_textRect = new RectangleF(_borderWidth, Height - _borderWidth - sf.Height, sf.Width, sf.Height);
break;
case ContentAlignment.BottomCenter:
_textRect = new RectangleF((Width - sf.Width) / 2f, Height - _borderWidth - sf.Height, sf.Width, sf.Height);
break;
case ContentAlignment.BottomRight:
_textRect = new RectangleF(Width - _borderWidth - sf.Width, Height - _borderWidth - sf.Height, sf.Width, sf.Height);
break;
}
}
private void FlatLabel_Paint(object sender, PaintEventArgs e)
{
if (BackBlue)
e.Graphics.Clear(Common.BLUE_COLOR);
else
e.Graphics.Clear(Common.BACK_COLOR);
if (_borderWidth > 0)
{
if (Inside)
e.Graphics.DrawRectangle(new Pen(Common.BLUE_COLOR, _borderWidth), _borderRect);
else
e.Graphics.DrawRectangle(new Pen(Common.BORDER_COLOR, _borderWidth), _borderRect);
}
e.Graphics.DrawString(Text, Font, Common.FORE_BRUSH, _textRect);
}
}
}