-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDateControl.cs
485 lines (400 loc) · 13.9 KB
/
DateControl.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Diagnostics;
using DigitalPlatform.GUI;
namespace DigitalPlatform.CommonControl
{
public partial class DateControl : UserControl
{
string m_strCaption = "";
int IngoreTextChange = 0;
[Category("New Event")]
public event EventHandler DateTextChanged = null;
public DateControl()
{
this.m_bInitial = true;
InitializeComponent();
this.m_bInitial = false;
m_oldTextBoxLocation = this.maskedTextBox_date.Location;
OnTextBoxHeightChanged(); // 初始化高度
}
[Category("Appearance")]
[DescriptionAttribute("Caption")]
[DefaultValue(typeof(string), "")]
public string Caption
{
get
{
return this.m_strCaption;
}
set
{
this.m_strCaption = value;
}
}
[Category("Appearance")]
[DescriptionAttribute("Border style of the control")]
[DefaultValue(typeof(System.Windows.Forms.BorderStyle), "None")]
public new BorderStyle BorderStyle
{
get
{
return base.BorderStyle;
}
set
{
base.BorderStyle = value;
OnTextBoxHeightChanged(); // 边框改变,导致高度改变
}
}
bool m_bEnabled = true;
Color m_savedBackColor = SystemColors.Window;
public new bool Enabled
{
get
{
return base.Enabled;
}
set
{
// base.Enabled = Enabled;
base.Enabled = value;
this.maskedTextBox_date.Enabled = value;
this.Invalidate();
this.Update();
}
}
private void maskedTextBox_date_TextChanged(object sender, EventArgs e)
{
if (IngoreTextChange > 0)
return;
if (this.DateTextChanged != null)
{
this.DateTextChanged(this, e); // 205/5/25 以前为 sender
}
// this.OnTextChanged(e);
}
public override string Text
{
get
{
return this.maskedTextBox_date.Text;
}
set
{
this.maskedTextBox_date.Text = value;
}
}
// 当前value是否为空值
public bool IsValueNull()
{
if (this.Value == new DateTime((long)0))
return true;
return false;
}
public DateTime Value
{
get
{
// get pure text
string strPureText = GetPureDateText();
if (strPureText.Trim() == "")
return new DateTime((long)0);
try
{
DateTime date = DateTime.Parse(this.maskedTextBox_date.Text,
this.maskedTextBox_date.Culture,
DateTimeStyles.NoCurrentDateDefault);
return date;
}
catch
{
return new DateTime((long)0);
}
}
set
{
if (value == new DateTime((long)0))
{
this.maskedTextBox_date.Text = "";
return;
}
this.maskedTextBox_date.Text = GetDateString(value);
}
}
static string GetDateString(DateTime date)
{
return date.Year.ToString().PadLeft(4, '0') + "年"
+ date.Month.ToString().PadLeft(2, '0') + "月"
+ date.Day.ToString().PadLeft(2, '0') + "日";
}
string GetPureDateText()
{
// get pure text
MaskFormat oldformat = this.maskedTextBox_date.TextMaskFormat;
this.IngoreTextChange++;
this.maskedTextBox_date.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
string strPureText = this.maskedTextBox_date.Text;
this.maskedTextBox_date.TextMaskFormat = oldformat;
this.IngoreTextChange--;
return strPureText;
}
private void maskedTextBox_date_Validating(object sender, CancelEventArgs e)
{
string strPureText = GetPureDateText();
if (strPureText.Trim() == "")
return; // blank value
try
{
DateTime date = DateTime.Parse(this.maskedTextBox_date.Text,
this.maskedTextBox_date.Culture,
DateTimeStyles.NoCurrentDateDefault);
}
catch (Exception ex)
{
MessageBox.Show(this, this.Text + " error : " + ex.Message);
e.Cancel = true;
}
}
private void button_findDate_Click(object sender, EventArgs e)
{
GetDateDlg dlg = new GetDateDlg();
GuiUtil.SetControlFont(dlg, this.Font);
dlg.Text = this.Caption;
dlg.DateTime = this.Value;
// dlg.StartLocation = Control.MousePosition;
dlg.StartPosition = FormStartPosition.Manual;
dlg.Location = this.PointToScreen(
new Point(this.Margin.Horizontal,
4 + this.Height + this.Margin.Vertical)
);
dlg.ShowDialog(this);
if (dlg.DialogResult != DialogResult.OK)
return;
this.Value = dlg.DateTime;
}
private void DateControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left
&& this.maskedTextBox_date.Enabled == true)
{
if (GuiUtil.PtInRect(e.X, e.Y, this.RectButton) == true)
{
button_findDate_Click(sender, e);
}
}
}
Rectangle RectButton
{
get
{
int nButtonHeight = this.Height /*- this.KernelMargin.Vertical*/ - BorderSize.Height * 2 - 1;
int nButtonWidth = (int)((float)nButtonHeight * 0.75);
int nCenterY = this.Height / 2;
int x = this.ClientRectangle.Width - nButtonWidth - 1/* - this.KernelMargin.Right*/;
int y = nCenterY - nButtonHeight / 2 - BorderSize.Height - 1;
return new Rectangle(x, y, nButtonWidth, nButtonHeight);
}
}
Size BorderSize
{
get
{
Size border_size = new System.Drawing.Size(0, 0);
if (this.BorderStyle == BorderStyle.Fixed3D)
border_size = SystemInformation.Border3DSize;
else if (this.BorderStyle == System.Windows.Forms.BorderStyle.FixedSingle)
border_size = SystemInformation.BorderSize;
return border_size;
}
}
Padding KernelMargin
{
get
{
int nDelta = 0;
return new System.Windows.Forms.Padding(this.Margin.Left + nDelta, this.Margin.Top + nDelta,
this.Margin.Right + nDelta, this.Margin.Bottom + nDelta);
}
}
bool m_bInitial = false;
Point m_oldTextBoxLocation;
int m_nInHeightChanging = 0;
void OnTextBoxHeightChanged()
{
if (this.m_nInHeightChanging > 0)
return;
int nDelta = this.m_oldTextBoxLocation.Y;
this.m_nInHeightChanging++;
this.Height = this.maskedTextBox_date.Height
+ 2 * nDelta /*+ this.KernelMargin.Vertical*/ + this.Padding.Vertical
+ BorderSize.Height * 2;
this.m_nInHeightChanging--;
this.maskedTextBox_date.Location = new Point(/*this.KernelMargin.Left + */this.Padding.Left + this.m_oldTextBoxLocation.X,
/*this.KernelMargin.Top + */this.Padding.Top + this.m_oldTextBoxLocation.Y);
this.maskedTextBox_date.Width = this.Width /*- this.KernelMargin.Horizontal*/ - this.Padding.Horizontal - this.RectButton.Width - BorderSize.Width * 2 - 1;
this.Invalidate();
}
public void SelectAll()
{
this.maskedTextBox_date.SelectAll();
}
public new bool Focus()
{
return this.maskedTextBox_date.Focus();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
OnTextBoxHeightChanged();
}
static void DrawFlatComboButton(Graphics g,
Rectangle rect,
bool bActive)
{
using (Brush brush = new SolidBrush(
SystemColors.ButtonFace))
{
g.FillRectangle(brush, rect);
}
if (bActive == true)
{
using (Pen penBorder = new Pen(SystemColors.Window))
{
g.DrawRectangle(penBorder, rect);
}
}
int nCenterX = rect.X + rect.Width / 2 + (rect.Width % 2);
int nCenterY = rect.Y + rect.Height / 2 + (rect.Height % 2);
using (Pen pen = new Pen(
bActive ? SystemColors.ControlText : SystemColors.GrayText))
{
Point pt1 = new Point(nCenterX - 2, nCenterY - 1);
Point pt2 = new Point(nCenterX + 2, nCenterY - 1);
g.DrawLine(pen, pt1, pt2);
pt1 = new Point(nCenterX - 1, nCenterY);
pt2 = new Point(nCenterX + 1, nCenterY);
g.DrawLine(pen, pt1, pt2);
pt1 = new Point(nCenterX, nCenterY + 1);
using (Brush brush = new SolidBrush(pen.Color))
{
g.FillRectangle(brush, pt1.X, pt1.Y, 1, 1); // draw one pixel
}
// g.DrawLine(pen, pt1, pt2);
}
}
private void DateControl_Paint(object sender, PaintEventArgs e)
{
if (this.maskedTextBox_date.Enabled == false)
{
using (Brush brush = new SolidBrush(SystemColors.Control))
{
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}
using (Pen pen = new Pen(Color.DarkGray))
{
Rectangle rect = this.ClientRectangle;
rect.Height--;
rect.Width--;
// 画边框
e.Graphics.DrawRectangle(pen, rect);
}
// 按钮
if (this.m_flatstyle == System.Windows.Forms.FlatStyle.Flat)
{
DrawFlatComboButton(e.Graphics, this.RectButton, false);
}
else
{
if (ComboBoxRenderer.IsSupported == true)
{
ComboBoxRenderer.DrawDropDownButton(e.Graphics,
this.RectButton,
System.Windows.Forms.VisualStyles.ComboBoxState.Disabled);
}
else
{
ControlPaint.DrawComboButton(e.Graphics,
this.RectButton,
ButtonState.Inactive);
}
}
}
else
{
using (Brush brush = new SolidBrush(SystemColors.Window))
{
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}
// 按钮
if (this.m_flatstyle == System.Windows.Forms.FlatStyle.Flat)
{
DrawFlatComboButton(e.Graphics, this.RectButton, true);
}
else
{
if (ComboBoxRenderer.IsSupported == true)
{
ComboBoxRenderer.DrawDropDownButton(e.Graphics,
this.RectButton,
System.Windows.Forms.VisualStyles.ComboBoxState.Normal);
}
else
{
ControlPaint.DrawComboButton(e.Graphics,
this.RectButton,
ButtonState.Normal);
}
}
}
}
FlatStyle m_flatstyle = FlatStyle.Standard;
[Category("Appearance")]
[DescriptionAttribute("FlatStyle")]
[DefaultValue(typeof(FlatStyle), "Standard")]
public FlatStyle FlatStyle
{
get
{
return this.m_flatstyle;
}
set
{
this.m_flatstyle = value;
}
}
private void DateControl_FontChanged(object sender, EventArgs e)
{
OnTextBoxHeightChanged();
}
private void DateControl_PaddingChanged(object sender, EventArgs e)
{
OnTextBoxHeightChanged();
}
protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
{
// 模仿ComboBox TextBox等,Location和Size可以被缩放,但是Margin和Padding不变
Padding margin = this.Margin;
Padding Padding = this.Padding;
base.ScaleControl(factor, specified);
this.Margin = margin;
this.Padding = Padding;
}
public new Control.ControlCollection Controls
{
get
{
if (this.m_bInitial == false)
return new ControlCollection(this);
else
return base.Controls;
}
}
}
}