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

namespace Asa.Theme
{
    public partial class FlatUpDown : FlatBase
    {

        private bool _upDown;  //鼠标按下
        private bool _downDown;  //鼠标按下
        private bool _upOver;  //鼠标经过
        private bool _downOver;  //鼠标经过
        private Rectangle _borderRect = new Rectangle();
        private Rectangle _upRect = new Rectangle();
        private Rectangle _downRect = new Rectangle();
        private Point[] _updownLine = new Point[4];
        private Point[] _upIcon;
        private Point[] _downIcon;

        private float _max;        //最大值
        private float _min;        //最小值
        private float _value;      //当前值
        private int _decimal;      //小数
        private float _increment;  //增减数量

        private const int BORDER_W = 2;
        private readonly int BORDER_HALF = BORDER_W / 2;

        public FlatUpDown()
        {
            InitializeComponent();
            Font = new Font("宋体", 9f);
            Padding = new Padding(3);
            _max = 100;
            _min = 0;
            _value = 0;
            _decimal = 0;
            _increment = 1;
            Txt.BackColor = Common.BACK_COLOR;
            Txt.ForeColor = Common.FORE_COLOR;
            CalcSize();
        }



        [Browsable(true), Category(""), Description("显示的文本的字体")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override Font Font
        {
            set
            {
                base.Font = value;
                Txt.Font = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return base.Font;
            }
        }

        [Browsable(true), Category(""), Description("控件内部间距")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public new Padding Padding
        {
            set
            {
                base.Padding = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return base.Padding;
            }
        }

        [Browsable(true), Category(""), Description("当前值"), DefaultValue(0)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public float Value
        {
            set
            {
                _value = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _value;
            }
        }

        [Browsable(true), Category(""), Description("最大值"), DefaultValue(100)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public float Maximum
        {
            set
            {
                _max = value;
                if (_value > _max)
                    Value = _max;
            }
            get
            {
                return _max;
            }
        }

        [Browsable(true), Category(""), Description("最小值"), DefaultValue(0)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public float Minimum
        {
            set
            {
                _min = value;
                if (_value < _min)
                    Value = _min;
            }
            get
            {
                return _min;
            }
        }

        [Browsable(true), Category(""), Description("小数位数"), DefaultValue(0)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int DecimalPlaces
        {
            set
            {
                if (value < 0)
                    _decimal = 0;
                else if (value > 10)
                    _decimal = 10;
                else
                    _decimal = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _decimal;
            }
        }

        [Browsable(true), Category(""), Description("每次单击按钮时增加或减少的数量"), DefaultValue(1)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public float Increment
        {
            set
            {
                _increment = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _increment;
            }
        }





        protected override void CalcSize()
        {
            Txt.Left = BORDER_W + Padding.Left;
            Txt.Width = Width - BORDER_W * 2 - Padding.Horizontal - Common.RBUTTON_W;
            Txt.Top = BORDER_W + (Height - Txt.Height) / 2;
            _borderRect = new Rectangle(BORDER_HALF, BORDER_HALF, Width - BORDER_W, Height - BORDER_W);

            _upRect.X = Width - BORDER_W - Common.RBUTTON_W;
            _upRect.Y = BORDER_W;
            _upRect.Width = Common.RBUTTON_W;
            _upRect.Height = (Height - BORDER_W * 3) / 2;
            _upIcon = Shape.Triangle(_upRect, 5, true);

            _downRect.X = _upRect.X;
            _downRect.Y = _upRect.Bottom + BORDER_W;
            _downRect.Width = _upRect.Width;
            _downRect.Height = _upRect.Height;
            _downIcon = Shape.Triangle(_downRect, 5, false);

            _updownLine[0] = new Point(_upRect.X - BORDER_HALF, _upRect.Y);
            _updownLine[1] = new Point(_upRect.X - BORDER_HALF, _downRect.Bottom + BORDER_HALF);
            _updownLine[2] = new Point(_upRect.X, _upRect.Bottom + BORDER_HALF);
            _updownLine[3] = new Point(_upRect.Right, _upRect.Bottom + BORDER_HALF);

            Txt.Text = string.Format("{0:F" + _decimal + "}", _value);
        }

        private void FlatTextBox_Paint(object sender, PaintEventArgs e)
        {
            //e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            //e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            //e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.Clear(Common.BACK_COLOR);

            
            if (Inside)
            {
                e.Graphics.DrawRectangle(Common.BLUE_PEN, _borderRect);
                e.Graphics.DrawLine(Common.BLUE_PEN, _updownLine[0], _updownLine[1]);
                e.Graphics.DrawLine(Common.BLUE_PEN, _updownLine[2], _updownLine[3]);

                if(_upDown)
                    e.Graphics.FillRectangle(Common.DOWN_BRUSH, _upRect);
                else if(_downDown)
                    e.Graphics.FillRectangle(Common.DOWN_BRUSH, _downRect);
                else if (_upOver)
                    e.Graphics.FillRectangle(Common.OVER_BRUSH, _upRect);
                else if (_downOver)
                    e.Graphics.FillRectangle(Common.OVER_BRUSH, _downRect);
            }
            else
            {
                e.Graphics.DrawRectangle(Common.BORDER_PEN, _borderRect);
            }

            e.Graphics.FillPolygon(Common.FORE_BRUSH, _upIcon);
            e.Graphics.FillPolygon(Common.FORE_BRUSH, _downIcon);

        }

        private void FlatTextBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (_upOver)
            {
                _upDown = true;
                _value += _increment;
                if (_value > _max) _value = _max;
                Txt.Text = string.Format("{0:F" + _decimal + "}", _value);
                Refresh();
            }
            else if (_downOver)
            {
                _downDown = true;
                _value -= _increment;
                if (_value < _min) _value = _min;
                Txt.Text = string.Format("{0:F" + _decimal + "}", _value);
                Refresh();
            }
        }

        private void FlatTextBox_MouseUp(object sender, MouseEventArgs e)
        {
            _upDown = false;
            _downDown = false;
            Refresh();
        }

        private void FlatTextBox_MouseMove(object sender, MouseEventArgs e)
        {
            bool bb = false;

            bool bln = _upRect.Contains(e.Location);
            if (bln != _upOver)
            {
                _upOver = bln;
                bb = true;
            }
            bln = _downRect.Contains(e.Location);
            if (bln != _downOver)
            {
                _downOver = bln;
                bb = true;
            }

            if (bb) Refresh();

        }

        private void Txt_MouseEnter(object sender, EventArgs e)
        {
            //MouseFocus(true);
        }

        private void Txt_TextChanged(object sender, EventArgs e)
        {
            //if (Text == Txt.Text) return;
            //Text = Txt.Text;
        }

        private void Txt_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void Txt_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void Txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (float.TryParse(Txt.Text, out float result))
            {
                if (result < _min)
                    _value = _min;
                else if (result > _max)
                    _value = _max;
                else
                    _value = result;

                Txt.Text = string.Format("{0:F" + _decimal + "}", _value);
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}