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

色等,当然我们可以使用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 }
}

C#自定义控件(3)—PanelHead控件的更多相关文章

  1. C# 自定义控件VS用户控件

    1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Con ...

  2. Android开发技巧——自定义控件之组合控件

    Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...

  3. Android自定义控件之日历控件

      标签: android 控件 日历 应用 需求 2015年09月26日 22:21:54 25062人阅读 评论(109) 收藏 举报 分类: Android自定义控件系列(7) 版权声明:转载注 ...

  4. 背水一战 Windows 10 (79) - 自定义控件: Layout 系统, 控件模板, 事件处理

    [源码下载] 背水一战 Windows 10 (79) - 自定义控件: Layout 系统, 控件模板, 事件处理 作者:webabcd 介绍背水一战 Windows 10 之 控件(自定义控件) ...

  5. winform 自定义控件:半透明Loading控件

    winform  自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...

  6. 自定义控件VS用户控件

    自定义控件VS用户控件 2015-06-16 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container ...

  7. Flutter学习笔记(38)--自定义控件之组合控件

    如需转载,请注明出处:Flutter学习笔记(38)--自定义控件之组合控件 在开始之前想先写点其他的,emm...就是今天在学习到自定义控件的时候,由于自定义控件这块一直是我的短板,无论是Andro ...

  8. Android自定义控件1--自定义控件介绍

    Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...

  9. 自定义控件和XControl控件

    (1)LabVIEW的自定义控件,实际上就是对LabVIEW自带的控件的一种修改,但是这种修改只能改变它的外观,即大小.颜色.位置等等,但是功能是改变不了的.如你对一个按钮进行自定义控件,无论怎么改, ...

随机推荐

  1. Java Web中MVC设计模式与IOC

    MVC是由Model(模型).View(视图).Controller(控制器)三个模块组成 视图:用于做数据展示以及和用户交互的一个界面(html页面) 控制层:能够接受客户端的请求,具体的业务功能还 ...

  2. KingbaseFlySync 评估工具的使用

    关键字: KingbaseFlySync.Linux.x86_64.mips64el.aarch64.Java **** 评估工具的使用**** 1.查询评估工具所在服务器的硬件平台(x86_64.m ...

  3. ELK套件部署

    前言 经过两周的不断碰壁,版本的选择 最终选择ELK的7.6.1套餐 因为我所需要的的警报插件sentinl也才跟新到7.6.1 运行环境:centos7 需要开放的端口:5601,9200,514( ...

  4. 利用高级组策略管理AGPM复制组策略GPO

    有时候管理多个林,在一个林中配置了GPO之后,想复制出来用到其它林里.默认系统的组策略管理里没有这个功能.但是微软在微软企业桌面优化套件Microsoft Desktop Optimization P ...

  5. k8s上安装安装 Ingress Controller &卸载

    在 master 节点上执行 nginx-ingress.yaml文件内容 # 如果打算用于生产环境,请参考 https://github.com/nginxinc/kubernetes-ingres ...

  6. 解决inode满

    登陆服务器运行df -i 然后运行 for i in /*; do echo $i; find $i |wc -l|sort -nr; done 看看每个文件夹下面的数量 最后发现是/var/spoo ...

  7. C++自学笔记 Composition:对象组合

    继承是实现软件重用的一种方式. 在C++中拥有另一种实现软件重用的方式----- Composition:对象组合 用已经有的对象制造新的对象 (设计一个类的时候它的成员变量可以是另一个类的对象) 对 ...

  8. python流程控制下-for、while循环补充

    循环结构之for循环 实现循环结构还可以用关键字for. for关键字 我们来看这一段代码: emotions = ['smile', 'laugh', 'cry', 'angry'] for emo ...

  9. HDU2844 Coins(多重背包)

    多重背包就是每种物品有数量限制时求解最大价值. 如果一种物品数量和重量之积超过背包容量,可视为完全背包:其余情况通过二进制拆分,将几个数量的物品看成一个,转化为01背包求解. 按照这种思路代码是这样的 ...

  10. C语言中的位域的使用

    转载:http://blog.sina.com.cn/s/blog_648d306d0100mv1c.html C语言中的位域的使用一.位域 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几 ...