FlatLabel.cs 6.0 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;

namespace Asa.Theme
{
    /// <summary>
    /// 标签控件
    /// </summary>
    public partial class FlatLabel : FlatBase
    {
        private bool _backBlue = false;
        private int _borderWidth = 0;
        private Rectangle _borderRect = new Rectangle();
        private RectangleF _textRect = new RectangleF();
        private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;

        /// <summary>
        /// 标签控件
        /// </summary>
        public FlatLabel()
        {
            InitializeComponent();
            base.Font = new Font("宋体", 9f);
            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("显示的文本的对齐方式"), DefaultValue(ContentAlignment.MiddleCenter)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ContentAlignment TextAlign
        {
            set
            {
                _textAlign = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _textAlign;
            }
        }

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

        /// <summary>
        /// 蓝色的背景
        /// </summary>
        [Browsable(true), Category(""), Description("蓝色的背景"), DefaultValue(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool BackBlue
        {
            get => _backBlue;
            set
            {
                _backBlue = value;
                CalcSize();
                Refresh();
            }
        }

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






        /// <summary>
        /// 计算大小
        /// </summary>
        protected override void CalcSize()
        {
            _borderRect = new Rectangle(_borderWidth / 2, _borderWidth / 2, Width - _borderWidth, Height - _borderWidth);
            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, (Height - sf.Height) / 2f, sf.Width, sf.Height);
                    break;
                case ContentAlignment.MiddleCenter:
                    _textRect = new RectangleF((Width - sf.Width) / 2f, (Height - sf.Height) / 2f, sf.Width, sf.Height);
                    break;
                case ContentAlignment.MiddleRight:
                    _textRect = new RectangleF(Width - _borderWidth - sf.Width, (Height - sf.Height) / 2f, sf.Width, sf.Height);
                    break;
                case ContentAlignment.BottomLeft:
                    _textRect = new RectangleF(_borderWidth, Height - _borderWidth - sf.Height, sf.Width, sf.Height);
                    break;
                case ContentAlignment.BottomCenter:
                    _textRect = new RectangleF((Width - sf.Width) / 2f, Height - _borderWidth - sf.Height, sf.Width, sf.Height);
                    break;
                case ContentAlignment.BottomRight:
                    _textRect = new RectangleF(Width - _borderWidth - sf.Width, Height - _borderWidth - sf.Height, sf.Width, sf.Height);
                    break;
            }
        }

        private void FlatLabel_Paint(object sender, PaintEventArgs e)
        {
            if (BackBlue)
                e.Graphics.Clear(Common.BLUE_COLOR);
            else
                e.Graphics.Clear(Common.BACK_COLOR);

            if (_borderWidth > 0)
            {
                if (Inside)
                    e.Graphics.DrawRectangle(new Pen(Common.BLUE_COLOR, _borderWidth), _borderRect);
                else
                    e.Graphics.DrawRectangle(new Pen(Common.BORDER_COLOR, _borderWidth), _borderRect);
            }

            e.Graphics.DrawString(Text, Font, Common.FORE_BRUSH, _textRect);
        }




    }
}