DateTimePickerEx.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.ComponentModel;
  9. using System.Runtime.InteropServices;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. public class DateTimePickerEx : System.Windows.Forms.DateTimePicker
  13. {
  14. public DateTimePickerEx()
  15. {
  16. this.Format = DateTimePickerFormat.Custom;
  17. this.CustomFormat = " ";
  18. this.ValueChanged += DateTimePickerEx_ValueChanged;
  19. this.KeyDown += DateTimePickerEx_KeyDown;
  20. this.CloseUp += DateTimePickerEx_CloseUp;
  21. }
  22. void DateTimePickerEx_CloseUp(object sender, EventArgs e)
  23. {
  24. DateTimePickerEx_ValueChanged(null, null);
  25. }
  26. void DateTimePickerEx_KeyDown(object sender, KeyEventArgs e)
  27. {
  28. if (e == null || e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
  29. {
  30. this.dateValue = "";
  31. setCustomFormat();
  32. }
  33. //throw new NotImplementedException();
  34. }
  35. void DateTimePickerEx_ValueChanged(object sender, EventArgs e)
  36. {
  37. if (this.Value > LYFZ.WinAPI.CustomPublicMethod.GetNullDateTime())
  38. {
  39. if (IsShowTime)
  40. {
  41. this.dateValue = this.Value.ToString(timeFormat);
  42. }
  43. else
  44. {
  45. this.dateValue = this.Value.ToString(dateFormat);
  46. }
  47. }
  48. else {
  49. this.dateValue = "";
  50. }
  51. setCustomFormat();
  52. }
  53. string timeFormat = "yyyy-MM-dd HH:mm";
  54. string dateFormat = "yyyy-MM-dd";
  55. bool isShowTime = false;
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. [Category("控件扩展属性"), Description("是否显示时间")]
  60. public bool IsShowTime
  61. {
  62. get { return isShowTime; }
  63. set
  64. {
  65. isShowTime = value;
  66. setCustomFormat();
  67. Invalidate(false);
  68. }
  69. }
  70. void setCustomFormat()
  71. {
  72. if (IsEmpty)
  73. {
  74. this.CustomFormat = " ";
  75. }
  76. else
  77. {
  78. if (IsShowTime)
  79. {
  80. this.CustomFormat = timeFormat;
  81. }
  82. else
  83. {
  84. this.CustomFormat = dateFormat;
  85. }
  86. }
  87. }
  88. DateTime minimumDate = Convert.ToDateTime("1900-01-01");
  89. string dateValue = "";
  90. /// <summary>
  91. /// 获取日期字符串
  92. /// </summary>
  93. [Category("控件扩展属性"), Description("获取日期字符串")]
  94. public string DateValue
  95. {
  96. get { return dateValue; }
  97. set
  98. {
  99. dateValue = value;
  100. DateTime tempTime;
  101. if (DateTime.TryParse(dateValue, out tempTime) && tempTime > minimumDate)
  102. {
  103. try
  104. {
  105. this.Value = tempTime;
  106. }
  107. catch { }
  108. }
  109. else
  110. {
  111. // this.Value = DateTime.Now;
  112. this.dateValue = "";
  113. //DateTimePickerEx_KeyDown(null, null);
  114. }
  115. setCustomFormat();
  116. Invalidate(false);
  117. }
  118. }
  119. /// <summary>
  120. /// 获取日期时间字符串
  121. /// </summary>
  122. [Category("控件扩展属性"), Description("获取日期时间字符串")]
  123. public string DateTimeValue
  124. {
  125. get { return dateValue+" "+SDateTime.Now.ToString("HH:mm:ss"); }
  126. }
  127. /// <summary>
  128. ///
  129. /// </summary>
  130. [Category("控件扩展属性"), Description("是否为空")]
  131. public bool IsEmpty
  132. {
  133. get
  134. {
  135. bool bl = true;
  136. if (dateValue.Trim() == "")
  137. {
  138. bl = true;
  139. }
  140. else
  141. {
  142. bl = false;
  143. }
  144. return bl;
  145. }
  146. }
  147. #region 画边框
  148. [DllImport("user32.dll", EntryPoint = "SendMessageA")]
  149. private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, object lParam);
  150. [DllImport("user32")]
  151. private static extern IntPtr GetWindowDC(IntPtr hWnd);
  152. [DllImport("user32")]
  153. private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  154. const int WM_ERASEBKGND = 0x14;
  155. const int WM_NC_PAINT = 0x85;
  156. const int WM_PAINT = 0xF;
  157. const int WM_PRINTCLIENT = 0x318;
  158. //边框颜色
  159. private Pen BorderPen = new Pen(System.Drawing.Color.FromArgb(23, 169, 254), 2);//23, 169, 254
  160. protected override void WndProc(ref System.Windows.Forms.Message m)
  161. {
  162. setCustomFormat();
  163. IntPtr hDC = IntPtr.Zero;
  164. Graphics gdc = null;
  165. switch (m.Msg)
  166. {
  167. //画背景色
  168. case WM_ERASEBKGND:
  169. gdc = Graphics.FromHdc(m.WParam);
  170. gdc.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, this.Width, this.Height));
  171. gdc.Dispose();
  172. break;
  173. //画边框
  174. case WM_PAINT:
  175. base.WndProc(ref m);
  176. hDC = GetWindowDC(m.HWnd);
  177. gdc = Graphics.FromHdc(hDC);
  178. OverrideControlBorder(gdc);
  179. ReleaseDC(m.HWnd, hDC);
  180. gdc.Dispose();
  181. break;
  182. default:
  183. base.WndProc(ref m);
  184. break;
  185. }
  186. }
  187. private void OverrideControlBorder(Graphics g)
  188. {
  189. g.DrawRectangle(BorderPen, new Rectangle(0, 0, this.Width, this.Height));
  190. }
  191. #endregion
  192. }
  193. }