FlatLanguage.cs
10.5 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
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;
using System.Drawing.Drawing2D;
namespace Asa.Theme
{
[DefaultProperty("LangDir")]
[DefaultEvent("LanguageChanged")]
public partial class FlatLanguage : FlatBase
{
private Language _lang = new Language();
private string _langPath;
private FlatForm[] _frm;
private int _index;
public event Event.StringEvent LanguageChanged;
private GraphicsPath _borderPath; //边框路径
private GraphicsPath _leftPath; //左边按钮边框路径
private GraphicsPath _rightPath; //右边按钮边框路径
private RectangleF _leftRect; //左边按钮矩形
private RectangleF _rightRect; //右边按钮矩形
private RectangleF _leftStrRect; //左边文本矩形
private RectangleF _rightStrRect; //右边文本矩形
private string _leftStr = "LEFT"; //左边文本
private string _rightStr = "RIGHT"; //右边文本
private bool _leftOver; //左边按钮经过
private bool _rightOver; //右边按钮经过
private bool _leftDown; //左边按钮按下
private bool _rightDown; //右边按钮按下
private const int RADIUS = 5; //半径
private const int DIAMETER = RADIUS * 2; //直径
private const int SPACE = 5; //间隔
public FlatLanguage()
{
InitializeComponent();
}
/// <summary>
/// 语言文件目录
/// </summary>
public string LangDir
{
set
{
_langPath = value;
if (_langPath != null)
{
_lang.Load(value);
string[] s = _lang.Name;
if (s.Length == 2)
{
_leftStr = s[0];
_rightStr = s[1];
}
CalcSize();
Refresh();
}
}
get
{
return _langPath;
}
}
/// <summary>
/// 关联到窗体
/// </summary>
/// <param name="frm"></param>
public void Link(params FlatForm[] frm)
{
_frm = frm;
}
/// <summary>
/// 加载语言
/// </summary>
/// <param name="s"></param>
public void LoadLanguage(string s)
{
int n = Array.FindIndex(_lang.Name, ss => ss == s);
if (n == 0)
{
_leftDown = true;
_rightDown = false;
_index = 0;
Refresh();
SetLang(0);
}
else if (n == 1)
{
_leftDown = false;
_rightDown = true;
_index = 1;
Refresh();
SetLang(1);
}
}
/// <summary>
/// 设置语言
/// </summary>
/// <param name="frm"></param>
public void SetLanguage(FlatForm frm)
{
_lang.Shift(_index, frm);
}
protected override void CalcSize()
{
// 1 2
// -----
// | |
// -----
// 3 4
float x1, x2, y1, y2;
//边框路径
x1 = Width - DIAMETER - 1;
y1 = Height - DIAMETER - 1;
RectangleF rect1 = new RectangleF(0, 0, DIAMETER, DIAMETER);
RectangleF rect2 = new RectangleF(x1, 0, DIAMETER, DIAMETER);
RectangleF rect3 = new RectangleF(0, y1, DIAMETER, DIAMETER);
RectangleF rect4 = new RectangleF(x1, y1, DIAMETER, DIAMETER);
_borderPath = new GraphicsPath();
_borderPath.AddArc(rect1, 180, 90);
_borderPath.AddLine(rect1.X + RADIUS, rect1.Y, rect2.X + RADIUS, rect2.Y);
_borderPath.AddArc(rect2, 270, 90);
_borderPath.AddLine(rect2.Right, rect2.Y + RADIUS, rect4.Right, rect4.Y + RADIUS);
_borderPath.AddArc(rect4, 0, 90);
_borderPath.AddLine(rect4.X + RADIUS, rect4.Bottom, rect3.X + RADIUS, rect3.Bottom);
_borderPath.AddArc(rect3, 90, 90);
_borderPath.AddLine(rect3.X, rect3.Y + RADIUS, rect1.X, rect1.Y + RADIUS);
//左边按钮路径
x1 = (Width - SPACE * 3) / 2f + SPACE - DIAMETER - 1;
rect1 = new RectangleF(SPACE, SPACE, DIAMETER, DIAMETER);
rect2 = new RectangleF(x1, SPACE, DIAMETER, DIAMETER);
rect3 = new RectangleF(SPACE, y1 - SPACE, DIAMETER, DIAMETER);
rect4 = new RectangleF(x1, y1 - SPACE, DIAMETER, DIAMETER);
_leftRect = new RectangleF(rect1.X, rect1.Y, rect2.Right - rect1.X, rect3.Bottom - rect1.Y);
_leftPath = new GraphicsPath();
_leftPath.AddArc(rect1, 180, 90);
_leftPath.AddLine(rect1.X + RADIUS, rect1.Y, rect2.X + RADIUS, rect2.Y);
_leftPath.AddArc(rect2, 270, 90);
_leftPath.AddLine(rect2.Right, rect2.Y + RADIUS, rect4.Right, rect4.Y + RADIUS);
_leftPath.AddArc(rect4, 0, 90);
_leftPath.AddLine(rect4.X + RADIUS, rect4.Bottom, rect3.X + RADIUS, rect3.Bottom);
_leftPath.AddArc(rect3, 90, 90);
_leftPath.AddLine(rect3.X, rect3.Y + RADIUS, rect1.X, rect1.Y + RADIUS);
//左边文字区域
SizeF sf = CreateGraphics().MeasureString(_leftStr, Common.FONT_SONG_9);
x2 = SPACE + (rect2.Right - rect1.X - sf.Width) / 2f;
y2 = (Height - sf.Height) / 2f;
_leftStrRect = new RectangleF(x2, y2, sf.Width, sf.Height);
//右边按钮路径
x1 += (DIAMETER + SPACE + 1);
x2 = Width - SPACE - DIAMETER - 1;
rect1 = new RectangleF(x1, SPACE, DIAMETER, DIAMETER);
rect2 = new RectangleF(x2, SPACE, DIAMETER, DIAMETER);
rect3 = new RectangleF(x1, y1 - SPACE, DIAMETER, DIAMETER);
rect4 = new RectangleF(x2, y1 - SPACE, DIAMETER, DIAMETER);
_rightRect = new RectangleF(rect1.X, rect1.Y, rect2.Right - rect1.X, rect3.Bottom - rect1.Y);
_rightPath = new GraphicsPath();
_rightPath.AddArc(rect1, 180, 90);
_rightPath.AddLine(rect1.X + RADIUS, rect1.Y, rect2.X + RADIUS, rect2.Y);
_rightPath.AddArc(rect2, 270, 90);
_rightPath.AddLine(rect2.Right, rect2.Y + RADIUS, rect4.Right, rect4.Y + RADIUS);
_rightPath.AddArc(rect4, 0, 90);
_rightPath.AddLine(rect4.X + RADIUS, rect4.Bottom, rect3.X + RADIUS, rect3.Bottom);
_rightPath.AddArc(rect3, 90, 90);
_rightPath.AddLine(rect3.X, rect3.Y + RADIUS, rect1.X, rect1.Y + RADIUS);
//右边文字区域
sf = CreateGraphics().MeasureString(_rightStr, Common.FONT_SONG_9);
x2 = x1 + (rect2.Right - x1 - sf.Width) / 2f;
y2 = (Height - sf.Height) / 2f;
_rightStrRect = new RectangleF(x2, y2, sf.Width, sf.Height);
}
private void SetLang(int idx)
{
if (_frm == null) return;
_lang.Shift(idx, _frm);
}
private void FlatLanguage_MouseDown(object sender, MouseEventArgs e)
{
if (_leftOver)
{
if (!_leftDown)
{
_leftDown = true;
_rightDown = false;
Refresh();
SetLang(0);
_index = 0;
LanguageChanged?.Invoke(this, _lang.Name[0]);
}
}
else if (_rightOver)
{
if (!_rightDown)
{
_leftDown = false;
_rightDown = true;
Refresh();
SetLang(1);
_index = 1;
LanguageChanged?.Invoke(this, _lang.Name[1]);
}
}
}
private void FlatLanguage_MouseMove(object sender, MouseEventArgs e)
{
bool bb = false;
bool bln = _leftRect.Contains(e.Location);
if (bln != _leftOver)
{
_leftOver = bln;
bb = true;
}
bln = _rightRect.Contains(e.Location);
if (bln != _rightOver)
{
_rightOver = bln;
bb = true;
}
if (bb) Refresh();
}
private void FlatLanguage_MouseUp(object sender, MouseEventArgs e)
{
}
private void FlatLanguage_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.Clear(Common.BACK_COLOR); //背景
if (Inside)
e.Graphics.DrawPath(Common.BLUE_PEN, _borderPath); //进入边框
else
e.Graphics.DrawPath(Common.BORDER_PEN, _borderPath); //离开边框
if (_leftDown)
{
e.Graphics.FillPath(Common.BLUE_BRUSH, _leftPath);
}
else if (_leftOver)
{
e.Graphics.FillPath(Common.OVER_BRUSH, _leftPath);
e.Graphics.DrawPath(Common.BORDER_PEN, _leftPath);
}
else
{
e.Graphics.DrawPath(Common.BORDER_PEN, _leftPath);
}
if (_rightDown)
{
e.Graphics.FillPath(Common.BLUE_BRUSH, _rightPath);
}
else if (_rightOver)
{
e.Graphics.FillPath(Common.OVER_BRUSH, _rightPath);
e.Graphics.DrawPath(Common.BORDER_PEN, _rightPath);
}
else
{
e.Graphics.DrawPath(Common.BORDER_PEN, _rightPath);
}
e.Graphics.DrawString(_leftStr, Common.FONT_SONG_9, Common.FORE_BRUSH, _leftStrRect);
e.Graphics.DrawString(_rightStr, Common.FONT_SONG_9, Common.FORE_BRUSH, _rightStrRect);
}
}
}