现在的位置: 首页 > 综合 > 正文

扩展DateTimePicker使其可空 可设置背景

2017年12月07日 ⁄ 综合 ⁄ 共 2851字 ⁄ 字号 评论关闭

本文来自网络整理而成,不知道原作者是谁.如果有不适,请告之.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

 public partial class BackColorDateTimePicker : DateTimePicker
    {
        #region NULL
        private DateTimePickerFormat oldFormat = DateTimePickerFormat.Long;
        private string oldCustomFormat = null;
        private bool bIsNull = false;
        public new DateTime Value
        {
            get
            {
                if (bIsNull)
                    return DateTime.MinValue;
                else
                    return base.Value;
            }
            set
            {
                if (value == DateTime.MinValue)
                {
                    if (bIsNull == false)
                    {
                        oldFormat = this.Format;
                        oldCustomFormat = this.CustomFormat;
                        bIsNull = true;
                    }

                    this.Format = DateTimePickerFormat.Custom;
                    this.CustomFormat = " ";
                }
                else
                {
                    if (bIsNull)
                    {
                        this.Format = oldFormat;
                        this.CustomFormat = oldCustomFormat;
                        bIsNull = false;
                    }
                    base.Value = value;
                }
            }
        }

        protected override void OnCloseUp(EventArgs eventargs)
        {
            if (Control.MouseButtons == MouseButtons.None)
            {
                if (bIsNull)
                {
                    this.Format = oldFormat;
                    this.CustomFormat = oldCustomFormat;
                    bIsNull = false;
                }
            }
            base.OnCloseUp(eventargs);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.KeyCode == Keys.Delete)
                this.Value = DateTime.MinValue;
        }

        #endregion

        [Browsable(true)]
        public override Color BackColor
        {
            get { return base.BackColor; }
            set
            {
                base.BackColor = value;
                base.Invalidate();
            }
        }

        public BackColorDateTimePicker()
        {
            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case (int)0x0014:/* WM_ERASEBKGND */  //0x0014表示一个数字,在C++平台上面应该是叫WM_ERASEBKGND,这个消息主要用于绘制背景
                    base.WndProc(ref m);
                    Graphics g = Graphics.FromHdc(m.WParam);
                    g.FillRectangle(new SolidBrush(BackColor),
                    ClientRectangle);
                    g.Dispose();
                    break;
                default:
                    break;
            }
        }
    }

 

使用时:

 在WinForm的窗体加载事件时,添加:

   this.backColorDateTimePicker1.Value = DateTime.MinValue;

 

可在BackColor属性设置背景色

抱歉!评论已关闭.