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

色等,当然我们可以使用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】05---排序算法总结

    前言 数据 data 结构(structure)是一门 研究组织数据方式的学科,有了编程语言也就有了数据结构.学好数据结构才可以编写出更加漂亮,更加有效率的代码. 要学习好数据结构就要多多考虑如何将生 ...

  2. 关于KingbaseES临时文件过大问题

    背景 前几天碰到一个问题,现场并发很高,数据量很大(6T),主备库经常出现临时文件过多. 临时文件概念: 当私有进程buffer不够用时,会将buffer内容dump到tempfile供数据库正常sq ...

  3. 小结event.target与this

    <!DOCTYPE html> <html> <head>     <meta charset="UTF-8">     <t ...

  4. 硬核解析MySQL的MVCC实现原理,面试官看了都直呼内行

    1. 什么是MVCC MVCC全称是Multi-Version Concurrency Control(多版本并发控制),是一种并发控制的方法,通过维护一个数据的多个版本,减少读写操作的冲突. 如果没 ...

  5. 【读书笔记】C#高级编程 第八章 委托、lambda表达式和事件

    (一)引用方法 委托是寻址方法的.NET版本.委托是类型安全的类,它定义了返回类型和参数的类型.委托不仅包含对方法的引用,也可以包含对多个方法的引用. Lambda表达式与委托直接相关.当参数是委托类 ...

  6. @EqualsAndHashCode(callSuper = false) 解释

    当我们的pojo使用@Data注解时,@Data默认包含的是:@EqualsAndHashCode(callSuper = false),但是我们的pojo有继承父类,我们可能需要重新定义这个注解为: ...

  7. Django ORM 事务和查询优化

    一.事务操作 模块 from django.db import transaction 1 开启事务:with transaction.atomic() from django.db import t ...

  8. 【项目实战】CNN手写识别

    由于只需要修改之前基于ANN模型代码的模型设计部分所以篇幅较短,简单的加点注释给自己查看即可 视频链接:https://www.bilibili.com/video/BV1Y7411d7Ys?p=10 ...

  9. 8. Ceph 基础篇 - 运维常用操作

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247485300&idx=1&sn=aacff9f7 ...

  10. Python微服务实战

    文档地址:https://www.qikqiak.com/tdd-book/ GitHub地址:https://github.com/cnych/flask-microservices-users 源 ...