SurMultiButton.cs 5.2 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.Face
{
    /// <summary>
    /// 多个按钮控件
    /// </summary>
    [DefaultProperty("Text")]
    [DefaultEvent("Click")]
    public partial class SurMultiButton : BaseCtl
    {
        private RectangleF[] _btnRect;
        private string[] _btnText;
        private RectangleF[] _btnTextRect;
        private int _over = -1;
        private int _down = -1;
        private const int SPACE = 3;

        /// <summary>
        /// 多个按钮控件
        /// </summary>
        public SurMultiButton()
        {
            InitializeComponent();
        }


        //==========事件==========
        /// <summary>
        /// 单击时发生
        /// </summary>
        [Browsable(true), Category(""), Description("单击时发生")]
        public new event MyEvent.Click Click;
        

        //==========属性==========
        /// <summary>
        /// 选中的按钮从零开始的索引
        /// </summary>
        [Browsable(false), Category(""), Description("选中的按钮从零开始的索引")]
        public int Index { get; private set; }


        //==========私有==========
        /// <summary>
        /// 计算位置大小
        /// </summary>
        protected override void CalcSize()
        {
            base.CalcSize();

            _btnText = Text.Split(',');
            int count = _btnText.Length;
            _btnRect = new RectangleF[count];
            _btnTextRect = new RectangleF[count];
            float w = Width - BorderWidth - BorderWidth;
            float h = Height - BorderWidth - BorderWidth;
            float ww = (w - SPACE * (count + 1)) / count;
            float hh = h - SPACE - SPACE;
            Graphics g = CreateGraphics();
            SizeF sf;

            for (int i = 0; i < count; i++)
            {
                _btnRect[i] = new RectangleF(BorderWidth + SPACE * (i + 1) + ww * i, BorderWidth + SPACE, ww, hh);
                sf = g.MeasureString(_btnText[i], Font);
                _btnTextRect[i] = new RectangleF(_btnRect[i].X + (_btnRect[i].Width - sf.Width) / 2, _btnRect[i].Y + (_btnRect[i].Height - sf.Height) / 2, sf.Width, sf.Height);
            }




        }

        private void SurChoose_Paint(object sender, PaintEventArgs e)
        {
            BufferedGraphicsContext current = BufferedGraphicsManager.Current;
            BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
            Graphics g = bg.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.Clear(Common.BACK_COLOR);  //背景

            //控件边框
            if (Inside && Enabled)
            {
                if (BorderWidth > 0)
                    g.DrawRectangle(BorderInsidePen, BorderRect);
            }
            else
            {
                _over = -1;  //避免移动太快没有执行MouseMove
                if (BorderWidth > 0)
                    g.DrawRectangle(BorderOutsidePen, BorderRect);
            }

            for (int i = 0; i < _btnRect.Length; i++)
            {
                if (Enabled)
                {
                    if (i == _down)
                        g.FillRectangle(Common.BLUE_BRUSH, _btnRect[i]);
                    else if (i == _over)
                        g.FillRectangle(Common.OVER_BRUSH, _btnRect[i]);
                    g.DrawString(_btnText[i], Font, Common.FORE_BRUSH, _btnTextRect[i]);
                }
                else
                {
                    g.DrawString(_btnText[i], Font, Common.BORDER_BRUSH, _btnTextRect[i]);
                }
            }

            bg.Render();
            bg.Dispose();
        }

        private void SurChoose_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Enabled) return;
            int move = -1;

            for (int i = 0; i < _btnRect.Length; i++)
            {
                if (_btnRect[i].Contains(e.Location))
                {
                    move = i;
                    break;
                }
            }

            if (_over != move)
            {
                _over = move;
                Refresh();
            }
        }

        private void SurChoose_MouseDown(object sender, MouseEventArgs e)
        {
            if (!Enabled) return;
            if (e.Button == MouseButtons.Left)
            {
                if (_over > -1)
                {
                    Index = _over;
                    _down = _over;
                    Refresh();
                }
            }
        }

        private void SurChoose_MouseUp(object sender, MouseEventArgs e)
        {
            if (!Enabled) return;
            if (e.Button == MouseButtons.Left)
            {
                if (_over > -1 && _down > -1)
                {
                    _down = -1;
                    Refresh();
                    Click?.Invoke(this);
                }
            }
        }


    }
}