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

namespace CombBoxXX
{
    public partial class DatePickerCustom : UserControl
    {
        private Button btnNull;
        private Button btnOK;
        private MonthCalendar Calendar;
        private Form FrmCalendar;
        private string _dttext;
        private Panel panel;
        private NumericUpDown num1;
        private NumericUpDown num2;
        /// <summary>
        /// 供外部调用获取值
        /// </summary>
        public string DtText
        { get { return _dttext; } set { _dttext = value; } }

        private bool _isshowtime = false;
        /// <summary>
        /// 是否要提供时间
        /// </summary>
        public bool IsShowTime
        { get { return _isshowtime; } set { _isshowtime = value; } }

        public DatePickerCustom()
        {
            InitializeComponent();

            #region 添加个层
            panel = new Panel();
            panel.Size = new Size(120, 25);

            Label lbl1 = new Label();
            lbl1.Name = "";
            lbl1.Text = "时";
            lbl1.Width = 15;
            lbl1.Location = new Point(0, 5);

            num1 = new NumericUpDown();
            num1.Name = "numHour";
            num1.Size = new Size(35, num1.Height);
            num1.Maximum = 23;
            num1.Minimum = 0;
            num1.Location = new Point(lbl1.Width + 2, 2);

            Label lbl2 = new Label();
            lbl2.Name = "";
            lbl2.Text = "分";
            lbl2.Width = 15;
            lbl2.Location = new Point(num1.Location.X + num1.Width + 2, lbl1.Location.Y);

            num2 = new NumericUpDown();
            num2.Name = "numMinute";
            num2.Size = new Size(35, num2.Height);
            num2.Maximum = 59;
            num2.Minimum = 0;
            num2.Location = new Point(lbl2.Location.X + lbl2.Width + 2, num1.Location.Y);

            panel.Controls.Add(lbl1);
            panel.Controls.Add(num1);
            panel.Controls.Add(lbl2);
            panel.Controls.Add(num2);
            #endregion

            this.btnOK = new Button();
            this.btnOK.Text = "确定";

            this.Calendar = new MonthCalendar();
            this.Calendar.Visible = true;
            this.Calendar.ShowToday = false;
            this.Calendar.ShowTodayCircle = true;
            this.Calendar.MaxSelectionCount = 1;

            this.Calendar.DateSelected += Calendar_DateSelected;
            this.textBoxEx1.Leave += textBoxEx1_Leave;
            this.textBoxEx1.EventTextBoxEx_TextChanged += textBoxEx1_EventTextBoxEx_TextChanged;
            this.Resize += DatePickerCustom_Resize;
            this.btnOK.Click += btnOK_Click;
            
        }

        
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnOK_Click(object sender, EventArgs e)
        {
            Calendar_DateSelected(null, null);
        }
        /// <summary>
        /// 窗体大小发生变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DatePickerCustom_Resize(object sender, EventArgs e)
        {
            this.textBoxEx1.Width = this.Width;
            this.Height = this.textBoxEx1.Height;
            this.buttonExpand1.Size = new Size(this.textBoxEx1.Height - 2, this.textBoxEx1.Height - 2);
            this.buttonExpand1.Location = new Point(this.textBoxEx1.Width - this.buttonExpand1.Size.Width-1,1);
        }
        public new Font Font
        {
            get
            {

                return _Font;
            }
            set
            {
                _Font = value;
                this.NewFont = value;
            }
        }
        Font _Font = new Font("宋体", 10.5f);
        [Category("控件扩展属性"), Description("设置控件中文本字体")]
        public Font NewFont
        {
            get
            {

                return _Font;
            }
            set
            {
                _Font = value;
                this.textBoxEx1.Font = _Font;
                this.Height = this.textBoxEx1.Height;
                this.buttonExpand1.Size = new Size(this.textBoxEx1.Height - 2, this.textBoxEx1.Height - 2);
                this.buttonExpand1.Location = new Point(this.textBoxEx1.Width - this.buttonExpand1.Size.Width-1, 1);
                Invalidate();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void textBoxEx1_EventTextBoxEx_TextChanged(object sender, EventArgs e)
        {
            this.DtText = this.textBoxEx1.Text.Trim();
            if (this.txt_TextChanged != null)
                txt_TextChanged(sender, e);
        }

        #region 定义文本框值更改后触发事件
        //定义delegate
        public delegate void TextBox_TextChanged(object sender, EventArgs e);
        //用event 关键字声明事件对象
        /// <summary>
        /// 
        /// </summary>
        [Category("控件扩展事件"), Description("文本框值修改时")]
        public event TextBox_TextChanged txt_TextChanged;
        #endregion

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DatePickerCustom_Load(object sender, EventArgs e)
        {
            this.buttonExpand1.BackgroundImage = LYFZ.ComponentLibrary.GetUIResources.DateTime;
            
        }



        /// <summary>
        /// 点击弹出日期框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonExpand1_Click(object sender, EventArgs e)
        {
            if (this.FrmCalendar != null) {
                if (!this.FrmCalendar.Visible) {
                    this.HideWindos();
                    this.FrmCalendar.Visible = true;
                    return;
                }
                this.FrmCalendar.Visible = false;
            }
           
            this.btnNull = new Button();
            this.btnNull.Text = "空值";

            this.FrmCalendar = new Form();
            this.FrmCalendar.FormBorderStyle = FormBorderStyle.None;
            this.FrmCalendar.TopMost = true;
            this.FrmCalendar.Size = new Size(220, 205);
            this.FrmCalendar.Controls.Add(this.Calendar);
            this.FrmCalendar.Controls.Add(this.btnNull);
            this.FrmCalendar.BackColor = this.Calendar.BackColor;
            this.Calendar.Dock = DockStyle.Top;
            this.FrmCalendar.StartPosition = FormStartPosition.Manual;
            this.FrmCalendar.ShowInTaskbar = false;

            this.btnNull.Size = new Size(this.FrmCalendar.Width, 25);
            this.btnNull.Location = new Point(0, this.Calendar.Location.Y + this.Calendar.Height + 5);
            this.btnNull.Click += btnNull_Click;

            if (IsShowTime)
            {
                this.FrmCalendar.Controls.Add(this.btnOK);
                this.btnOK.Size = new Size(50, 25);
                this.btnOK.Location = new Point(120, this.Calendar.Location.Y + this.Calendar.Height + 5);
                this.btnNull.Size = new Size(50, 25);
                this.btnNull.Location = new Point(170, this.Calendar.Location.Y + this.Calendar.Height + 5);
                panel.Location = new Point(0, this.Calendar.Location.Y + this.Calendar.Height + 5);
                this.FrmCalendar.Controls.Add(panel);
            }
            else
            {
                this.btnNull.Size = new Size(this.FrmCalendar.Width, 25);
                this.btnNull.Location = new Point(0, this.Calendar.Location.Y + this.Calendar.Height + 5);
            }

            Point pos = this.Location;
            pos.Y = pos.Y + this.Height;
            pos = this.Parent.PointToScreen(pos);
            pos = GetPoint(pos, this.FrmCalendar);
            this.FrmCalendar.SetDesktopLocation(pos.X, pos.Y);
            this.FrmCalendar.Show();
            if (textBoxEx1.Text.ToString().Trim() != "")
            {
                Calendar.SelectionStart = Convert.ToDateTime(textBoxEx1.Text);
                if (IsShowTime)
                {
                    int hh = Convert.ToDateTime(textBoxEx1.Text).Hour;
                    int mm = Convert.ToDateTime(textBoxEx1.Text).Minute;
                    num1.Value = hh;
                    num2.Value = mm;
                }
            }
            this.textBoxEx1.Focus();
        }

        /// <summary>
        /// 文本框推动焦点时隐藏选择面板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void textBoxEx1_Leave(object sender, EventArgs e)
        {
            this.DtText = this.textBoxEx1.Text.Trim();
            if (this.txt_TextChanged != null)
                txt_TextChanged(sender, e);
            this.HideWindos();
        }
        /// <summary>
        /// 选择日期值隐藏当前控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Calendar_DateSelected(object sender, DateRangeEventArgs e)
        {
            string StrDate = this.Calendar.SelectionRange.Start.ToString("yyyy-MM-dd");
            if (IsShowTime)
            {
                string StrHour = "";
                string StrMinute = "";
                if (this.num1.Value.ToString().Trim().Length > 1)
                { StrHour = this.num1.Value.ToString().Trim(); }
                else { StrHour = "0" + this.num1.Value.ToString().Trim(); }
                if (this.num2.Value.ToString().Trim().Length > 1)
                { StrMinute = this.num2.Value.ToString().Trim(); }
                else { StrMinute = "0" + this.num2.Value.ToString().Trim(); }
                this.textBoxEx1.Text = StrDate + " " + StrHour + ":" + StrMinute;
            }
            this.textBoxEx1.Text = StrDate;
            DtText = textBoxEx1.Text;
            this.HideWindos();
        }
        /// <summary>
        /// 空值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnNull_Click(object sender, EventArgs e)
        {
            textBoxEx1.Text = "";
            this.Calendar.SelectionStart = this.Calendar.TodayDate;
            DtText = textBoxEx1.Text;
            this.HideWindos();
        }
        /// <summary>
        /// 取得指定控件全部显示时的顶点坐标
        /// </summary>
        /// <param name="sPos"></param>
        /// <param name="ConToShow"></param>
        /// <returns></returns>
        private Point GetPoint(Point sPos, Control ConToShow)
        {
            Point NewPos = new Point(sPos.X, sPos.Y);
            int iConWidth = ConToShow.Width;
            int iConHeight = ConToShow.Height;
            int iScrWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            int iScrHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            if (sPos.Y + iConHeight > iScrHeight)
            { NewPos.Y = iScrHeight - iConHeight; }
            if (sPos.X + iConWidth > iScrWidth)
            { NewPos.X = iScrWidth - iConWidth; }

            return NewPos;
        }

        /// <summary>
        /// 供外部调用赋值
        /// </summary>
        /// <param name="strText"></param>
        public void TextBoxGiveText(string strText)
        {
            DtText = strText;
            if (DtText != "")
            {
                if (IsShowTime)
                { DtText = Convert.ToDateTime(DtText).ToString("yyyy-MM-dd HH:mm").Trim(); }
                else { DtText = Convert.ToDateTime(DtText).ToString("yyyy-MM-dd").Trim(); }
                textBoxEx1.Text = DtText;
            }
        }
        /// <summary>
        /// 供外部调用,隐藏选择面板
        /// </summary>
        public void HideWindos()
        {
            this.FrmCalendar.Hide();
        }
    }
}