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

色等,当然我们可以使用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. React报错之Rendered more hooks than during the previous render

    正文从这开始~ 总览 当我们有条件地调用一个钩子或在所有钩子运行之前提前返回时,会产生"Rendered more hooks than during the previous render ...

  2. [SDR] GNU Radio 系列教程(二) —— 绘制第一个信号分析流程图

    目录 1.前言 2.启动 GNU Radio 3.新增块 4.运行 本文视频 参考链接 1.前言 本文将介绍如何在 GNU Radio 中创建和运行第一个流程图. 2.启动 GNU Radio GNU ...

  3. KingbaseESV8R6临时表和全局临时表

    临时表概述 临时表用于存放只存在于事务或会话期间的数据.临时表中的数据对会话是私有的,每个会话只能看到和修改自己会话的数据. 您可以创建全局(global)临时表或本地(locall)临时表. 下表列 ...

  4. KingbaseES 绑定变量与游标共享

    对于重复执行的SQL,需要使用绑定变量,避免SQL的重复解析.但是,并不是说使用了绑定变量,就一定能避免硬解析.具体可以参见:https://www.cnblogs.com/kingbase/p/16 ...

  5. git stash总结

    git stash 1. git stash save "message" ​ 执行存储,并添加备注信息(直接git stash 也可以,但没有备注信息) 2. git stash ...

  6. 阿里云服务器如何使用phpmailer发送邮件

    原因是因为阿里云把25端口给禁用了,所以,普通方式发送不了,解决办法就是在阿里云安全组中把465端口打开,然后再才能发送邮件.亲测成功,贴出引用代码 function emailTo($user,$c ...

  7. 【疑难杂症】关于Transformer到底是什么

    在学习transform的时候,很多视频上来就是一张图开始解释图里面残差网络,self-attention等等巴拉巴拉的意思,然后组装,看也看完了,但是还是不明白transformer和selfatt ...

  8. Elasticsearch 8.X 节点角色划分深入详解

    文章转载自: https://mp.weixin.qq.com/s/3486iH3VH7TV6lza-a7adQ 0.问题引出 如果你的 Elasticsearch 集群是 7.9 之前的版本,在配置 ...

  9. 实战---在Portainer中编排docker-compose.yml文件

    选择要部署容器的主机上,不用事先安装配置docker-compose 官方示例文档地址,2.0版本的:https://docs.docker.com/compose/compose-file/comp ...

  10. Git使用与心得体会

    Git使用与心得体会 一.闲聊 闲暇时间学一下Git,也算是不用在网页端操作github了 二.Git相关 集中式与分布式 Git是一个分布式的版本控制系统,而传统的SVN则属于集中式 集中式与分布式 ...