C#自定义控件(3)—PanelHead控件

有时候我们会需要这样一种控件效果,上面是标题,下面是另外一个区域,且分别需要设置不同的颜

色等,当然我们可以使用splitContainer控件来制作,也可以直接使用自定义控件来,这样可以减少一

定的麻烦。添加一个组件并继承Panel类,对Panel进行扩展。

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JSControl
{
    public partial class PanelHead : Panel
    {
        //设置字体格式使用
        private StringFormat sf=new StringFormat();
            
        public PanelHead()
        {
            InitializeComponent();
            this.sf.Alignment = StringAlignment.Center;//文字水平居中
            this.sf.LineAlignment = StringAlignment.Center;//文字垂直居中
            //设置控件样式
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
        }

        public PanelHead(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            this.sf.Alignment = StringAlignment.Center;//文字水平居中
            this.sf.LineAlignment = StringAlignment.Center;//文字垂直居中
            //设置控件样式
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
        }


        private Graphics graphics;

        #region Filed
        private Font headFont = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("标题字体")]
        public Font HeadFont
        {
            get { return headFont; }
            set { headFont = value; this.Invalidate(); }
        }

        private string headTitle = "PanelHead";
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("标题")]
        public string HeadTitle
        {
            get { return headTitle; }
            set { headTitle = value; this.Invalidate(); }
        }

        private int headHeight = 30;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("标题高度")]
        public int HeadHeight
        {
            get { return headHeight; }
            set { headHeight = value; this.Invalidate(); }
        }

        private Color headForeColor = Color.White;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("标题字体颜色")]
        public Color HeadForeColor
        {
            get { return headForeColor; }
            set { headForeColor = value; this.Invalidate(); }
        }

        private Color headBackColor = Color.LimeGreen;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("标题栏背景色")]
        public Color HeadBackColor
        {
            get { return headBackColor; }
            set { headBackColor = value; this.Invalidate(); }
        }

        private Color borderColor = Color.Gray;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("标题边框颜色")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;

                this.Invalidate();
            }
        }
        #endregion

        #region
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            graphics = e.Graphics;
            //消除锯齿,高质量显示
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            //为了显示边框,所以需要减去1
            graphics.DrawRectangle(new Pen(this.borderColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            //为了显示边框,开始位置为(1,1)
            RectangleF rec = new RectangleF(1, 1, this.Width - 2, this.headHeight);
            graphics.FillRectangle(new SolidBrush(this.headBackColor), rec);
            //绘制文字
            graphics.DrawString(this.headTitle, this.headFont, new SolidBrush(this.headForeColor), rec, sf);
        }
        #endregion

    }
}

 

posted @ 2021-06-01 22:28  WellMandala  阅读(411)  评论(0编辑  收藏  举报