官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

准备工作

这个比较简单,对label扩展,重绘划线即可

开始

添加一个类UCSplitLabel ,继承 Label

代码比较少

 // ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-10-09
//
// ***********************************************************************
// <copyright file="UCSplitLabel.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel; namespace HZH_Controls.Controls
{
/// <summary>
/// Class UCSplitLabel.
/// Implements the <see cref="System.Windows.Forms.Label" />
/// </summary>
/// <seealso cref="System.Windows.Forms.Label" />
public class UCSplitLabel : Label
{
/// <summary>
/// Gets or sets the text.
/// </summary>
/// <value>The text.</value>
[Localizable(true)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
ResetMaxSize();
}
}
/// <summary>
/// 获取或设置控件显示的文字的字体。
/// </summary>
/// <value>The font.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
[Localizable(true)]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
ResetMaxSize();
}
}
/// <summary>
/// 获取或设置大小,该大小是 <see cref="M:System.Windows.Forms.Control.GetPreferredSize(System.Drawing.Size)" /> 可以指定的下限。
/// </summary>
/// <value>The minimum size.</value>
[Localizable(true)]
public override Size MinimumSize
{
get
{
return base.MinimumSize;
}
set
{
base.MinimumSize = value;
ResetMaxSize();
}
} /// <summary>
/// 获取或设置大小,该大小是 <see cref="M:System.Windows.Forms.Control.GetPreferredSize(System.Drawing.Size)" /> 可以指定的上限。
/// </summary>
/// <value>The maximum size.</value>
[Localizable(true)]
public override Size MaximumSize
{
get
{
return base.MaximumSize;
}
set
{
base.MaximumSize = value;
ResetMaxSize();
}
}
/// <summary>
/// 获取或设置一个值,该值指示是否自动调整控件的大小以完整显示其内容。
/// </summary>
/// <value><c>true</c> if [automatic size]; otherwise, <c>false</c>.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
} /// <summary>
/// The line color
/// </summary>
private Color lineColor = LineColors.Light; /// <summary>
/// Gets or sets the color of the line.
/// </summary>
/// <value>The color of the line.</value>
public Color LineColor
{
get { return lineColor; }
set
{
lineColor = value;
Invalidate();
}
} /// <summary>
/// Resets the maximum size.
/// </summary>
private void ResetMaxSize()
{
using (var g = this.CreateGraphics())
{
var _width = Width;
var size = g.MeasureString(string.IsNullOrEmpty(Text) ? "A" : Text, Font);
if (MinimumSize.Height != (int)size.Height)
MinimumSize = new Size(base.MinimumSize.Width, (int)size.Height);
if (MaximumSize.Height != (int)size.Height)
MaximumSize = new Size(base.MaximumSize.Width, (int)size.Height);
this.Width = _width;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="UCSplitLabel"/> class.
/// </summary>
public UCSplitLabel()
: base()
{
if (ControlHelper.IsDesignMode())
{
Text = "分割线";
Font = new Font("微软雅黑", 8f);
}
this.AutoSize = false;
Padding = new Padding(, , , );
MinimumSize = new System.Drawing.Size(, );
PaddingChanged += UCSplitLabel_PaddingChanged;
this.Width = ;
} /// <summary>
/// Handles the PaddingChanged event of the UCSplitLabel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void UCSplitLabel_PaddingChanged(object sender, EventArgs e)
{
if (Padding.Left < )
{
Padding = new Padding(, Padding.Top, Padding.Right, Padding.Bottom);
}
} /// <summary>
/// Handles the <see cref="E:Paint" /> event.
/// </summary>
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh(); var size = g.MeasureString(Text, Font);
g.DrawLine(new Pen(new SolidBrush(lineColor)), new PointF(, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / ), new PointF(Padding.Left - , Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / ));
g.DrawLine(new Pen(new SolidBrush(lineColor)), new PointF(Padding.Left + size.Width + , Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / ), new PointF(Width - Padding.Right, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / )); }
}
}

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

(八十)c#Winform自定义控件-分割线标签-HZHControls的更多相关文章

  1. (八)c#Winform自定义控件-分割线

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. (十八)c#Winform自定义控件-提示框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (四十八)c#Winform自定义控件-下拉按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  4. (三十八)c#Winform自定义控件-圆形进度条-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  5. (二十八)c#Winform自定义控件-文本框(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (五十八)c#Winform自定义控件-管道阀门(工业)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  7. (六十八)c#Winform自定义控件-DEMO整理

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  8. (七十八)c#Winform自定义控件-倒影组件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  9. (八十一)c#Winform自定义控件-时间轴-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

随机推荐

  1. ubuntu安装过程中遇到的一些问题及解决办法。

    由于ubuntu与win10的双系统安装过程百度一下就有很多,在此不再赘述. (其实主要是忘记拍照片了,我一个菜鸡说得肯定也没有那些大佬们好,但网上确实也很多哈,加上有中文引导安装,问题不大.) 此篇 ...

  2. 人人学IoT 助学思维导图

    原来学IoT记录的学习笔记,学完之后,对考试和工作都有些帮助,特分享给大家 笔记分享链接 https://share.mindmanager.com/#publish/s6TqusKeSG6aflXL ...

  3. Python如何爬取实时变化的WebSocket数据

    一.前言 作为一名爬虫工程师,在工作中常常会遇到爬取实时数据的需求,比如体育赛事实时数据.股市实时数据或币圈实时变化的数据.如下图: Web 领域中,用于实现数据'实时'更新的手段有轮询和 WebSo ...

  4. Pandas学习(二)——双色球开奖数据分析

    学习笔记汇总 Pandas学习(一)–数据的导入 pandas学习(二)–双色球数据分析 pandas学习(三)–NAB球员薪资分析 pandas学习(四)–数据的归一化 pandas学习(五)–pa ...

  5. Linux下RocketMQ下载安装教程

    一.下载 1.官网下载:下载地址   2.百度网盘下载:下载地址 提取码:0g5a java开发工具下载地址及安装教程大全,点这里. 更多深度技术文章,在这里. 二.安装及启动 1.将zip文件上传到 ...

  6. CodeForces-Round524 A~D

    A. Petya and Origami time limit per test  1 second   memory limit per test  256 megabytes input stan ...

  7. WY 2019 人工智能数学基础视频教程

    ├─01.第一阶段:AI数学基石 │ ├─01.第一章:线性代数 │ │ │ ]AI Math_[第1章]线性代数v2 .pdf │ │ │ │ │ └─视频 │ │ 01_本章概述.mp4 │ │ ...

  8. Python学习笔记—自动化部署【新手必学】

      前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:唯恋殊雨   目录 pexpect fabric pexpect P ...

  9. java开发中常用的Liunx操作命令

    查看所有端口的占用情况 netstat -nultp 其中State值为LISTEN则表示已经被占用 查看某个端口的占用情况: netstat -anp |grep 端口号 在liunx中启动tomc ...

  10. Django 07

    目录 ORM查询优化 only与defer(单表) select_related与prefetch_related(跨表) choices参数 MTV与MVC模型 Ajax简介 前后端传输数据编码格式 ...