FlatPanel.cs 9.4 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Asa.Theme
{
    [DefaultValue("Text")]
    public partial class FlatPanel : Panel
    {
        private Rectangle _borderRect = new Rectangle();
        private Rectangle _titleRect = new Rectangle();
        private RectangleF _textRect = new RectangleF();
        private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;
        private List<FlatBase> _enter = new List<FlatBase>();
        private int _borderWidth;
        private int _titleHeight;

        /// <summary>
        /// 
        /// </summary>
        public FlatPanel()
        {
            InitializeComponent();

            MouseEnter += FlatPanel_MouseEnter;
            MouseLeave += FlatPanel_MouseLeave;
            MouseMove += FlatPanel_MouseMove;
            Paint += FlatPanel_Paint;
            Resize += FlatPanel_Resize;

            base.Text = "";
            _titleHeight = 24;
            _borderWidth = 2;
            Padding = new Padding(6, 6 + _titleHeight, 6, 6);
            CalcSize();
            Refresh();
        }


        /// <summary>
        /// 显示的文本
        /// </summary>
        [Browsable(true), Category(""), Description("显示的文本"), DefaultValue("")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get => base.Text;
            set
            {
                base.Text = value;
                CalcSize();
                Refresh();
            }
        }

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

        /// <summary>
        /// 显示的文本的对齐方式
        /// </summary>
        [Browsable(true), Category(""), Description("显示的文本的对齐方式"), DefaultValue(ContentAlignment.MiddleCenter)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ContentAlignment TextAlign
        {
            set
            {
                _textAlign = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _textAlign;
            }
        }

        /// <summary>
        /// 显示的标题的高度
        /// </summary>
        [Browsable(true), Category(""), Description("显示的标题的高度"), DefaultValue(24)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int TitleHeight
        {
            set
            {
                if (value < 0)
                    _titleHeight = 0;
                else
                    _titleHeight = value;
                Padding = new Padding(6, 6 + _titleHeight, 6, 6);
                CalcSize();
                Refresh();
            }
            get
            {
                return _titleHeight;
            }
        }

        /// <summary>
        /// 控件的边框的宽度
        /// </summary>
        [Browsable(true), Category(""), Description("控件的边框的宽度"), DefaultValue(2)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int BorderWidth
        {
            set
            {
                _borderWidth = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _borderWidth;
            }
        }





        [Browsable(false)]
        public override bool AutoScroll { get => base.AutoScroll; set => base.AutoScroll = value; }
        [Browsable(false)]
        public override bool AutoSize { get => base.AutoSize; set => base.AutoSize = value; }
        [Browsable(false)]
        public override Point AutoScrollOffset { get => base.AutoScrollOffset; set => base.AutoScrollOffset = value; }
        [Browsable(false)]
        public override bool AllowDrop { get => base.AllowDrop; set => base.AllowDrop = value; }
        [Browsable(false)]
        public override Color ForeColor { get => base.ForeColor; set => base.ForeColor = value; }
        [Browsable(false)]
        public override RightToLeft RightToLeft { get => base.RightToLeft; set => base.RightToLeft = value; }
        [Browsable(false)]
        public override Color BackColor { get => base.BackColor; set => base.BackColor = value; }
        [Browsable(false)]
        public override Image BackgroundImage { get => base.BackgroundImage; set => base.BackgroundImage = value; }
        [Browsable(false)]
        public override ImageLayout BackgroundImageLayout { get => base.BackgroundImageLayout; set => base.BackgroundImageLayout = value; }
        [Browsable(false)]
        public new Padding Padding { set => base.Padding = value; get => base.Padding; }





        /// <summary>
        /// 鼠标焦点在控件里面
        /// </summary>
        public bool Inside { set; get; }

        /// <summary>
        /// 焦点进入的控件
        /// </summary>
        /// <param name="flat"></param>
        public void FocusEnter(FlatBase flat)
        {
            _enter.Add(flat);
        }

        /// <summary>
        /// 焦点离开的控件
        /// </summary>
        /// <param name="flat"></param>
        public void FocusLeave(FlatBase flat)
        {
            _enter.Remove(flat);
        }



        private void CalcSize()
        {
            _borderRect = new Rectangle(_borderWidth / 2, _borderWidth / 2, Width - _borderWidth, Height - _borderWidth);
            _titleRect.Width = Width;
            _titleRect.Height = _titleHeight;

            if (_titleHeight == 0) return;
            SizeF sf = CreateGraphics().MeasureString(Text, Font);
            switch (TextAlign)
            {
                case ContentAlignment.TopLeft:
                    _textRect = new RectangleF(_borderWidth, _borderWidth, sf.Width, sf.Height);
                    break;
                case ContentAlignment.TopCenter:
                    _textRect = new RectangleF((Width - sf.Width) / 2f, _borderWidth, sf.Width, sf.Height);
                    break;
                case ContentAlignment.TopRight:
                    _textRect = new RectangleF(Width - _borderWidth - sf.Width, _borderWidth, sf.Width, sf.Height);
                    break;
                case ContentAlignment.MiddleLeft:
                    _textRect = new RectangleF(_borderWidth, (_titleHeight - sf.Height) / 2f, sf.Width, sf.Height);
                    break;
                case ContentAlignment.MiddleCenter:
                    _textRect = new RectangleF((Width - sf.Width) / 2f, (_titleHeight - sf.Height) / 2f, sf.Width, sf.Height);
                    break;
                case ContentAlignment.MiddleRight:
                    _textRect = new RectangleF(Width - _borderWidth - sf.Width, (_titleHeight - sf.Height) / 2f, sf.Width, sf.Height);
                    break;
                case ContentAlignment.BottomLeft:
                    _textRect = new RectangleF(_borderWidth, _titleHeight - _borderWidth - sf.Height, sf.Width, sf.Height);
                    break;
                case ContentAlignment.BottomCenter:
                    _textRect = new RectangleF((Width - sf.Width) / 2f, _titleHeight - _borderWidth - sf.Height, sf.Width, sf.Height);
                    break;
                case ContentAlignment.BottomRight:
                    _textRect = new RectangleF(Width - _borderWidth - sf.Width, _titleHeight - _borderWidth - sf.Height, sf.Width, sf.Height);
                    break;
            }
        }

        private void FlatPanel_Resize(object sender, EventArgs e)
        {
            CalcSize();
            Refresh();
        }

        private void FlatPanel_MouseLeave(object sender, EventArgs e)
        {
            System.Windows.Forms.Control ctl = this.Parent;

            Rectangle rect = new Rectangle(ctl.Left + this.Left, ctl.Top + this.Top, this.Width, this.Height);
            bool rtn = rect.Contains(MousePosition);

            if (!rtn)
            {
                Inside = false;
                Refresh();
            }
        }

        private void FlatPanel_MouseEnter(object sender, EventArgs e)
        {
            Inside = true;
            Refresh();
        }

        private void FlatPanel_MouseMove(object sender, MouseEventArgs e)
        {
            //if (_enter.Count > 0)
            //{
            //    for (int i = 0; i < _enter.Count; i++)
            //        _enter[i].MouseFocusLeave();
            //    _enter.Clear();
            //}
        }

        private void FlatPanel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(Common.BACK_COLOR);
            if (Inside)
                e.Graphics.DrawRectangle(Common.BLUE_PEN, _borderRect);
            else
                e.Graphics.DrawRectangle(Common.BORDER_PEN, _borderRect);

            if (_titleHeight > 0)
            {
                e.Graphics.FillRectangle(Common.BLUE_BRUSH, _titleRect);
                e.Graphics.DrawString(Text, Font, Common.FORE_BRUSH, _textRect);
            }
        }

    }
}