123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.ComponentModel;
- using System.Runtime.InteropServices;
- namespace LYFZ.ComponentLibrary
- {
- public class DateTimePickerEx : System.Windows.Forms.DateTimePicker
- {
- public DateTimePickerEx()
- {
- this.Format = DateTimePickerFormat.Custom;
- this.CustomFormat = " ";
- this.ValueChanged += DateTimePickerEx_ValueChanged;
- this.KeyDown += DateTimePickerEx_KeyDown;
- this.CloseUp += DateTimePickerEx_CloseUp;
- }
- void DateTimePickerEx_CloseUp(object sender, EventArgs e)
- {
- DateTimePickerEx_ValueChanged(null, null);
- }
- void DateTimePickerEx_KeyDown(object sender, KeyEventArgs e)
- {
- if (e == null || e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
- {
- this.dateValue = "";
- setCustomFormat();
- }
- //throw new NotImplementedException();
- }
- void DateTimePickerEx_ValueChanged(object sender, EventArgs e)
- {
- if (this.Value > LYFZ.WinAPI.CustomPublicMethod.GetNullDateTime())
- {
- if (IsShowTime)
- {
- this.dateValue = this.Value.ToString(timeFormat);
- }
- else
- {
- this.dateValue = this.Value.ToString(dateFormat);
- }
- }
- else {
- this.dateValue = "";
- }
- setCustomFormat();
- }
- string timeFormat = "yyyy-MM-dd HH:mm";
- string dateFormat = "yyyy-MM-dd";
- bool isShowTime = false;
- /// <summary>
- ///
- /// </summary>
- [Category("控件扩展属性"), Description("是否显示时间")]
- public bool IsShowTime
- {
- get { return isShowTime; }
- set
- {
- isShowTime = value;
- setCustomFormat();
- Invalidate(false);
- }
- }
- void setCustomFormat()
- {
- if (IsEmpty)
- {
- this.CustomFormat = " ";
- }
- else
- {
- if (IsShowTime)
- {
- this.CustomFormat = timeFormat;
- }
- else
- {
- this.CustomFormat = dateFormat;
- }
- }
- }
- DateTime minimumDate = Convert.ToDateTime("1900-01-01");
- string dateValue = "";
- /// <summary>
- /// 获取日期字符串
- /// </summary>
- [Category("控件扩展属性"), Description("获取日期字符串")]
- public string DateValue
- {
- get { return dateValue; }
- set
- {
- dateValue = value;
- DateTime tempTime;
- if (DateTime.TryParse(dateValue, out tempTime) && tempTime > minimumDate)
- {
- try
- {
- this.Value = tempTime;
- }
- catch { }
- }
- else
- {
- // this.Value = DateTime.Now;
- this.dateValue = "";
- //DateTimePickerEx_KeyDown(null, null);
- }
- setCustomFormat();
- Invalidate(false);
- }
- }
- /// <summary>
- /// 获取日期时间字符串
- /// </summary>
- [Category("控件扩展属性"), Description("获取日期时间字符串")]
- public string DateTimeValue
- {
- get { return dateValue+" "+SDateTime.Now.ToString("HH:mm:ss"); }
- }
- /// <summary>
- ///
- /// </summary>
- [Category("控件扩展属性"), Description("是否为空")]
- public bool IsEmpty
- {
- get
- {
- bool bl = true;
- if (dateValue.Trim() == "")
- {
- bl = true;
- }
- else
- {
- bl = false;
- }
- return bl;
- }
- }
- #region 画边框
- [DllImport("user32.dll", EntryPoint = "SendMessageA")]
- private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, object lParam);
- [DllImport("user32")]
- private static extern IntPtr GetWindowDC(IntPtr hWnd);
- [DllImport("user32")]
- private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
- const int WM_ERASEBKGND = 0x14;
- const int WM_NC_PAINT = 0x85;
- const int WM_PAINT = 0xF;
- const int WM_PRINTCLIENT = 0x318;
- //边框颜色
- private Pen BorderPen = new Pen(System.Drawing.Color.FromArgb(23, 169, 254), 2);//23, 169, 254
- protected override void WndProc(ref System.Windows.Forms.Message m)
- {
- setCustomFormat();
- IntPtr hDC = IntPtr.Zero;
- Graphics gdc = null;
- switch (m.Msg)
- {
- //画背景色
- case WM_ERASEBKGND:
- gdc = Graphics.FromHdc(m.WParam);
- gdc.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, this.Width, this.Height));
- gdc.Dispose();
- break;
- //画边框
- case WM_PAINT:
- base.WndProc(ref m);
- hDC = GetWindowDC(m.HWnd);
- gdc = Graphics.FromHdc(hDC);
- OverrideControlBorder(gdc);
- ReleaseDC(m.HWnd, hDC);
- gdc.Dispose();
- break;
- default:
- base.WndProc(ref m);
- break;
- }
- }
- private void OverrideControlBorder(Graphics g)
- {
- g.DrawRectangle(BorderPen, new Rectangle(0, 0, this.Width, this.Height));
- }
- #endregion
- }
- }
|