forked from unknwon/CharmControlLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharmTextBox.cs
377 lines (347 loc) · 11.7 KB
/
CharmTextBox.cs
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#region 文档说明
/* ****************************************************************************************************
* 文档作者:无闻
* 创建日期:2012年10月6日
* 文档用途:CharmTextBox - 文本框控件
* -----------------------------------------------------------------------------------------------------
* 修改记录:
* 2013-01-20(无闻):
* - 将类名从 cTexxBox 修改为 CharmTextBox
* - 规范类字段、属性的命令及相关方法
* 2013-01-21(无闻):修复 边框出现白框 的问题
* 2013-03-01:针对CSBox界面标准2.0进行升级改造
* -----------------------------------------------------------------------------------------------------
* 参考文献:
*
* *****************************************************************************************************/
#endregion
#region 命名空间引用
using System;
using System.Drawing;
using System.Windows.Forms;
using CharmCommonMethod;
using CharmControlLibrary.Properties;
#endregion
namespace CharmControlLibrary
{
#region 枚举类型
/// <summary>
/// 文本框输入模式:通常方式,只读方式,密码输入,整数输入
/// </summary>
public enum InputMode
{
/// <summary>
/// 通常方式,允许用户对文本框进行所有操作
/// </summary>
Normal,
/// <summary>
/// 只读方式,不允许用户进行任何输入
/// </summary>
ReadOnly,
/// <summary>
/// 密码输入,文本框将以密码框的形式呈现
/// </summary>
Password,
/// <summary>
/// 整数输入,只允许用户输入数字(0-9)或退格键
/// </summary>
Integer
}
#endregion
/// <summary>
/// 表示 CharmControlLibrary.CharmTextBox 文本框控件
/// </summary>
public class CharmTextBox : PictureBox
{
#region 字段
// 控件的文本框输入模式
private InputMode mInputMode;
// 文本框控件
private TextBox mTextbox;
// 控件的状态
private ControlStatus mControlStatus;
// 控件的状态图片
private Image[] mStatusImages;
// 控件的水印文本
private string mWatermark;
#endregion
#region 属性
/// <summary>
/// 获取或设置控件的宽度
/// </summary>
public new int Width
{
get { return base.Width; }
set
{
base.Width = value;
// 重新设置文本框大小
mTextbox.Size = new Size(base.Width - 4, mTextbox.Height);
mStatusImages[0] = ImageOperation.ResizeImageWithoutBorder(mStatusImages[0], 2, 1, 2, 1, base.Size);
mStatusImages[1] = ImageOperation.ResizeImageWithoutBorder(mStatusImages[1], 2, 2, 2, 2, base.Size);
}
}
/// <summary>
/// 获取或设置控件中文本的水平对齐方式
/// </summary>
public HorizontalAlignment TextAlign
{
get { return mTextbox.TextAlign; }
set { mTextbox.TextAlign = value; }
}
/// <summary>
/// 获取或设置控件的文本框输入模式
/// </summary>
public InputMode TextInputMode
{
get { return mInputMode; }
set
{
mInputMode = value;
// 如果输入类型为只读类型或密码输入则无水印效果
if (mInputMode == InputMode.ReadOnly || mInputMode == InputMode.Password)
{
mWatermark = null;
if (mInputMode == InputMode.Password)
mTextbox.PasswordChar = '●'; // 密码输入
}
}
}
/// <summary>
/// 获取或设置控件的水印文本
/// </summary>
public string Watermark
{
get { return mWatermark; }
set
{
mWatermark = value;
// 判断文本是否为空
if (string.Equals(mTextbox.Text, string.Empty))
{
mTextbox.ForeColor = Color.DarkGray;
mTextbox.Text = mWatermark;
}
}
}
/// <summary>
/// 获取或设置用户可在控件内输入的最大字符数
/// </summary>
public int MaxLength
{
get { return mTextbox.MaxLength; }
set
{
// 判断是否为非负整数
if (value >= 0)
mTextbox.MaxLength = value;
else
throw (new ArgumentException("MaxLength:文本框最大可输入字符数必须是非负整数."));
}
}
/// <summary>
/// 获取或设置文本框的内容
/// </summary>
public new string Text
{
get { return mTextbox.Text; }
set { mTextbox.Text = value; }
}
#endregion
#region 重载事件
/// <summary>
/// 控件重绘事件
/// </summary>
protected override void OnPaint(PaintEventArgs pe)
{
// 获取绘制对象
Graphics g = pe.Graphics;
// 判断控件状态,绘制背景
switch (mControlStatus)
{
case ControlStatus.Normal:
g.DrawImage(mStatusImages[0], new Rectangle(new Point(0, 0), mStatusImages[0].Size));
break;
case ControlStatus.Hover:
g.DrawImage(mStatusImages[1], new Rectangle(new Point(0, 0), mStatusImages[1].Size));
break;
}
}
#endregion
#region 自定义事件
/// <summary>
/// 定义文本框双击事件委托
/// </summary>
public delegate void DoubleClickEventHandler(object sender, EventArgs e);
/// <summary>
/// 当双击控件时发生
/// </summary>
public new event DoubleClickEventHandler DoubleClick;
/// <summary>
/// 定义文本框文本改变事件委托
/// </summary>
public delegate void TextChangedEventHandler(object sender, EventArgs e);
/// <summary>
/// 当控件文本改变时发生
/// </summary>
public new event TextChangedEventHandler TextChanged;
// 鼠标进入事件
private void TextBox_MouseEnter(object sender, EventArgs e)
{
// 修改背景
mControlStatus = ControlStatus.Hover;
Invalidate();
// 判断是否有水印
if (mWatermark != null)
{
// 判断文本是否为默认文本
if (string.Equals(mTextbox.Text, mWatermark))
{
mTextbox.Text = string.Empty;
mTextbox.ForeColor = Color.Black;
}
}
}
// 鼠标离开事件
private void TextBox_MouseLeave(object sender, EventArgs e)
{
// 修改背景
mControlStatus = ControlStatus.Normal;
Invalidate();
// 判断是否拥有焦点且有水印
if (!mTextbox.Focused)
{
if (mWatermark != null)
{
// 判断文本是否为空
if (string.Equals(mTextbox.Text, string.Empty))
{
mTextbox.ForeColor = Color.DarkGray;
mTextbox.Text = mWatermark;
}
}
}
}
// 键盘按键事件
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
// 判断输入类型
switch (mInputMode)
{
case InputMode.Normal:
break;
case InputMode.ReadOnly:
e.Handled = true;
break;
case InputMode.Password:
break;
case InputMode.Integer:
if (!(char.IsDigit(e.KeyChar) | e.KeyChar == '\b'))
{
e.Handled = true;
}
break;
}
}
// 获得焦点事件
private void TextBox_GotFocus(object sender, EventArgs e)
{
// 判断是否有水印
if (mWatermark != null)
{
// 判断文本是否为默认文本
if (string.Equals(mTextbox.Text, mWatermark))
{
mTextbox.Text = string.Empty;
mTextbox.ForeColor = Color.Black;
}
}
}
// 失去焦点事件
private void TextBox_LostFocus(object sender, EventArgs e)
{
// 判断是否有水印
if (mWatermark != null)
{
// 判断文本是否为空
if (string.Equals(mTextbox.Text, string.Empty))
{
mTextbox.ForeColor = Color.DarkGray;
mTextbox.Text = mWatermark;
}
}
}
#endregion
#region 控件事件
// 文本框被双击事件
private void mTextbox_DoubleClick(object sender, EventArgs e)
{
if (DoubleClick != null)
DoubleClick(sender, e);
}
// 文本改变事件
private void mTextbox_TextChanged(object sender, EventArgs e)
{
if (TextChanged != null)
TextChanged(sender, e);
}
#endregion
#region 构造方法
/// <summary>
/// 初始化 CharmTextBox 类的新实例
/// </summary>
public CharmTextBox()
: base()
{
// * 初始化属性 *
Size = new Size(160, 26);
BackColor = Color.White;
mStatusImages = new Image[2];
mStatusImages[0] = Resources.textbox_bkg_normal;
// 去除常态背景空白部分
mStatusImages[0] =
ImageOperation.GetPartOfImage(mStatusImages[0], mStatusImages[0].Width, mStatusImages[0].Height - 2, 0, 1);
mStatusImages[1] = Resources.textbox_bkg_hover;
// 重新设置文本框大小
mStatusImages[0] = ImageOperation.ResizeImageWithoutBorder(mStatusImages[0], 2, 1, 2, 1, base.Size);
mStatusImages[1] = ImageOperation.ResizeImageWithoutBorder(mStatusImages[1], 2, 2, 2, 2, base.Size);
// * 文本框外观样式设置 *
mTextbox = new TextBox();
mTextbox.BorderStyle = BorderStyle.None; // 无边框
mTextbox.Font = new Font("微软雅黑", 9.75F); // 字体
mTextbox.Size = new Size(156, 20); // 大小
mTextbox.Location = new Point(2, (Height - mTextbox.Height) / 2); // 位置
mTextbox.ImeMode = ImeMode.NoControl; // 输入法设置
// 设置水印
if (mWatermark != null)
{
mTextbox.ForeColor = Color.DarkGray;
mTextbox.Text = mWatermark;
}
// * 控件属性设置 *
mInputMode = InputMode.Normal; // 输入模式
// * 关联控件事件 *
mTextbox.MouseEnter += new EventHandler(TextBox_MouseEnter);
mTextbox.MouseLeave += new EventHandler(TextBox_MouseLeave);
mTextbox.KeyPress += new KeyPressEventHandler(TextBox_KeyPress);
mTextbox.LostFocus += new EventHandler(TextBox_LostFocus);
mTextbox.GotFocus += new EventHandler(TextBox_GotFocus);
mTextbox.DoubleClick += new EventHandler(mTextbox_DoubleClick);
mTextbox.TextChanged += new EventHandler(mTextbox_TextChanged);
// * 加载控件到控件集合中 *
Controls.Add(mTextbox);
}
#endregion
#region 方法
/// <summary>
/// 设置文本框文本,用于有水印时正常显示内容
/// </summary>
/// <param name="text">文本内容</param>
public void SetText(string text)
{
mTextbox.ForeColor = Color.Black;
mTextbox.Text = text;
}
#endregion
}
}