FlatPanel.cs
9.4 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Asa.Theme
{
[DefaultValue("Text")]
public partial class FlatPanel : Panel
{
private Rectangle _borderRect = new Rectangle();
private Rectangle _titleRect = new Rectangle();
private RectangleF _textRect = new RectangleF();
private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;
private List<FlatBase> _enter = new List<FlatBase>();
private int _borderWidth;
private int _titleHeight;
/// <summary>
///
/// </summary>
public FlatPanel()
{
InitializeComponent();
MouseEnter += FlatPanel_MouseEnter;
MouseLeave += FlatPanel_MouseLeave;
MouseMove += FlatPanel_MouseMove;
Paint += FlatPanel_Paint;
Resize += FlatPanel_Resize;
base.Text = "";
_titleHeight = 24;
_borderWidth = 2;
Padding = new Padding(6, 6 + _titleHeight, 6, 6);
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("显示的文本的字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
set
{
base.Font = value;
CalcSize();
Refresh();
}
get
{
return base.Font;
}
}
/// <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("显示的标题的高度"), DefaultValue(24)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int TitleHeight
{
set
{
if (value < 0)
_titleHeight = 0;
else
_titleHeight = value;
Padding = new Padding(6, 6 + _titleHeight, 6, 6);
CalcSize();
Refresh();
}
get
{
return _titleHeight;
}
}
/// <summary>
/// 控件的边框的宽度
/// </summary>
[Browsable(true), Category(""), Description("控件的边框的宽度"), DefaultValue(2)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int BorderWidth
{
set
{
_borderWidth = value;
CalcSize();
Refresh();
}
get
{
return _borderWidth;
}
}
[Browsable(false)]
public override bool AutoScroll { get => base.AutoScroll; set => base.AutoScroll = value; }
[Browsable(false)]
public override bool AutoSize { get => base.AutoSize; set => base.AutoSize = value; }
[Browsable(false)]
public override Point AutoScrollOffset { get => base.AutoScrollOffset; set => base.AutoScrollOffset = value; }
[Browsable(false)]
public override bool AllowDrop { get => base.AllowDrop; set => base.AllowDrop = value; }
[Browsable(false)]
public override Color ForeColor { get => base.ForeColor; set => base.ForeColor = value; }
[Browsable(false)]
public override RightToLeft RightToLeft { get => base.RightToLeft; set => base.RightToLeft = value; }
[Browsable(false)]
public override Color BackColor { get => base.BackColor; set => base.BackColor = value; }
[Browsable(false)]
public override Image BackgroundImage { get => base.BackgroundImage; set => base.BackgroundImage = value; }
[Browsable(false)]
public override ImageLayout BackgroundImageLayout { get => base.BackgroundImageLayout; set => base.BackgroundImageLayout = value; }
[Browsable(false)]
public new Padding Padding { set => base.Padding = value; get => base.Padding; }
/// <summary>
/// 鼠标焦点在控件里面
/// </summary>
public bool Inside { set; get; }
/// <summary>
/// 焦点进入的控件
/// </summary>
/// <param name="flat"></param>
public void FocusEnter(FlatBase flat)
{
_enter.Add(flat);
}
/// <summary>
/// 焦点离开的控件
/// </summary>
/// <param name="flat"></param>
public void FocusLeave(FlatBase flat)
{
_enter.Remove(flat);
}
private void CalcSize()
{
_borderRect = new Rectangle(_borderWidth / 2, _borderWidth / 2, Width - _borderWidth, Height - _borderWidth);
_titleRect.Width = Width;
_titleRect.Height = _titleHeight;
if (_titleHeight == 0) return;
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, (_titleHeight - sf.Height) / 2f, sf.Width, sf.Height);
break;
case ContentAlignment.MiddleCenter:
_textRect = new RectangleF((Width - sf.Width) / 2f, (_titleHeight - sf.Height) / 2f, sf.Width, sf.Height);
break;
case ContentAlignment.MiddleRight:
_textRect = new RectangleF(Width - _borderWidth - sf.Width, (_titleHeight - sf.Height) / 2f, sf.Width, sf.Height);
break;
case ContentAlignment.BottomLeft:
_textRect = new RectangleF(_borderWidth, _titleHeight - _borderWidth - sf.Height, sf.Width, sf.Height);
break;
case ContentAlignment.BottomCenter:
_textRect = new RectangleF((Width - sf.Width) / 2f, _titleHeight - _borderWidth - sf.Height, sf.Width, sf.Height);
break;
case ContentAlignment.BottomRight:
_textRect = new RectangleF(Width - _borderWidth - sf.Width, _titleHeight - _borderWidth - sf.Height, sf.Width, sf.Height);
break;
}
}
private void FlatPanel_Resize(object sender, EventArgs e)
{
CalcSize();
Refresh();
}
private void FlatPanel_MouseLeave(object sender, EventArgs e)
{
System.Windows.Forms.Control ctl = this.Parent;
Rectangle rect = new Rectangle(ctl.Left + this.Left, ctl.Top + this.Top, this.Width, this.Height);
bool rtn = rect.Contains(MousePosition);
if (!rtn)
{
Inside = false;
Refresh();
}
}
private void FlatPanel_MouseEnter(object sender, EventArgs e)
{
Inside = true;
Refresh();
}
private void FlatPanel_MouseMove(object sender, MouseEventArgs e)
{
//if (_enter.Count > 0)
//{
// for (int i = 0; i < _enter.Count; i++)
// _enter[i].MouseFocusLeave();
// _enter.Clear();
//}
}
private void FlatPanel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Common.BACK_COLOR);
if (Inside)
e.Graphics.DrawRectangle(Common.BLUE_PEN, _borderRect);
else
e.Graphics.DrawRectangle(Common.BORDER_PEN, _borderRect);
if (_titleHeight > 0)
{
e.Graphics.FillRectangle(Common.BLUE_BRUSH, _titleRect);
e.Graphics.DrawString(Text, Font, Common.FORE_BRUSH, _textRect);
}
}
}
}