SurTextBox.cs
10.1 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
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.Face
{
/// <summary>
/// 文本控件
/// </summary>
[DefaultValue("Text")]
[DefaultEvent("TextChanged")]
public partial class SurTextBox : BaseCtl
{
private TextBox _txt = new TextBox();
private bool change = false;
private string _textHint = "";
private bool _textExist;
/// <summary>
/// 文本控件
/// </summary>
public SurTextBox()
{
InitializeComponent();
_txt.BorderStyle = BorderStyle.None;
_txt.BackColor = Common.BACK_COLOR;
_txt.ForeColor = Common.FORE_COLOR;
_txt.TextChanged += Txt_TextChanged;
_txt.GotFocus += Txt_GotFocus;
_txt.LostFocus += Txt_LostFocus;
_txt.KeyDown += Txt_KeyDown;
_txt.KeyPress += Txt_KeyPress;
_txt.KeyUp += Txt_KeyUp;
Controls.Add(_txt);
SurTextBox_Resize(null, EventArgs.Empty);
if (_txt.Text.Length == 0)
{
_textExist = false;
_txt.Text = _textHint;
_txt.ForeColor = Common.BORDER_COLOR;
}
}
//==========事件==========
/// <summary>
/// 显示的文本改变时发生
/// </summary>
[Browsable(true), Category(""), Description("显示的文本改变时发生")]
public new event MyEvent.Changed TextChanged;
[Browsable(true), Category(""), Description("")]
public new event KeyEventHandler KeyDown;
[Browsable(true), Category(""), Description("")]
public new event KeyEventHandler KeyUp;
[Browsable(true), Category(""), Description("")]
public new event KeyPressEventHandler KeyPress;
//==========属性==========
/// <summary>
/// 选中的文本起始位置
/// </summary>
[Browsable(false), Category(""), Description("选中的文本起始位置")]
public int SelectionStart { set => _txt.SelectionStart = value; get => _txt.SelectionStart; }
/// <summary>
/// 选中的文本长度
/// </summary>
[Browsable(false), Category(""), Description("选中的文本长度")]
public int SelectionLength { set => _txt.SelectionLength = value; get => _txt.SelectionLength; }
/// <summary>
/// 选中的文本
/// </summary>
[Browsable(false), Category(""), Description("选中的文本")]
public string SelectedText { set => _txt.SelectedText = value; get => _txt.SelectedText; }
/// <summary>
/// 控件最大字符数
/// </summary>
[Browsable(false), Category(""), Description("控件最大字符数")]
public int MaxLength { set => _txt.MaxLength = value; get => _txt.MaxLength; }
/// <summary>
/// 显示文本只读不可更改
/// </summary>
[Browsable(true), Category(""), Description("显示文本只读不可更改"), DefaultValue(false)]
public bool ReadOnly { set => _txt.ReadOnly = value; get => _txt.ReadOnly; }
/// <summary>
/// 显示文本的对齐方式
/// </summary>
[Browsable(true), Category(""), Description("显示文本的对齐方式"), DefaultValue(HorizontalAlignment.Left)]
public HorizontalAlignment TextAlign { set => _txt.TextAlign = value; get => _txt.TextAlign; }
/// <summary>
/// 是否为多行文本
/// </summary>
[Browsable(true), Category(""), Description("是否为多行文本"), DefaultValue(false)]
public bool Multiline
{
set
{
_txt.Multiline = value;
SurTextBox_Resize(null, EventArgs.Empty);
}
get
{
return _txt.Multiline;
}
}
/// <summary>
/// 显示的文本
/// </summary>
[Browsable(true), Category(""), Description("显示的文本"), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get => base.Text;
set
{
base.Text = value;
if (value.Length == 0 && Focused)
{
_textExist = false;
_txt.Text = _textHint;
_txt.ForeColor = Common.BORDER_COLOR;
}
else
{
if (!_textExist)
{
_textExist = true;
_txt.ForeColor = Common.FORE_COLOR;
}
}
if (!change)
{
change = true;
_txt.Text = value;
change = false;
}
}
}
/// <summary>
/// 文本提示
/// </summary>
[Browsable(true), Category(""), Description("文本提示"), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string TextHint
{
get
{
return _textHint;
}
set
{
_textHint = value;
if (!_textExist)
_txt.Text = value;
}
}
/// <summary>
/// 控件中文本的字体
/// </summary>
[Browsable(true), Category(""), Description("控件中文本的字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
get => base.Font;
set
{
base.Font = value;
_txt.Top = BorderWidth + (Height - BorderWidth - BorderWidth - _txt.Height) / 2;
}
}
/// <summary>
/// 控件是否可用
/// </summary>
[Browsable(true), Category(""), Description("控件是否可用"), DefaultValue(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public new bool Enabled
{
set
{
base.Enabled = value;
_txt.Enabled = value;
Refresh();
}
get
{
return base.Enabled;
}
}
/// <summary>
/// 用于密码字符
/// </summary>
[Browsable(true), Category(""), Description("用于密码字符"), DefaultValue(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public char PasswordChar
{
set
{
_txt.PasswordChar = value;
}
get
{
return _txt.PasswordChar;
}
}
/// <summary>
/// 选中所有文本
/// </summary>
public void SelectAll()
{
_txt.SelectAll();
}
//==========私有==========
/// <summary>
/// 计算位置大小
/// </summary>
protected override void CalcSize()
{
base.CalcSize();
}
private void Txt_GotFocus(object sender, EventArgs e)
{
_txt.Text = Text;
_textExist = true;
_txt.ForeColor = Common.FORE_COLOR;
}
private void Txt_LostFocus(object sender, EventArgs e)
{
if (_txt.Text.Length == 0)
{
_textExist = false;
_txt.Text = _textHint;
_txt.ForeColor = Common.BORDER_COLOR;
}
}
private void Txt_TextChanged(object sender, EventArgs e)
{
if (!_textExist) return;
if (!change)
{
change = true;
Text = _txt.Text;
change = false;
}
TextChanged?.Invoke(this);
}
private void Txt_KeyDown(object sender, KeyEventArgs e)
{
KeyDown?.Invoke(this, e);
}
private void Txt_KeyUp(object sender, KeyEventArgs e)
{
KeyUp?.Invoke(this, e);
}
private void Txt_KeyPress(object sender, KeyPressEventArgs e)
{
KeyPress?.Invoke(this, e);
}
private void SurTextBox_Paint(object sender, PaintEventArgs e)
{
BufferedGraphicsContext current = BufferedGraphicsManager.Current;
BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = bg.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(Common.BACK_COLOR); //背景
if (Inside && Enabled)
{
if (BorderWidth > 0)
g.DrawRectangle(BorderInsidePen, BorderRect);
}
else
{
if (BorderWidth > 0)
g.DrawRectangle(BorderOutsidePen, BorderRect);
}
bg.Render();
bg.Dispose();
}
private void SurTextBox_Resize(object sender, EventArgs e)
{
if (_txt.Multiline)
{
_txt.Left = BorderWidth + 3;
_txt.Top = BorderWidth + 3;
_txt.Width = Width - BorderWidth - BorderWidth - 3 - 3;
_txt.Height = Height - BorderWidth - BorderWidth - 3 - 3;
}
else
{
_txt.Left = BorderWidth + 3;
_txt.Width = Width - BorderWidth - BorderWidth - 3 - 3;
_txt.Top = BorderWidth + (Height - BorderWidth - BorderWidth - _txt.Height) / 2;
}
}
}
}