SurButtonOKCancel.cs 5.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.Face
{
    /// <summary>
    /// OK/Cancel两个按钮
    /// </summary>
    [DefaultProperty("ButtonIndex")]
    [DefaultEvent("Click")]
    public partial class SurButtonOKCancel : BaseCtl
    {
        private int _down = -1;
        private int _over = -1;
        private RectangleF[] _textRect = new RectangleF[2];
        private Rectangle[] _btnRect = new Rectangle[2];
        private const int SPACE = 6;
        private static readonly string[] TEXT = new string[] { "OK", "Cancel" };

        /// <summary>
        /// OK/Cancel两个按钮
        /// </summary>
        public SurButtonOKCancel()
        {
            InitializeComponent();
        }


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


        //==========属性==========
        /// <summary>
        /// 按钮的Index索引
        /// </summary>
        [Browsable(false), Category(""), Description("按钮的Index索引")]
        public int ButtonIndex { set; get; } = -1;


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

            int w = (Width - SPACE) / 2;
            _btnRect[0] = new Rectangle(BorderWidth / 2, BorderWidth / 2, w - BorderWidth, Height - BorderWidth);
            _btnRect[1] = new Rectangle(w + SPACE, BorderWidth / 2, w - BorderWidth, Height - BorderWidth);

            Graphics g = CreateGraphics();
            SizeF sf = g.MeasureString(TEXT[0], Font);
            _textRect[0] = new RectangleF((w - sf.Width) / 2f, (Height - sf.Height) / 2f, sf.Width, sf.Height);
            sf = g.MeasureString(TEXT[1], Font);
            _textRect[1] = new RectangleF(w + (w - sf.Width) / 2f + SPACE, (Height - sf.Height) / 2f, sf.Width, sf.Height);
        }

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

        private void SurButton_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 SurButton_MouseUp(object sender, MouseEventArgs e)
        {
            if (!Enabled) return;
            if (e.Button == MouseButtons.Left)
            {
                if (_over > -1 && _down > -1)
                {
                    ButtonIndex = _over;
                    _down = -1;
                    Refresh();
                    Click?.Invoke(this);
                    ButtonIndex = -1;
                }
            }
        }

        private void SurButton_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 (Enabled)
            {
                if (!Inside) _over = -1;
                for (int i = 0; i < _btnRect.Length; i++)
                {
                    if (i == _down)
                    {
                        g.FillRectangle(Common.BLUE_BRUSH, _btnRect[i]);
                    }
                    else if (i == _over)
                    {
                        g.FillRectangle(Common.OVER_BRUSH, _btnRect[i]);
                        if (BorderWidth > 0)
                            g.DrawRectangle(BorderInsidePen, _btnRect[i].X, _btnRect[i].Y, _btnRect[i].Width, _btnRect[i].Height);
                    }
                    else
                    {
                        g.DrawRectangle(BorderOutsidePen, _btnRect[i].X, _btnRect[i].Y, _btnRect[i].Width, _btnRect[i].Height);
                    }
                    g.DrawString(TEXT[i], Font, Common.FORE_BRUSH, _textRect[i]);
                }
                
            }
            else
            {
                if (BorderWidth > 0)
                {
                    g.DrawRectangle(BorderOutsidePen, _btnRect[0].X, _btnRect[0].Y, _btnRect[0].Width, _btnRect[0].Height);
                    g.DrawRectangle(BorderOutsidePen, _btnRect[1].X, _btnRect[1].Y, _btnRect[1].Width, _btnRect[1].Height);
                }
                g.DrawString(TEXT[0], Font, Common.BORDER_BRUSH, _textRect[0]);
                g.DrawString(TEXT[1], Font, Common.BORDER_BRUSH, _textRect[1]);
            }

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



    }
}