SurPanel.cs 4.9 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
{
    public partial class SurPanel : BasePnl
    {
        private bool _showTitle = true;
        private int _titleHeight = 24;
        private HorizontalAlignment _textAlign = HorizontalAlignment.Center;
        private Rectangle _titleRect = new Rectangle();
        private RectangleF _textRect = new RectangleF();
        private Font _titleFont = new Font("宋体", 12f);

        public SurPanel()
        {
            InitializeComponent();
        }


        [Browsable(false)]
        public override Font Font { get => base.Font; set => base.Font = value; }

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

        [Browsable(true)]
        public Font TitleFont
        {
            get => _titleFont;
            set
            {
                _titleFont = value;
                CalcSize();
                Refresh();
            }
        }

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

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





        protected override void CalcSize()
        {
            base.CalcSize();
            _titleRect = new Rectangle(0, 0, Width, _titleHeight + BorderWidth);
            if (_titleHeight == 0) return;

            SizeF sf = CreateGraphics().MeasureString(Text, _titleFont);
            _textRect = new RectangleF(0, (_titleRect.Height - sf.Height) / 2f, sf.Width, sf.Height);
            switch (TextAlign)
            {
                case HorizontalAlignment.Left:
                    _textRect.X = BorderWidth;
                    break;
                case HorizontalAlignment.Center:
                    _textRect.X = (Width - sf.Width) / 2f;
                    break;
                case HorizontalAlignment.Right:
                    _textRect.X = Width - BorderWidth - sf.Width;
                    break;
            }



        }

        private void SurPanel_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)
            {
                if (BorderWidth > 0)
                    g.DrawRectangle(BorderInsidePen, BorderRect);
            }
            else
            {
                if (BorderWidth > 0)
                    g.DrawRectangle(BorderOutsidePen, BorderRect);
            }

            if (_showTitle && _titleHeight > 0)
            {
                g.FillRectangle(Common.BLUE_BRUSH, _titleRect);
                g.DrawString(Text, _titleFont, Common.FORE_BRUSH, _textRect);
            }

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



    }
}