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 @   WellMandala  阅读(449)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示