官网

http://www.hzhcontrols.com

前提

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

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

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

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

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

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">

目录

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

准备工作

也没什么可准备的了

开始

添加一个用户控件,命名UCStep

来点属性

  public event EventHandler IndexChecked;

         private Color m_stepBackColor = Color.FromArgb(, , );
/// <summary>
/// 步骤背景色
/// </summary>
[Description("步骤背景色"), Category("自定义")]
public Color StepBackColor
{
get { return m_stepBackColor; }
set { m_stepBackColor = value; }
} private Color m_stepForeColor = Color.FromArgb(, , );
/// <summary>
/// 步骤前景色
/// </summary>
[Description("步骤前景色"), Category("自定义")]
public Color StepForeColor
{
get { return m_stepForeColor; }
set { m_stepForeColor = value; }
} private Color m_stepFontColor = Color.White;
/// <summary>
/// 步骤文字颜色
/// </summary>
[Description("步骤文字景色"), Category("自定义")]
public Color StepFontColor
{
get { return m_stepFontColor; }
set { m_stepFontColor = value; }
} private int m_stepWidth = ;
/// <summary>
/// 步骤宽度
/// </summary>
[Description("步骤宽度景色"), Category("自定义")]
public int StepWidth
{
get { return m_stepWidth; }
set { m_stepWidth = value; }
} private string[] m_steps = new string[] { "step1", "step2", "step3" }; [Description("步骤"), Category("自定义")]
public string[] Steps
{
get { return m_steps; }
set
{
if (m_steps == null || m_steps.Length <= )
return;
m_steps = value;
Refresh();
}
} private int m_stepIndex = ; [Description("步骤位置"), Category("自定义")]
public int StepIndex
{
get { return m_stepIndex; }
set
{
if (m_stepIndex >= Steps.Length)
return;
m_stepIndex = value;
Refresh();
if (IndexChecked != null)
{
IndexChecked(this, null);
}
}
}

重绘

  protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality; if (m_steps != null && m_steps.Length > )
{
System.Drawing.SizeF sizeFirst = g.MeasureString(m_steps[], this.Font);
int y = (this.Height - m_stepWidth - - (int)sizeFirst.Height) / ;
if (y < )
y = ; int intTxtY = y + m_stepWidth + ;
int intLeft = ;
if (sizeFirst.Width > m_stepWidth)
{
intLeft = (int)(sizeFirst.Width - m_stepWidth) / + ;
} int intRight = ;
System.Drawing.SizeF sizeEnd = g.MeasureString(m_steps[m_steps.Length - ], this.Font);
if (sizeEnd.Width > m_stepWidth)
{
intRight = (int)(sizeEnd.Width - m_stepWidth) / + ;
} int intSplitWidth = ;
intSplitWidth = (this.Width - m_steps.Length - (m_steps.Length * m_stepWidth) - intRight) / (m_steps.Length - );
if (intSplitWidth < )
intSplitWidth = ; for (int i = ; i < m_steps.Length; i++)
{
#region 画圆,横线
g.FillEllipse(new SolidBrush(m_stepBackColor), new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth), y), new Size(m_stepWidth, m_stepWidth))); if (m_stepIndex > i)
{
g.FillEllipse(new SolidBrush(m_stepForeColor), new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth) + , y + ), new Size(m_stepWidth - , m_stepWidth - ))); if (i != m_steps.Length - )
{
if (m_stepIndex == i + )
{
g.DrawLine(new Pen(m_stepForeColor, ), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth, y + (m_stepWidth / )), new Point((i + ) * (m_stepWidth + intSplitWidth) - intSplitWidth / , y + (m_stepWidth / )));
g.DrawLine(new Pen(m_stepBackColor, ), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth + intSplitWidth / , y + (m_stepWidth / )), new Point((i + ) * (m_stepWidth + intSplitWidth), y + (m_stepWidth / )));
}
else
{
g.DrawLine(new Pen(m_stepForeColor, ), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth, y + (m_stepWidth / )), new Point((i + ) * (m_stepWidth + intSplitWidth), y + (m_stepWidth / )));
}
}
}
else
{
if (i != m_steps.Length - )
{
g.DrawLine(new Pen(m_stepBackColor, ), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth, y + (m_stepWidth / )), new Point((i + ) * (m_stepWidth + intSplitWidth), y + (m_stepWidth / )));
}
} System.Drawing.SizeF _numSize = g.MeasureString((i + ).ToString(), this.Font);
g.DrawString((i + ).ToString(), Font, new SolidBrush(m_stepFontColor), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - (int)_numSize.Width) / + , y + (m_stepWidth - (int)_numSize.Height) / + ));
#endregion System.Drawing.SizeF sizeTxt = g.MeasureString(m_steps[i], this.Font);
g.DrawString(m_steps[i], Font, new SolidBrush(m_stepIndex > i ? m_stepForeColor : m_stepBackColor), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - (int)sizeTxt.Width) / + , intTxtY));
}
} }

全部代码

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D; namespace HZH_Controls.Controls
{
public partial class UCStep : UserControl
{ [Description("步骤更改事件"), Category("自定义")]
public event EventHandler IndexChecked; private Color m_stepBackColor = Color.FromArgb(, , );
/// <summary>
/// 步骤背景色
/// </summary>
[Description("步骤背景色"), Category("自定义")]
public Color StepBackColor
{
get { return m_stepBackColor; }
set { m_stepBackColor = value; }
} private Color m_stepForeColor = Color.FromArgb(, , );
/// <summary>
/// 步骤前景色
/// </summary>
[Description("步骤前景色"), Category("自定义")]
public Color StepForeColor
{
get { return m_stepForeColor; }
set { m_stepForeColor = value; }
} private Color m_stepFontColor = Color.White;
/// <summary>
/// 步骤文字颜色
/// </summary>
[Description("步骤文字景色"), Category("自定义")]
public Color StepFontColor
{
get { return m_stepFontColor; }
set { m_stepFontColor = value; }
} private int m_stepWidth = ;
/// <summary>
/// 步骤宽度
/// </summary>
[Description("步骤宽度景色"), Category("自定义")]
public int StepWidth
{
get { return m_stepWidth; }
set { m_stepWidth = value; }
} private string[] m_steps = new string[] { "step1", "step2", "step3" }; [Description("步骤"), Category("自定义")]
public string[] Steps
{
get { return m_steps; }
set
{
if (m_steps == null || m_steps.Length <= )
return;
m_steps = value;
Refresh();
}
} private int m_stepIndex = ; [Description("步骤位置"), Category("自定义")]
public int StepIndex
{
get { return m_stepIndex; }
set
{
if (m_stepIndex >= Steps.Length)
return;
m_stepIndex = value;
Refresh();
if (IndexChecked != null)
{
IndexChecked(this, null);
}
}
} public UCStep()
{
InitializeComponent();
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);
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality; if (m_steps != null && m_steps.Length > )
{
System.Drawing.SizeF sizeFirst = g.MeasureString(m_steps[], this.Font);
int y = (this.Height - m_stepWidth - - (int)sizeFirst.Height) / ;
if (y < )
y = ; int intTxtY = y + m_stepWidth + ;
int intLeft = ;
if (sizeFirst.Width > m_stepWidth)
{
intLeft = (int)(sizeFirst.Width - m_stepWidth) / + ;
} int intRight = ;
System.Drawing.SizeF sizeEnd = g.MeasureString(m_steps[m_steps.Length - ], this.Font);
if (sizeEnd.Width > m_stepWidth)
{
intRight = (int)(sizeEnd.Width - m_stepWidth) / + ;
} int intSplitWidth = ;
intSplitWidth = (this.Width - m_steps.Length - (m_steps.Length * m_stepWidth) - intRight) / (m_steps.Length - );
if (intSplitWidth < )
intSplitWidth = ; for (int i = ; i < m_steps.Length; i++)
{
#region 画圆,横线
g.FillEllipse(new SolidBrush(m_stepBackColor), new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth), y), new Size(m_stepWidth, m_stepWidth))); if (m_stepIndex > i)
{
g.FillEllipse(new SolidBrush(m_stepForeColor), new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth) + , y + ), new Size(m_stepWidth - , m_stepWidth - ))); if (i != m_steps.Length - )
{
if (m_stepIndex == i + )
{
g.DrawLine(new Pen(m_stepForeColor, ), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth, y + (m_stepWidth / )), new Point((i + ) * (m_stepWidth + intSplitWidth) - intSplitWidth / , y + (m_stepWidth / )));
g.DrawLine(new Pen(m_stepBackColor, ), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth + intSplitWidth / , y + (m_stepWidth / )), new Point((i + ) * (m_stepWidth + intSplitWidth), y + (m_stepWidth / )));
}
else
{
g.DrawLine(new Pen(m_stepForeColor, ), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth, y + (m_stepWidth / )), new Point((i + ) * (m_stepWidth + intSplitWidth), y + (m_stepWidth / )));
}
}
}
else
{
if (i != m_steps.Length - )
{
g.DrawLine(new Pen(m_stepBackColor, ), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth, y + (m_stepWidth / )), new Point((i + ) * (m_stepWidth + intSplitWidth), y + (m_stepWidth / )));
}
} System.Drawing.SizeF _numSize = g.MeasureString((i + ).ToString(), this.Font);
g.DrawString((i + ).ToString(), Font, new SolidBrush(m_stepFontColor), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - (int)_numSize.Width) / + , y + (m_stepWidth - (int)_numSize.Height) / + ));
#endregion System.Drawing.SizeF sizeTxt = g.MeasureString(m_steps[i], this.Font);
g.DrawString(m_steps[i], Font, new SolidBrush(m_stepIndex > i ? m_stepForeColor : m_stepBackColor), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - (int)sizeTxt.Width) / + , intTxtY));
}
} }
}
}
 namespace HZH_Controls.Controls
{
partial class UCStep
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// UCStep
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.Transparent;
this.Name = "UCStep";
this.Size = new System.Drawing.Size(, );
this.ResumeLayout(false); } #endregion
}
}

用处及效果

最后的话

如果你喜欢的话,请到 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自定义控件-采样控件-HZHControls

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

  3. (三十)c#Winform自定义控件-文本框(三)

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

  4. (十二)c#Winform自定义控件-分页控件

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

  5. 程序员编程艺术第三十六~三十七章、搜索智能提示suggestion,附近点搜索

    第三十六~三十七章.搜索智能提示suggestion,附近地点搜索 作者:July.致谢:caopengcs.胡果果.时间:二零一三年九月七日. 题记 写博的近三年,整理了太多太多的笔试面试题,如微软 ...

  6. 第三十六个知识点:Index Calculus算法

    第三十六个知识点:Index Calculus算法 我们这篇博客继续描述一种数学攻击,这种数学攻击被叫做Index Calculus(IC)算法. 注意这里Index Calculus算法没有找到合适 ...

  7. NeHe OpenGL教程 第三十六课:从渲染到纹理

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  8. Java进阶(三十六)深入理解Java的接口和抽象类

    Java进阶(三十六)深入理解Java的接口和抽象类 前言 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太 ...

  9. Gradle 1.12用户指南翻译——第三十六章. Sonar Runner 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

随机推荐

  1. CCF-CSP题解 201809-4 再卖菜

    碎碎念..近视加老花,还以为第二天除了第二家范围在100以内别的都不确定,于是x**算的记搜复杂度超时了.还鼓捣着什么差分区间最长路,虽然有大神用差分区间做出来了,然而自己并没有看懂. 其实就是一个记 ...

  2. 2016/11/10 kettle概述

    ETL(Extract-Transform-Load,即抽取,转换,加载),数据仓库技术,是用来处理将数据从来源(以前做的项目)经过抽取,转换,加载到达目的端(正在做的项目)的过程.也就是新的项目需要 ...

  3. scrapy框架(二)

    scrapy框架(二) 一.scrapy 选择器 概述: Scrapy提供基于lxml库的解析机制,它们被称为选择器. 因为,它们“选择”由XPath或CSS表达式指定的HTML文档的某部分. Sca ...

  4. Python爬虫入门CentOS环境安装

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:梦想橡皮擦 CentOS环境安装-简介你好,当你打开这个文档的时候,我知 ...

  5. Linux Bash文本操作之grep篇

    Linux grep命令用于查找文件里符合条件的字符串.是文本检索中常用的工具之一. grep  指令在文件中查找能够匹配指定模式字符串的行.如果没有指定文件名,或者文件名为  -  ,则从标准输入设 ...

  6. SpringBoot微服务电商项目开发实战 --- Kafka集成接入

    kafka作为消息中间件的一款产品,她比较轻量级,在吞吐量方面很优秀,默认消息持久化到硬盘当中 168小时=7天,log.retention.hours=168,比较适合来做运营的统计.其他的不多讲, ...

  7. C# 中的栈和堆

    程序运行时,它的数据必须存储在内存中.一个数据项需要多大的内存.存储在内存中的什么位置.以及如何存储都依赖于该数据项的类型. 运行中的程序使用两个内存区域来存储数据:栈和堆. 栈 栈是一个内存数组,是 ...

  8. Reproduction CVE_2019_0708

    Xx_introduction Please protection,respect,love,"China's Internet Security Act"! For learni ...

  9. linux for games; steamos; fedora game distribution

    最近对linux 游戏发行版系统产生了兴趣,下面简要记录一些链接: https://itsfoss.com/linux-gaming-distributions/ (9 款游戏系统) https:// ...

  10. NGUI 源码分析- AnchorPoint

    AnchorPoint 是 UIRect 的一个内部类,此处规定作为基准的那个对象称为锚点对象,基准对象对应的矩形框称为目标框,当前对象对应的矩形框称为源框. public class AnchorP ...