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 }
}
C#自定义控件(3)—PanelHead控件的更多相关文章
- C# 自定义控件VS用户控件
1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Con ...
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- Android自定义控件之日历控件
标签: android 控件 日历 应用 需求 2015年09月26日 22:21:54 25062人阅读 评论(109) 收藏 举报 分类: Android自定义控件系列(7) 版权声明:转载注 ...
- 背水一战 Windows 10 (79) - 自定义控件: Layout 系统, 控件模板, 事件处理
[源码下载] 背水一战 Windows 10 (79) - 自定义控件: Layout 系统, 控件模板, 事件处理 作者:webabcd 介绍背水一战 Windows 10 之 控件(自定义控件) ...
- winform 自定义控件:半透明Loading控件
winform 自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...
- 自定义控件VS用户控件
自定义控件VS用户控件 2015-06-16 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container ...
- Flutter学习笔记(38)--自定义控件之组合控件
如需转载,请注明出处:Flutter学习笔记(38)--自定义控件之组合控件 在开始之前想先写点其他的,emm...就是今天在学习到自定义控件的时候,由于自定义控件这块一直是我的短板,无论是Andro ...
- Android自定义控件1--自定义控件介绍
Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...
- 自定义控件和XControl控件
(1)LabVIEW的自定义控件,实际上就是对LabVIEW自带的控件的一种修改,但是这种修改只能改变它的外观,即大小.颜色.位置等等,但是功能是改变不了的.如你对一个按钮进行自定义控件,无论怎么改, ...
随机推荐
- const修饰符总结
1.什么是const? const就是constant的缩写,意思是"恒定不变的",它是定义只读变量的关键字,或者说const是定义常变量的关键字,常类型的变量或对象的值是不能被更 ...
- Linux安装LibreCAD
目录 目录 添加软件源并更新软件列表 sudo add-apt-repository ppa:librecad-dev/librecad-daily sudo add-apt-repository p ...
- 如何不编写 YAML 管理 Kubernetes 应用?
Kubernetes 将自身边界内的事物都抽象为资源.其中的主要部分,是以 Deployment.StatefulSet 为代表的 workload 工作负载控制器,其他各类资源都围绕这些主要的资源工 ...
- 解决前端开发报错(SyntaxError: missing : after property id)的问题
当使用对象初始化语法创建对象的时候,需要使用半角冒号 (:) 将属性键与属性值隔开. 1 var obj = { propertyKey: 'value' }; 冒号与等号 下面的代码会运行失败,原因 ...
- webgl(three.js)实现室内三维定位,3D定位,3D楼宇bim、实时定位三维可视化解决方案——第十四课(定位升级版)
序: 还是要抽出时间看书的,迷上了豆豆的作品,最近在看<天幕红尘>,书中主人公的人生价值观以及修为都是让我惊为叹止.很想成为那样的人,但是再看看自己每天干的事,与时间的支配情况,真是十分的 ...
- 1.关于433MHz按键单片机解码
近段时间做项目要用到单片机接收433MHz按键发过来的码值,涉及短按.连按.长按,由于之前没有做过这方面一开始有点蒙,找遍网上都没有案例,现在项目完成了整理自己的一些心得和大家分享分享!!!直入主题. ...
- 洛谷P7112 行列式求值
行列式求值 这是一个让你掉头发的模板题 行列式的定义 行列式 (\(\texttt{Determinant}\)) 是一个函数定义,取值是一个标量. 对一个 \(n\times n\) 的矩阵 \(A ...
- 天天写SQL,这些神奇的特性你知道吗?
摘要:不要歪了,我这里说特性它不是 bug,而是故意设计的机制或语法,你有可能天天写语句或许还没发现原来还能这样用,没关系我们一起学下涨姿势. 本文分享自华为云社区<[云驻共创]天天写 SQL, ...
- SSTI服务端模板注入漏洞原理详解及利用姿势集锦
目录 基本概念 模板引擎 SSTI Jinja2 Python基础 漏洞原理 代码复现 Payload解析 常规绕过姿势 其他Payload 过滤关键字 过滤中括号 过滤下划线 过滤点.(适用于Fla ...
- 基于.NET6的简单三层管理系统
前言 笔者前段时间搬砖的时候,有了一个偷懒的想法:如果开发的时候,简单的CURD可以由代码生成器完成,相应的实体.服务都不需要再做额外的注册,这样开发人员可以省了很多事. 于是就开了这个项目,期望实现 ...