FaceDateTime.cs 11.5 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FaceControl
{
    public partial class FaceDateTime : ControlBase
    {
        //private bool itemDown = false;
        private int itemOverIndex = -1;
        private int itemSelected = -1;
        private List<GraphicsPath> items = new List<GraphicsPath>();
        private List<Size> itemGridSize = new List<Size>();
        private RectangleF itemGridRect;

        private bool _showYear = true;
        private bool _showMonth = true;
        private bool _showDay = true;
        private bool _showHour = true;
        private bool _showMinute = true;
        private bool _showSecond = true;

        private const int SPACE = 12;
        private const int ITEMS_HEIGHT = 45;
        private readonly Dictionary<string, Size> GRID_SIZE = new Dictionary<string, Size>
        {
            { "Year", new Size(5, 5) },
            { "Month", new Size(4, 3) },
            { "Day", new Size(7, 5) },
            { "Hour", new Size(6, 4) },
            { "Minute", new Size(10, 6) },
            { "Second", new Size(10, 6) }
        };

        public FaceDateTime()
        {
            InitializeComponent();
        }




        #region 属性
        [Browsable(true), Category("外观"), Description("是否显示日期的年"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ShowYear
        {
            get => _showYear;
            set
            {
                _showYear = value;
                CalcSize();
                Refresh();
            }
        }

        [Browsable(true), Category("外观"), Description("是否显示日期的月"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ShowMonth
        {
            get => _showMonth;
            set
            {
                _showMonth = value;
                CalcSize();
                Refresh();
            }
        }

        [Browsable(true), Category("外观"), Description("是否显示日期的日"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ShowDay
        {
            get => _showDay;
            set
            {
                _showDay = value;
                CalcSize();
                Refresh();
            }
        }

        [Browsable(true), Category("外观"), Description("是否显示时间的时"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ShowHour
        {
            get => _showHour;
            set
            {
                _showHour = value;
                CalcSize();
                Refresh();
            }
        }

        [Browsable(true), Category("外观"), Description("是否显示时间的分"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ShowMinute
        {
            get => _showMinute;
            set
            {
                _showMinute = value;
                CalcSize();
                Refresh();
            }
        }

        [Browsable(true), Category("外观"), Description("是否显示时间的秒"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ShowSecond
        {
            get => _showSecond;
            set
            {
                _showSecond = value;
                CalcSize();
                Refresh();
            }
        }
        #endregion


        protected override void CalcSize()
        {
            base.CalcSize();
            CalcShape();
            CalcItemLocation();
            CalcItemGridLocation();
        }

        protected override void DrawEnabled(Graphics g)
        {
            base.DrawEnabled(g);

            if (CursorInside)
            {
                for (int i = 0; i < items.Count; i++)
                {
                    if (i == itemSelected)
                    {
                        g.FillPath(new SolidBrush(theme.DOWN_COLOR), items[itemSelected]);
                        if (i != itemOverIndex)
                            g.DrawPath(new Pen(theme.OVER_COLOR, BorderWidth), items[i]);
                    }
                    else
                    {
                        if (i == itemOverIndex)
                        {
                            g.FillPath(new SolidBrush(theme.OVER_COLOR), items[itemOverIndex]);
                            g.DrawPath(new Pen(theme.DOWN_COLOR, BorderWidth), items[i]);
                        }
                        else
                        {
                            g.DrawPath(new Pen(theme.OVER_COLOR, BorderWidth), items[i]);
                        }
                    }
                }

                if (BorderWidth > 0)
                    g.DrawRectangle(borderInsidePen, borderRect.X, borderRect.Y, borderRect.Width, borderRect.Height);
            }
            else
            {
                itemOverIndex = -1;
                for (int i = 0; i < items.Count; i++)
                {
                    if (i == itemSelected)
                        g.FillPath(new SolidBrush(theme.DOWN_COLOR), items[itemSelected]);
                    g.DrawPath(new Pen(theme.OVER_COLOR, BorderWidth), items[i]);
                }
                if (BorderWidth > 0)
                    g.DrawRectangle(borderOutsidePen, borderRect.X, borderRect.Y, borderRect.Width, borderRect.Height);
            }

            g.DrawRectangle(borderInsidePen, itemGridRect.X, itemGridRect.Y, itemGridRect.Width, itemGridRect.Height);
        }

        protected override void DrawDisabled(Graphics g)
        {
            base.DrawDisabled(g);
            Region reg = new Region(new Rectangle(0, 0, Width, Height));
            reg.Exclude(borderRect);
            g.FillRegion(new SolidBrush(theme.BACK_COLOR), reg);
            //控件边框
            if (BorderWidth > 0)
                g.DrawRectangle(borderOutsidePen, borderRect.X, borderRect.Y, borderRect.Width, borderRect.Height);


        }

        private void CalcShape()
        {
            //borderRect = new GraphicsPath();
            //float n;

            //switch (FaceShape)
            //{
            //    case Model.ButtonShape.Rectangle:
            //        borderRect.AddRectangle(base.borderRect);
            //        break;
            //    case Model.ButtonShape.RoundRectangle:
            //        n = base.borderRect.Height / 8f;
            //        borderRect.AddLine(base.borderRect.X + n, base.borderRect.Y, base.borderRect.Right - n, base.borderRect.Y);
            //        borderRect.AddArc(base.borderRect.Right - n - n, base.borderRect.Y, n + n, n + n, 270, 90);
            //        borderRect.AddLine(base.borderRect.Right, base.borderRect.Y + n, base.borderRect.Right, base.borderRect.Bottom - n);
            //        borderRect.AddArc(base.borderRect.Right - n - n, base.borderRect.Bottom - n - n, n + n, n + n, 0, 90);
            //        borderRect.AddLine(base.borderRect.Right - n, base.borderRect.Bottom, base.borderRect.X + n, base.borderRect.Bottom);
            //        borderRect.AddArc(base.borderRect.X, base.borderRect.Bottom - n - n, n + n, n + n, 90, 90);
            //        borderRect.AddLine(base.borderRect.X, base.borderRect.Bottom - n, base.borderRect.X, base.borderRect.Y + n);
            //        borderRect.AddArc(base.borderRect.X, base.borderRect.Y, n + n, n + n, 180, 90);
            //        break;
            //    case Model.ButtonShape.Ellipse:
            //        borderRect.AddEllipse(base.borderRect);
            //        break;
            //    case Model.ButtonShape.EllipseRectangle:
            //        n = base.borderRect.Height / 2f;
            //        borderRect.AddLine(base.borderRect.X + n, base.borderRect.Y, base.borderRect.Right - n, base.borderRect.Y);
            //        borderRect.AddArc(base.borderRect.Right - n - n, base.borderRect.Y, n + n, n + n, 270, 180);
            //        borderRect.AddLine(base.borderRect.Right - n, base.borderRect.Bottom, base.borderRect.X + n, base.borderRect.Bottom);
            //        borderRect.AddArc(base.borderRect.X, base.borderRect.Y, n + n, n + n, 90, 180);
            //        break;
            //    case Model.ButtonShape.Circle:
            //        if (base.borderRect.Width > base.borderRect.Height)
            //            borderRect.AddEllipse((base.borderRect.Width - base.borderRect.Height) / 2f, base.borderRect.Y, base.borderRect.Height, base.borderRect.Height);
            //        else
            //            borderRect.AddEllipse(base.borderRect.X, (base.borderRect.Height - base.borderRect.Width) / 2f, base.borderRect.Width, base.borderRect.Width);
            //        break;
            //}
        }

        private void CalcItemLocation()
        {
            int itemCount = 0;
            itemGridSize.Clear();

            if (_showYear)
            {
                itemGridSize.Add(GRID_SIZE["Year"]);
                itemCount++;
            }
            if (_showMonth)
            {
                itemGridSize.Add(GRID_SIZE["Month"]);
                itemCount++;
            }
            if (_showDay)
            {
                itemGridSize.Add(GRID_SIZE["Day"]);
                itemCount++;
            }
            if (_showHour)
            {
                itemGridSize.Add(GRID_SIZE["Hour"]);
                itemCount++;
            }
            if (_showMinute)
            {
                itemGridSize.Add(GRID_SIZE["Minute"]);
                itemCount++;
            }
            if (_showSecond)
            {
                itemGridSize.Add(GRID_SIZE["Second"]);
                itemCount++;
            }

            float w = (borderRect.Width - SPACE * (itemCount + 1)) / itemCount;
            items.Clear();
            for (int i = 0; i < itemCount; i++)
            {
                GraphicsPath path = new GraphicsPath();
                path.AddRectangle(new RectangleF(borderRect.X + SPACE + (w + SPACE) * i, borderRect.Y + SPACE, w, ITEMS_HEIGHT));
                items.Add(path);
            }

            itemGridRect = new RectangleF(borderRect.X + SPACE, borderRect.Y + ITEMS_HEIGHT + SPACE * 2, borderRect.Width - SPACE * 2, borderRect.Bottom - SPACE * 3 - ITEMS_HEIGHT);

        }

        private void CalcItemGridLocation()
        {
        }

        private void FaceDateTime_MouseDown(object sender, MouseEventArgs e)
        {
            if (!Enabled) return;
            if (e.Button == MouseButtons.Left)
            {
                itemSelected = itemOverIndex;
                Refresh();
            }
        }

        private void FaceDateTime_MouseMove(object sender, MouseEventArgs e)
        {





            itemOverIndex = -1;
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].IsVisible(e.Location))
                {
                    itemOverIndex = i;
                    break;
                }
            }
            Refresh();
        }

        private void FaceDateTime_MouseUp(object sender, MouseEventArgs e)
        {
            if (!Enabled) return;
            if (e.Button == MouseButtons.Left)
            {
                //itemDown = false;
                Refresh();
                //Click?.Invoke(this, new EventArgs());
            }
        }
    }
}