官网

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. hdu 2955 Robberies (01背包好题)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. django基础之day05,F与Q查询,Q查询的高级用法

    #F与Q查询 #*************************** F 查询 ******************** # F 查询数据库中的其他字段!!! #1.查询库存数大于卖出数的书籍 fr ...

  3. Android Studio 安装教程

    前言 前段时间周围有很多认识的人学习Android,看蛮多人在装Android Studio,然而看他们装的过程不是那么顺利?然后也有高中同学来问我,于是乎就自己也试着去装了下,也方便日后学习Andr ...

  4. Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例

    场景 Nginx入门简介和反向代理.负载均衡.动静分离理解 https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102790862 Ub ...

  5. C# ling to sql 左表连接

    var begin_daily = from a in _postgreDbContext.tab1 join b in _postgreDbContext.tab2 on a.id equals b ...

  6. 《Dotnet9》系列之建站-中文站最好WordPress主题,自媒体,博客,企业,商城主题一网打尽

    大家好,我是Dotnet9小编,一个从事dotnet开发8年+的程序员.本文介绍WordPress主题JustNews,本站Dotnet9既是使用WordPress + JustNews主题搭建而成的 ...

  7. HTTP_header安全选项(浅谈)

    HTTP报文头-安全问题 Mirror王宇阳 2019-10-01 参考:MDN技术文档:<http头安全相关的选项_by`myh0st> 认识HTTP协议 https://www.cnb ...

  8. SQL server 2014 安装

    一.规划并成功的完成SQLSERVER 2014的安装 1.规划系统 安装SQL server 前,第一步应该是合理的规划.以下是规划时的任务和要点: ① 当前工作负载的基准 ② 估计工作负载的增长情 ...

  9. centos7下安装客户端rabbitmq9.0

    下载目前最新rabbitmq客户端版本: wget https://github.com/alanxz/rabbitmq-c/archive/v0.9.0.tar.gz php扩展 : wget ht ...

  10. Server基本语句的用法

    1.创建数据库 create database databaseName use databaseName go   /*  转到指定数据库 */ 2.创建表 create table tableNa ...