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

色等,当然我们可以使用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. AtCoder Beginner Contest 255(E-F)

    Aising Programming Contest 2022(AtCoder Beginner Contest 255) - AtCoder E - Lucky Numbers 题意: 给两个数组a ...

  2. Linux之主从数据库(1+X)

    主从数据库搭建 改主机名 配置网络 配置yum源(下载mysql) 写域名解析文件 主从同步:(备份,负载(读)) 第一步:数据库的初始化,修改配置文件,定义server-id(所有节点),开启二进制 ...

  3. 【loj2538】 【PKUWC 2018】Slay the Spire dp

    我们不难发现,假设抽了x张攻击牌,y张强化牌,那么肯定是打出尽可能多张的强化牌后,再开始出攻击牌(当然最少要一张攻击牌) 我们设G(i,j)表示:所有(抽到的攻击牌牌数为i,打出的攻击牌牌数为j)的方 ...

  4. 凭借SpringBoot整合Neo4j,我理清了《雷神》中错综复杂的人物关系

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. 虽然距离中秋放假还要熬过漫长的两天,不过也有个好消息,今天是<雷神4>上线Disney+流媒体的日子 ...

  5. elementUI中page(分页)的使用方法

    HTML部分 <!-- 快捷键 page-div --> <el-pagination background layout="sizes,prev, pager, next ...

  6. Redis可视化工具(支持ssh链接)

    1.Redis Desktop Manager RedisDesktopManager,简称RDM,这是一款很出名的Redis可视化管理工具,支持Windows,Mac,Ipad,LInux 开源地址 ...

  7. Pixar 故事公式

    文章转载自:https://mp.weixin.qq.com/s/wMfFVh9tAM5Qo4ED658yUg

  8. Query String Query和Sumple Query String

  9. 解决nexus仓库只能拉取不能推送的问题

    当时正在使用jenkins自动构造镜像推送到nexus上的docker镜像仓库,突然间就报错如下,没法推送,超过重试次数后也是没法推送: ERROR: Build step failed with e ...

  10. 安装 Ubuntu 教程

    1.选择中文安装 2.****到了如下界面,我们点击继续: 3.然后点击,现在安装: 4.****到了这界面,点击继续: 5.如下,输入你的位置,随便输入就好: 6.****然后选择汉语,点击继续: ...