| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346 |
-
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using Dongke.WinForm.Utilities;
- namespace Dongke.WinForm.Controls
- {
- /// <summary>
- /// 日期控件,该控件用来让用户选择日期和时间并以指定的格式显示此日期和时间。
- /// </summary>
- public abstract class DateTimePickerBase : DateTimePicker, IDKControl,
- IDataVerifiable, IAsyncControl, IChildNativeWindow
- {
- #region 常量
- /// <summary>
- /// 日期时间格式过滤字符
- /// </summary>
- private const string DATETIME_FORMAT_S = "[^yMdHhmsfFt]";
- #endregion
- #region 获取UpDown控件
- [DllImport("user32.dll", ExactSpelling = true)]
- private static extern bool EnumChildWindows(HandleRef hwndParent, DateTimePickerBase.EnumChildrenCallback lpEnumFunc, HandleRef lParam);
- private delegate bool EnumChildrenCallback(IntPtr hwnd, IntPtr lParam);
- private sealed class EnumChildren
- {
- public IntPtr hwndFound = IntPtr.Zero;
- public bool enumChildren(IntPtr hwnd, IntPtr lparam)
- {
- this.hwndFound = hwnd;
- return true;
- }
- }
- #endregion
- #region 事件声明
- /*
- #region TextPaste
- /// <summary>
- /// 当输入文本粘贴后发生
- /// </summary>
- private static readonly object EventTextPaste = new object();
- /// <summary>
- /// TextPaste 事件
- /// </summary>
- public event TextPasteEventHandler TextPaste
- {
- add
- {
- base.Events.AddHandler(EventTextPaste, value);
- }
- remove
- {
- base.Events.RemoveHandler(EventTextPaste, value);
- }
- }
- #endregion
- */
- #region HasErrorChanged
- /// <summary>
- /// 当 HasError 属性的值更改时发生。
- /// </summary>
- private static readonly object EventHasErrorChanged = new object();
- /// <summary>
- /// 当 HasError 属性的值更改时发生。
- /// </summary>
- [Description("当 HasError 属性的值更改时发生。"), Category("CustomerEx")]
- public event EventHandler HasErrorChanged
- {
- add
- {
- base.Events.AddHandler(EventHasErrorChanged, value);
- }
- remove
- {
- base.Events.RemoveHandler(EventHasErrorChanged, value);
- }
- }
- #endregion
- #endregion
- #region 成员变量
- private ChildNativeWindow _updown = null;
- /// <summary>
- /// 焦点是否进入控件
- /// </summary>
- private bool _entered = false;
- /// <summary>
- /// 鼠标是否进入控件
- /// </summary>
- private bool _mouseOver = false;
- /// <summary>
- /// 边框颜色
- /// </summary>
- private Color? _borderColor = null;
- /// <summary>
- /// 自定义日期/时间格式字符串
- /// </summary>
- private string _customFormat = Constant.DATETIME_FORMAT_YYYYMMDDHHMMSS;
- /// <summary>
- /// 控件中显示的日期和时间格式
- /// </summary>
- private DateTimePickerFormat _format = DateTimePickerFormat.Custom;
- /// <summary>
- /// 指示是否设定了日期时间值
- /// </summary>
- private bool _nullValue = false;
- /// <summary>
- /// 指示控件是否允许输入null
- /// </summary>
- private bool _allowNull = true;
- /// <summary>
- /// 不触发ValueChanged
- /// </summary>
- private bool _isSetValue = false;
- /// <summary>
- /// 日期文本(无标点)格式化
- /// </summary>
- private string _dateTextFormat = null;
- /// <summary>
- /// 日历选择显示
- /// </summary>
- private bool _isDropDown = false;
- #endregion
- #region 构造函数
- /// <summary>
- /// 日期控件,该控件用来让用户选择日期和时间并以指定的格式显示此日期和时间。
- /// </summary>
- public DateTimePickerBase()
- {
- base.ImeMode = ImeMode.Off;
- this.Value = null;
- this._dateTextFormat = Regex.Replace(this._customFormat, DATETIME_FORMAT_S, string.Empty);
- CommonSetting.InitControls(this);
- }
- #endregion
- #region 属性
- /// <summary>
- /// 获取一个值,该值指示控件是否有输入焦点。
- /// </summary>
- [DefaultValue(false)]
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public virtual bool Entered
- {
- get
- {
- return this._entered;
- }
- protected set
- {
- if (this._entered != value)
- {
- this._entered = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取一个值,该值指示鼠标是否在控件上方。
- /// </summary>
- [DefaultValue(false)]
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public virtual bool MouseOver
- {
- get
- {
- return this._mouseOver;
- }
- protected set
- {
- if (this._mouseOver != value)
- {
- this._mouseOver = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取日期的开始时间(该日期的00:00:00)
- /// </summary>
- [Browsable(true), Description("获取日期的开始时间(该日期的00:00:00)"), Category("CustomerEx")]
- public virtual DateTime? BeginTime
- {
- get
- {
- if (this._nullValue)
- {
- return null;
- }
- return this.Value.Value.Date;
- }
- }
- /// <summary>
- /// 获取日期的结束时间(该日期的23:59:59)
- /// </summary>
- [Browsable(true), Description("获取日期的结束时间(该日期的23:59:59)"), Category("CustomerEx")]
- public virtual DateTime? EndTime
- {
- get
- {
- if (this._nullValue)
- {
- return null;
- }
- DateTime dt = this.Value.Value;
- return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
- }
- }
- /// <summary>
- /// 获取或设置与此控件关联的日期文本(无格式)。
- /// </summary>
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public virtual string DateText
- {
- get
- {
- if (this._nullValue)
- {
- return string.Empty;
- }
- // 日期时间改变时,控件闪烁。
- //if (this.Focused)
- //{
- // // 输入不完整时,自动补齐日期。
- // SendKeys.SendWait("{RIGHT}");
- // SendKeys.SendWait("{LEFT}");
- //}
- return this.Value.Value.ToString(this._dateTextFormat);
- }
- set
- {
- if (value == null || value.Length == 0)
- {
- this.Value = null;
- return;
- }
- DateTime dt;
- if (DateTime.TryParseExact(value, this._dateTextFormat, null, System.Globalization.DateTimeStyles.None, out dt))
- {
- this.Value = dt;
- }
- }
- }
- #endregion
- #region 重写属性
- /// <summary>
- /// 获取或设置自定义日期/时间格式字符串。
- /// </summary>
- [DefaultValue(Constant.DATETIME_FORMAT_YYYYMMDDHHMMSS)]
- public new virtual string CustomFormat
- {
- get
- {
- return this._customFormat;
- }
- set
- {
- this._customFormat = value;
- this._dateTextFormat = Regex.Replace(this._customFormat, DATETIME_FORMAT_S, string.Empty);
- if (!this._nullValue)
- {
- base.CustomFormat = value;
- }
- }
- }
- /// <summary>
- /// 获取或设置控件中显示的日期和时间格式。
- /// </summary>
- [DefaultValue(typeof(DateTimePickerFormat), "Custom")]
- public new virtual DateTimePickerFormat Format
- {
- get
- {
- return this._format;
- }
- set
- {
- this._format = value;
- if (!this._nullValue)
- {
- base.Format = value;
- }
- }
- }
- /// <summary>
- /// 获取或设置与此控件关联的格式化文本。
- /// </summary>
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- [EditorBrowsable(EditorBrowsableState.Advanced)]
- public override string Text
- {
- get
- {
- if (this._nullValue)
- {
- return string.Empty;
- }
- // 日期时间改变时,控件闪烁。
- //// 输入不完整时,自动补齐日期。
- //if (this.Focused && !this._isDropDown)
- //{
- // SendKeys.SendWait("{RIGHT}");
- // SendKeys.SendWait("{LEFT}");
- //}
- return base.Text;
- }
- set
- {
- if (value == null || value.Length == 0)
- {
- this.Value = null;
- return;
- }
- if (this._nullValue)
- {
- this.SetDateValueNotNull();
- }
- base.Text = value;
- }
- }
- /// <summary>
- /// 获取或设置分配给控件的日期/时间值。
- /// </summary>
- //[Browsable(false)]
- //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- //[EditorBrowsable(EditorBrowsableState.Never)]
- [Bindable(true)]
- [DefaultValue(typeof(DateTime?), "")]
- [RefreshProperties(RefreshProperties.All)]
- [Description("获取或设置分配给控件的日期/时间值。"), Category("CustomerEx")]
- public new virtual DateTime? Value
- {
- get
- {
- if (this._allowNull && this._nullValue)
- {
- return null;
- }
- // 日期时间改变时,控件闪烁。
- //// 输入不完整时,自动补齐日期。
- //if (this.Focused && !this._isDropDown)
- //{
- // SendKeys.SendWait("{RIGHT}");
- // SendKeys.SendWait("{LEFT}");
- //}
- return base.Value;
- }
- set
- {
- if (value.HasValue)
- {
- this.SetDateValueNotNull(value.Value);
- }
- else
- {
- if (!this._allowNull)
- {
- return;
- }
- if (!this._nullValue)
- {
- this.SetDateValueNull();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示控件是否允许输入null值。
- /// </summary>
- [DefaultValue(true)]
- [RefreshProperties(RefreshProperties.All)]
- [Description("获取或设置一个值,该值指示控件是否允许输入null值。"), Category("CustomerEx")]
- public virtual bool AllowNull
- {
- get
- {
- return this._allowNull;
- }
- set
- {
- this._allowNull = value;
- if (!this._allowNull && this._nullValue)
- {
- this.SetDateValueNotNull();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件的输入法编辑器 (IME) 模式。
- /// </summary>
- [Browsable(false)]
- [EditorBrowsable(EditorBrowsableState.Never)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public new ImeMode ImeMode
- {
- get
- {
- return base.ImeMode;
- }
- set
- {
- base.ImeMode = value;
- }
- }
- /// <summary>
- /// 获取一个用以指示是否可以将 System.Windows.Forms.Control.ImeMode 属性设置为活动值的值,以启用 IME 支持。
- /// </summary>
- protected override bool CanEnableIme
- {
- get
- {
- return false;
- }
- }
- #endregion
- #region 重写事件
- #region 属性改变
- /// <summary>
- /// 引发 HasErrorChanged 事件
- /// </summary>
- /// <param name="e">包含事件数据的 EventArgs</param>
- protected virtual void OnHasErrorChanged(EventArgs e)
- {
- EventHandler eventHandler = (EventHandler)base.Events[EventHasErrorChanged];
- if (eventHandler != null)
- {
- eventHandler(this, e);
- }
- }
- /// <summary>
- /// 文本改变事件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnTextChanged(EventArgs e)
- {
- base.OnTextChanged(EventArgs.Empty);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValueChanged(EventArgs e)
- {
- if (this._nullValue && !this._isSetValue)
- {
- return;
- }
- // 不显示天数时,如果是31日,没有31日的月份会报错。
- if (!this._nullValue &&
- this.Format == DateTimePickerFormat.Custom &&
- this.CustomFormat != null &&
- !this.CustomFormat.Contains("d") &&
- this.Value.Value.Day > 28)
- {
- this.Value = this.Value.Value.AddDays(1 - this.Value.Value.Day);
- }
- base.OnValueChanged(e);
- // TODO 实时验证?
- if (true)
- {
- this.ValidateData();
- }
- }
- #endregion
- #region 行为事件
- /// <summary>
- /// 当显示下拉日历时发生。
- /// </summary>
- /// <param name="e"></param>
- protected override void OnDropDown(EventArgs e)
- {
- base.OnDropDown(e);
- this._isDropDown = true;
- }
- /// <summary>
- /// 当下拉日历被关闭并消失时发生
- /// </summary>
- /// <param name="e"></param>
- protected override void OnCloseUp(EventArgs e)
- {
- if (!this._allowNull)
- {
- base.OnCloseUp(e);
- return;
- }
- if (this.ShowCheckBox && !this.Checked)
- {
- base.OnCloseUp(e);
- return;
- }
- if (Control.MouseButtons == MouseButtons.None)
- {
- if (this._nullValue)
- {
- //this.Value = base.Value;
- this.SetDateValueNotNull(base.Value);
- }
- }
- base.OnCloseUp(e);
- this._isDropDown = false;
- }
- #endregion
- #region 键盘事件
- /// <summary>
- /// 当有按键动作时发生
- /// </summary>
- /// <param name="e"></param>
- protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
- {
- if (!this._allowNull)
- {
- base.OnPreviewKeyDown(e);
- return;
- }
- if (this.ShowCheckBox && !this.Checked)
- {
- base.OnPreviewKeyDown(e);
- return;
- }
- // 删除时间
- if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
- {
- this.Value = null;
- base.OnPreviewKeyDown(e);
- return;
- }
- if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) ||
- (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||
- (e.KeyCode >= Keys.PageUp && e.KeyCode <= Keys.Down))
- {
- //PageUp = 33,
- //Next = 34,
- //PageDown = 34,
- //End = 35,
- //Home = 36,
- //Left = 37,
- //Up = 38,。
- //Right = 39,
- //Down = 40,
- if (this._nullValue)
- {
- this.SetDateValueNotNull();
- if (e.KeyCode != Keys.Right)
- {
- SendKeys.SendWait("{RIGHT}");
- }
- }
- }
- base.OnPreviewKeyDown(e);
- }
- /// <summary>
- /// 键盘输入
- /// </summary>
- /// <param name="e"></param>
- protected override void OnKeyPress(KeyPressEventArgs e)
- {
- base.OnKeyPress(e);
- }
- /// <summary>
- /// 键盘点击
- /// </summary>
- /// <param name="e"></param>
- protected override void OnKeyDown(KeyEventArgs e)
- {
- base.OnKeyDown(e);
- }
- #endregion
- #region 焦点事件
- /// <summary>
- /// 输入焦点进入控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnEnter(EventArgs e)
- {
- this.Entered = true;
- base.OnEnter(e);
- }
- /// <summary>
- /// 获得焦点
- /// </summary>
- /// <param name="e"></param>
- protected override void OnGotFocus(EventArgs e)
- {
- base.OnGotFocus(e);
- }
- /// <summary>
- /// 失去焦点
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLostFocus(EventArgs e)
- {
- base.OnLostFocus(e);
- }
- /// <summary>
- /// 输入焦点离开控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnLeave(EventArgs e)
- {
- base.OnLeave(e);
- }
- /// <summary>
- /// 控件正在验证
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValidating(CancelEventArgs e)
- {
- base.OnValidating(e);
- if (this.HasError && !this._canLostFocusOnError)
- {
- e.Cancel = true;
- }
- }
- /// <summary>
- /// 控件完成验证
- /// </summary>
- /// <param name="e"></param>
- protected override void OnValidated(EventArgs e)
- {
- this.Entered = false;
- base.OnValidated(e);
- }
- #endregion
- #region 鼠标事件
- /// <summary>
- /// 鼠标进入控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseEnter(EventArgs e)
- {
- this.MouseOver = true;
- base.OnMouseEnter(e);
- }
- /// <summary>
- /// 鼠标离开控件
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- this.MouseOver = false;
- }
- /// <summary>
- /// 鼠标移动
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- }
- /// <summary>
- /// 鼠标按下
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- }
- /// <summary>
- /// 鼠标抬起
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- }
- #endregion
- #endregion
- #region 重写方法
- /// <summary>
- /// 返回表示当前 System.Windows.Forms.DateTimePicker 控件的字符串。
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- if (this._nullValue)
- {
- return string.Empty;
- }
- return base.ToString();
- }
- /*
- public struct NMHDR
- {
- public IntPtr hwndFrom;
- public IntPtr idFrom;
- public int code;
- }
- [StructLayout(LayoutKind.Sequential)]
- public class SYSTEMTIME
- {
- public short wYear;
- public short wMonth;
- public short wDayOfWeek;
- public short wDay;
- public short wHour;
- public short wMinute;
- public short wSecond;
- public short wMilliseconds;
- public override string ToString()
- {
- return string.Concat(new string[]
- {
- "[SYSTEMTIME: ",
- this.wDay.ToString(CultureInfo.InvariantCulture),
- "/",
- this.wMonth.ToString(CultureInfo.InvariantCulture),
- "/",
- this.wYear.ToString(CultureInfo.InvariantCulture),
- " ",
- this.wHour.ToString(CultureInfo.InvariantCulture),
- ":",
- this.wMinute.ToString(CultureInfo.InvariantCulture),
- ":",
- this.wSecond.ToString(CultureInfo.InvariantCulture),
- "]"
- });
- }
- }
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
- public class NMDATETIMECHANGE
- {
- public NMHDR nmhdr;
- public int dwFlags;
- public SYSTEMTIME st;
- }
- internal static SYSTEMTIME DateTimeToSysTime(DateTime time)
- {
- return new SYSTEMTIME
- {
- wYear = (short)time.Year,
- wMonth = (short)time.Month,
- wDayOfWeek = (short)time.DayOfWeek,
- wDay = (short)time.Day,
- wHour = (short)time.Hour,
- wMinute = (short)time.Minute,
- wSecond = (short)time.Second,
- wMilliseconds = 0
- };
- }
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, SYSTEMTIME lParam);
- */
- /// <summary>
- /// 处理 Windows 消息
- /// </summary>
- /// <param name="m">要处理的 Windows System.Windows.Forms.Message</param>
- protected override void WndProc(ref Message m)
- {
- /*
- if (m.Msg == 0x204E)
- {
- if (m.HWnd == base.Handle)
- {
- NMDATETIMECHANGE nMDATETIMECHANGE = (NMDATETIMECHANGE)m.GetLParam(typeof(NMDATETIMECHANGE));
- if (nMDATETIMECHANGE.nmhdr.code == -759)
- {
- if (nMDATETIMECHANGE.dwFlags != 1)
- {
- if (nMDATETIMECHANGE.st.wYear == 0)
- {
- return;
- }
- }
- }
- }
- }
- */
- if (this.ShowUpDown)
- {
- if (this._updown == null)
- {
- DateTimePickerBase.EnumChildren enumChildren = new DateTimePickerBase.EnumChildren();
- DateTimePickerBase.EnumChildrenCallback lpEnumFunc = new DateTimePickerBase.EnumChildrenCallback(enumChildren.enumChildren);
- DateTimePickerBase.EnumChildWindows(new HandleRef(this, base.Handle), lpEnumFunc, WindowsAPI.NullHandleRef);
- if (enumChildren.hwndFound != IntPtr.Zero)
- {
- this._updown = new ChildNativeWindow(this, "UpDown");
- this._updown.AssignHandle(enumChildren.hwndFound);
- }
- }
- }
- else
- {
- if (this._updown != null)
- {
- this._updown.ReleaseHandle();
- this._updown = null;
- }
- }
- base.WndProc(ref m);
- // 释放句柄
- if (m.Msg == (int)WindowsMessage.WM_NCDESTROY)
- {
- if (this._updown != null)
- {
- this._updown.ReleaseHandle();
- this._updown = null;
- }
- return;
- }
- if (m.Msg == (int)WindowsMessage.WM_PAINT ||
- m.Msg == (int)WindowsMessage.WM_NCPAINT ||
- m.Msg == (int)WindowsMessage.WM_CTLCOLOREDIT)
- {
- BorderColorPaint.WmBorderPaint(this._borderColor, this.Width, this.Height, ref m);
- }
- }
- #endregion
- #region 公有方法
- #endregion
- #region 保护方法
- /// <summary>
- /// 处理DateTimePickerBase内部控件的消息
- /// </summary>
- /// <param name="child">UpDown</param>
- /// <param name="m">消息</param>
- /// <returns>消息是否已经处理过</returns>
- public virtual bool ChildWndProc(ChildNativeWindow child, ref Message m)
- {
- switch (m.Msg)
- {
- case (int)WindowsMessage.WM_LBUTTONDOWN:
- if (this._nullValue)
- {
- this.SetDateValueNotNull();
- if (!this.Focused)
- {
- this.Focus();
- }
- SendKeys.SendWait("{RIGHT}");
- return true;
- }
- break;
- }
- return false;
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 使父控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向父控件发送绘制消息。
- /// </summary>
- private void InvalidateBorder()
- {
- Color? borderColor = BorderColorPaint.GetBorderColor(this as IDataVerifiable, this._entered, this._mouseOver);
- if (borderColor != this._borderColor)
- {
- this._borderColor = borderColor;
- if (this.Parent == null)
- {
- this.Invalidate();
- }
- else
- {
- this.Parent.Invalidate(this.Bounds, true);
- }
- }
- }
- /// <summary>
- /// 设置日期
- /// </summary>
- /// <param name="value">日期</param>
- private void SetDateValueNotNull(DateTime value)
- {
- if (this._nullValue)
- {
- base.Format = this._format;
- base.CustomFormat = this._customFormat;
- this._nullValue = false;
- if (base.Value != value)
- {
- this.SetBaseValue(value);
- }
- else
- {
- this.OnValueChanged(EventArgs.Empty);
- this.OnValidated(EventArgs.Empty);
- }
- }
- else
- {
- if (base.Value != value)
- {
- this.SetBaseValue(value);
- }
- }
- }
- /// <summary>
- /// 设置空日期
- /// </summary>
- private void SetDateValueNull()
- {
- base.Format = DateTimePickerFormat.Custom;
- base.CustomFormat = Constant.S_SPACE;
- this._nullValue = true;
- // TODO null 时的默认时间
- this.SetBaseValue(DateTime.Now);
- }
- /// <summary>
- /// 设置日期(当前时间)
- /// </summary>
- private void SetDateValueNotNull()
- {
- base.Format = this._format;
- base.CustomFormat = this._customFormat;
- this._nullValue = false;
- // TODO null 时的默认时间
- this.SetBaseValue(DateTime.Now);
- }
- /// <summary>
- /// 设置日期
- /// </summary>
- /// <param name="vaue">日期</param>
- private void SetBaseValue(DateTime vaue)
- {
- this._isSetValue = true;
- base.Value = vaue;
- this._isSetValue = false;
- if (!this.Focused)
- {
- this.OnValidated(EventArgs.Empty);
- }
- }
- #endregion
- #region IDataVerifiable 成员
- #region 成员变量
- /// <summary>
- /// 显示边框颜色
- /// </summary>
- private bool _showBorderColor = true;
- /// <summary>
- /// 控件的项目名
- /// </summary>
- private string _itemName = null;
- /// <summary>
- /// 控件是否是必须输入项目
- /// </summary>
- private bool _mustInput = false;
- /// <summary>
- /// 控件在验证输入错误时,如何提示
- /// </summary>
- private InputErrorAlert _errorAlert = InputErrorAlert.Validated;
- /// <summary>
- /// 是否显示必须输入项目的提示
- /// </summary>
- private bool _showMustInputAlert = true;
- /// <summary>
- /// 验证不通过时,焦点能否离开
- /// </summary>
- private bool _canLostFocusOnError = true;
- /// <summary>
- /// 验证是否有错误
- /// </summary>
- private bool _hasError = false;
- /// <summary>
- /// 是否自定义错误
- /// </summary>
- private bool _hasCustomerError = false;
- /// <summary>
- /// 错误编码
- /// </summary>
- private ControlErrorCode _errorCode = ControlErrorCode.DKC_0000;
- /// <summary>
- /// 错误消息
- /// </summary>
- private string _errorMessage = null;
- /// <summary>
- /// 自定义错误消息
- /// </summary>
- private string _customerErrorMessage = null;
- #endregion
- #region 属性
- /// <summary>
- /// 获取或设置控件是否显示边框颜色。
- /// </summary>
- [Description("获取或设置控件是否显示边框颜色。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool ShowBorderColor
- {
- get
- {
- return this._showBorderColor;
- }
- set
- {
- if (this._showBorderColor != value)
- {
- this._showBorderColor = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件的项目名
- /// </summary>
- [Description("获取或设置控件的项目名。"), Category("IDataVerifiable")]
- [DefaultValue(null)]
- public string CDItemName
- {
- get
- {
- return this._itemName;
- }
- set
- {
- this._itemName = value;
- }
- }
- /// <summary>
- /// 获取或设置控件是否必须选中项目。
- /// </summary>
- [Description("获取或设置控件是否必须选中项目。"), Category("IDataVerifiable")]
- [DefaultValue(false)]
- public bool MustInput
- {
- get
- {
- return this._mustInput;
- }
- set
- {
- if (this._mustInput != value)
- {
- this._mustInput = value;
- this.ValidateData();
- if (this._mustInput && this._showMustInputAlert)
- {
- this.InvalidateBorder();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否必须选中项目。
- /// </summary>
- [Description("获取或设置控件在验证输入错误时,如何提示。"), Category("IDataVerifiable")]
- [DefaultValue(typeof(InputErrorAlert), "Validated")]
- public InputErrorAlert InputErrorAlert
- {
- get
- {
- return this._errorAlert;
- }
- set
- {
- if (this._errorAlert != value)
- {
- this._errorAlert = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否显示必须输入项目提示
- /// </summary>
- [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool ShowMustInputAlert
- {
- get
- {
- return this._showMustInputAlert;
- }
- set
- {
- if (this._showMustInputAlert != value)
- {
- this._showMustInputAlert = value;
- this.InvalidateBorder();
- }
- }
- }
- /// <summary>
- /// 获取或设置当验证不通过时,控件是否可以失去焦点
- /// </summary>
- [Description("获取或设置当验证不通过时,控件是否可以失去焦点。"), Category("IDataVerifiable")]
- [DefaultValue(true)]
- public bool CanLostFocusOnError
- {
- get
- {
- return this._canLostFocusOnError;
- }
- set
- {
- this._canLostFocusOnError = value;
- }
- }
- /// <summary>
- /// 获取控件校验时是否有错误
- /// </summary>
- [Description("获取控件校验时是否有错误。"), Category("IDataVerifiable")]
- [DefaultValue(false)]
- public bool HasError
- {
- get
- {
- return this._hasCustomerError || this._hasError;
- }
- }
- /// <summary>
- /// 获取控件校验时的错误编码
- /// </summary>
- [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
- [DefaultValue(typeof(ControlErrorCode), "DKC_0000")]
- public ControlErrorCode ErrorCode
- {
- get
- {
- return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this._errorCode;
- }
- }
- /// <summary>
- /// 获取控件校验时的错误消息
- /// </summary>
- [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
- [DefaultValue(null)]
- public string ErrorMessage
- {
- get
- {
- return this._hasCustomerError ? this._customerErrorMessage : this._errorMessage;
- }
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 设置自定义错误
- /// </summary>
- /// <param name="hasError">输入是否有错误</param>
- /// <param name="errorMessage">错误消息</param>
- public virtual void SetCustomerError(bool hasError, string errorMessage)
- {
- if (this._hasCustomerError != hasError ||
- this._customerErrorMessage != errorMessage)
- {
- this._hasCustomerError = hasError;
- this._customerErrorMessage = errorMessage;
- this.OnHasErrorChanged(EventArgs.Empty);
- this.InvalidateBorder();
- }
- }
- /// <summary>
- /// 清除自定义错误
- /// </summary>
- public virtual void ClearCustomerError()
- {
- this.SetCustomerError(false, null);
- }
- /// <summary>
- /// 验证输入内容
- /// </summary>
- /// <returns>验证结果</returns>
- public virtual bool ValidateData()
- {
- if (this._mustInput && this._nullValue)
- {
- this.SetError(true, ControlErrorCode.DKC_0001, this._itemName);
- return true;
- }
- this.ClearError();
- return false;
- }
- /// <summary>
- /// 清除输入项
- /// </summary>
- public virtual void ClearValue()
- {
- //this.Value = null;
- if (this._allowNull)
- {
- this.Value = null;
- }
- else
- {
- this.Value = DateTime.Now;
- }
- }
- #endregion
- #region 保护方法
- /// <summary>
- /// 设置验证不通过错误
- /// </summary>
- /// <param name="hasError">是否有错误</param>
- /// <param name="code">错误编码</param>
- /// <param name="args">设置格式的对象</param>
- protected void SetError(bool hasError, ControlErrorCode code, params object[] args)
- {
- if (this._hasError != hasError ||
- this._errorCode != code)
- {
- this._hasError = hasError;
- this._errorCode = code;
- if (args != null && args.Length > 0)
- {
- this._errorMessage = string.Format(code.GetDescription(), args);
- }
- else
- {
- this._errorMessage = code.GetDescription();
- }
- this.OnHasErrorChanged(EventArgs.Empty);
- this.InvalidateBorder();
- }
- }
- /// <summary>
- /// 清除验证不通过错误
- /// </summary>
- protected void ClearError()
- {
- this.SetError(false, ControlErrorCode.DKC_0000);
- }
- #endregion
- #endregion
- #region IAsyncControl 成员
- /// <summary>
- /// 异步处理开始时,控件状态
- /// </summary>
- private bool _asyncBeginStatus = false;
- private bool _asyncBeginFocused = false;
- /// <summary>
- /// 异步处理开始
- /// </summary>
- /// <param name="doFocus">是否处理焦点</param>
- public virtual void BeginAsync(ref bool doFocus)
- {
- this._asyncBeginFocused = false;
- if (doFocus && this.Focused)
- {
- this._asyncBeginFocused = true;
- doFocus = false;
- }
- this._asyncBeginStatus = this.Enabled;
- this.Enabled = false;
- }
- /// <summary>
- /// 异步处理结束
- /// </summary>
- public virtual void EndAsync()
- {
- this.Enabled = this._asyncBeginStatus;
- if (this._asyncBeginFocused)
- {
- this.Focus();
- }
- }
- #endregion
- }
- }
|