FlatForm.cs
11.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Asa.Theme
{
public partial class FlatForm : Form
{
private bool _center;
private bool _lost = false;
private int _titleHeight;
private Font _font = new Font("宋体", 9f);
private List<FlatBase> _enter = new List<FlatBase>();
private FlatBase _show;
private Rectangle _borderRect; //边框
private Rectangle _titleRect; //标题栏
private Rectangle _closeRect; //关闭按钮
private Rectangle _minRect; //最小化
private RectangleF _textRect; //标题文本
private bool _closeDown; //关闭按钮按下
private bool _minDown; //最小化按下
private bool _closeOver; //关闭按钮经过
private bool _minOver; //最小化经过
private bool _minimized;
private Point[] _closeIcon;
private Point[] _minIcon;
private const int BORDER_W = 2; //边框宽度
private readonly Pen BORDER_LOST = new Pen(Common.BORDER_COLOR, BORDER_W);
private readonly SolidBrush TITLE_LOST = new SolidBrush(Common.BORDER_COLOR);
public FlatForm()
{
InitializeComponent();
TitleHeight = 36;
Padding = new Padding(6, 6 + TitleHeight, 6, 6);
TitleHeight = 36;
MoveForm = true;
Minimized = true;
CalcSize();
}
[Browsable(true), Category(""), Description("标题文本"), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
set
{
base.Text = value;
CalcSize();
Refresh();
}
get
{
return base.Text;
}
}
[Browsable(true), Category(""), Description("标题文本是否居中显示"), DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool TextCenter
{
set
{
_center = value;
CalcSize();
Refresh();
}
get
{
return _center;
}
}
[Browsable(true), Category(""), Description("窗口是否最大化"), DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool Maximized { set; get; }
[Browsable(true), Category(""), Description("是否可以移动窗口"), DefaultValue(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool MoveForm { set; get; }
[Browsable(true), Category(""), Description("是否显示最小化按钮"), DefaultValue(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool Minimized
{
set
{
_minimized = value;
CalcSize();
Refresh();
}
get
{
return _minimized;
}
}
[Browsable(true), Category(""), Description("标题栏高度"), DefaultValue(36)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int TitleHeight
{
set
{
_titleHeight = value;
Padding = new Padding(6, 6 + TitleHeight, 6, 6);
CalcSize();
Refresh();
}
get
{
return _titleHeight;
}
}
[Browsable(true), Category(""), Description("标题栏文本字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public Font TitleFont
{
get { return _font; }
set
{
_font = value;
CalcSize();
Refresh();
}
}
/// <summary>
/// 对话框的语言
/// </summary>
[Browsable(false)]
public Dictionary<string, string> DialogLang { set; get; }
[Browsable(false)]
public override Font Font { get => base.Font; set => base.Font = value; }
[Browsable(false)]
public new FormWindowState WindowState { set => base.WindowState = value; get => base.WindowState; }
[Browsable(false)]
public new bool MaximizeBox { set => base.MaximizeBox = value; get => base.MaximizeBox; }
[Browsable(false)]
public new bool MinimizeBox { set => base.MinimizeBox = value; get => base.MinimizeBox; }
[Browsable(false)]
public override Size MaximumSize { get => base.MaximumSize; set => base.MaximumSize = value; }
[Browsable(false)]
public override Size MinimumSize { get => base.MinimumSize; set => base.MinimumSize = value; }
[Browsable(false)]
public new Padding Padding { set => base.Padding = value; get => base.Padding; }
/// <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);
}
public void SetVisible(FlatBase flat)
{
_show = flat;
}
private void CalcSize()
{
_borderRect = new Rectangle(BORDER_W / 2, BORDER_W / 2, Width - BORDER_W, Height - BORDER_W);
_titleRect = new Rectangle(BORDER_W, BORDER_W, Width - BORDER_W * 2, TitleHeight);
_closeRect = new Rectangle(Width - BORDER_W - 48, BORDER_W, 48, 32);
_minRect = new Rectangle(_closeRect.X - 48, BORDER_W, 48, 32);
_closeIcon = Shape.Multiply(_closeRect, 11);
_minIcon = Shape.Underline(_minRect, 11);
SizeF sf = CreateGraphics().MeasureString(Text, _font);
if (_center)
_textRect = new RectangleF((Width - sf.Width) / 2f, BORDER_W + (TitleHeight - sf.Height) / 2f, sf.Width, sf.Height);
else
_textRect = new RectangleF(6, BORDER_W + (TitleHeight - sf.Height) / 2f, sf.Width, sf.Height);
}
private void FlatForm_Load(object sender, EventArgs e)
{
BackColor = Common.BACK_COLOR;
if (Maximized)
{
Top = Left = 0;
Width = Screen.PrimaryScreen.WorkingArea.Width;
Height = Screen.PrimaryScreen.WorkingArea.Height;
}
}
private void FlatForm_Activated(object sender, EventArgs e)
{
_lost = false;
Refresh();
}
private void FlatForm_Deactivate(object sender, EventArgs e)
{
_lost = true;
Refresh();
}
private void FlatForm_Paint(object sender, PaintEventArgs e)
{
BufferedGraphicsContext current = BufferedGraphicsManager.Current;
BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = bg.Graphics;
g.Clear(Common.BACK_COLOR); //背景
if (_lost)
{
g.DrawRectangle(BORDER_LOST, _borderRect); //边框
g.FillRectangle(Common.BACK_BRUSH, _titleRect); //标题栏
g.DrawString(Text, _font, TITLE_LOST, _textRect); //标题文本
}
else
{
g.DrawRectangle(Common.BLUE_PEN, _borderRect); //边框
g.FillRectangle(Common.BACK_BRUSH, _titleRect); //标题栏
g.DrawString(Text, _font, Common.FORE_BRUSH, _textRect); //标题文本
}
if (_closeDown)
g.FillRectangle(Common.BLUE_BRUSH, _closeRect);
else if (_closeOver)
g.FillRectangle(Common.OVER_BRUSH, _closeRect);
for (int i = 0; i < _closeIcon.Length; i += 2)
g.DrawLine(Common.FORE_PEN, _closeIcon[i], _closeIcon[i + 1]);
if (_minimized)
{
if (_minDown)
g.FillRectangle(Common.BLUE_BRUSH, _minRect);
else if (_minOver)
g.FillRectangle(Common.OVER_BRUSH, _minRect);
g.DrawLine(Common.FORE_PEN, _minIcon[0], _minIcon[1]);
}
bg.Render();
bg.Dispose();
}
private void FlatForm_MouseDown(object sender, MouseEventArgs e)
{
if (_show != null)
{
_show.SetVisible();
_show = null;
}
if (e.Button == MouseButtons.Left)
{
if (_closeOver)
{
_closeDown = true;
Refresh();
}
else if (_minOver && _minimized)
{
_minDown = true;
Refresh();
}
else if (Maximized)
{
return;
}
else if (MoveForm && e.Y < TitleHeight)
{
Control.Move(Handle);
}
}
}
private void FlatForm_Resize(object sender, EventArgs e)
{
CalcSize();
Refresh();
}
private void FlatForm_MouseMove(object sender, MouseEventArgs e)
{
if (_enter.Count > 0)
{
for (int i = 0; i < _enter.Count; i++)
_enter[i].MouseFocusLeave();
_enter.Clear();
}
bool bln1 = _closeRect.Contains(e.Location);
bool bln2 = _minRect.Contains(e.Location);
if (bln1 != _closeOver || bln2 != _minOver)
{
_closeOver = bln1;
_minOver = bln2;
if (!_closeOver) _closeDown = false;
if (!_minOver) _minDown = false;
Refresh();
}
}
private void FlatForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
if (_closeDown)
{
_closeDown = false;
Refresh();
Close();
}
else if (_minDown && _minimized)
{
_minDown = false;
Refresh();
WindowState = FormWindowState.Minimized;
}
}
}
}