博客
关于我
(八十)c#Winform自定义控件-分割线标签-HZHControls
阅读量:410 次
发布时间:2019-03-06

本文共 2861 字,大约阅读时间需要 9 分钟。

NuGet包发布指南:自定义控件系列

作为一名开发者,经历了7年多的编程历程,我一直想打造一套独特而漂亮的自定义控件系列。于是,这一系列文章就诞生了。接下来,我将分享如何通过NuGet包轻松分发这些自定义控件的过程。


NuGet安装

安装该控件非常简单,只需执行以下命令即可:

Install-Package HZH_Controls

目录

  • 用处及效果
  • 准备工作
  • 开始
  • 代码实现
  • 最后的话

准备工作

对于Label控件的扩展,所需工作相对简单。主要任务是重绘划线即可。


开始

为了实现Split Label功能,我们首先创建一个继承自Label的类 UCSplitLabel

代码实现步骤如下:

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{    public class UCSplitLabel : Label    {        private Color lineColor = LineColors.Light;        public Color LineColor        {            get { return lineColor; }            set            {                lineColor = value;                Invalidate();            }        }        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;            }        }        public UCSplitLabel()        {            if (ControlHelper.IsDesignMode())            {                Text = "分割线";                Font = new Font("微软雅黑", 8f);            }            this.AutoSize = false;            Padding = new Padding(20, 0, 0, 0);            MinimumSize = new System.Drawing.Size(150, 10);            PaddingChanged += UCSplitLabel_PaddingChanged;            this.Width = 200;        }        private void UCSplitLabel_PaddingChanged(object sender, EventArgs e)        {            if (Padding.Left < 20)            {                Padding = new Padding(20, Padding.Top, Padding.Right, Padding.Bottom);            }        }        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(1, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2),                new PointF(Padding.Left - 2, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2));            g.DrawLine(new Pen(new SolidBrush(lineColor)),                 new PointF(Padding.Left + size.Width + 1, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2),                new PointF(Width - Padding.Right, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2));        }    }}

最后的话

如果你觉得这篇文章有价值,请支持一下,点击右上角的星号!欢迎在企鹅群 568015492 中来交流探讨。


希望这篇文章能为你的开发之路带来灵感!

转载地址:http://mdvkz.baihongyu.com/

你可能感兴趣的文章
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>
Ormlite数据库
查看>>
orm总结
查看>>
os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
查看>>
os.system 在 Python 中不起作用
查看>>
OSCACHE介绍
查看>>
SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
查看>>
OSChina 周五乱弹 ——吹牛扯淡的耽误你们学习进步了
查看>>