(三十六)c#Winform自定义控件-步骤控件-HZHControls
官网
前提
入行已经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的更多相关文章
- (三十三)c#Winform自定义控件-日期控件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (七十七)c#Winform自定义控件-采样控件-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (十二)c#Winform自定义控件-分页控件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- 程序员编程艺术第三十六~三十七章、搜索智能提示suggestion,附近点搜索
第三十六~三十七章.搜索智能提示suggestion,附近地点搜索 作者:July.致谢:caopengcs.胡果果.时间:二零一三年九月七日. 题记 写博的近三年,整理了太多太多的笔试面试题,如微软 ...
- 第三十六个知识点:Index Calculus算法
第三十六个知识点:Index Calculus算法 我们这篇博客继续描述一种数学攻击,这种数学攻击被叫做Index Calculus(IC)算法. 注意这里Index Calculus算法没有找到合适 ...
- NeHe OpenGL教程 第三十六课:从渲染到纹理
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Java进阶(三十六)深入理解Java的接口和抽象类
Java进阶(三十六)深入理解Java的接口和抽象类 前言 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太 ...
- Gradle 1.12用户指南翻译——第三十六章. Sonar Runner 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
随机推荐
- 运用Java调用Kettle Job和Trans(带参数)
1.首先创建一个kettle trans 2.对表输入进行编辑 ${dateNow}为要传入的参数. 3.代码编写 首先需要把包导入 import org.pentaho.di.core.Kettle ...
- 《Java基础知识》流程控制
流程控制分类: 一.顺序结构如果没有流程控制(即没有分支结构和循环结构),Java方法里面的语句是一个顺序执行流,从上到下依次执行每条语句. 二.分支结构1.if语句if语句使用布尔表达式或者布尔值作 ...
- var和let部分浅析
ES6中新增了let命令,用于声明变量,但所声明的变量只在let命令的代码块内有效. 举个例子: var a = []; for(var i=0;i<10;i++){ a[i] = functi ...
- 由malloc和new引发的段错误
class Queue{ private: struct node{ string data; struct node * next,*priv; } private: struct node * p ...
- Python实现的手机信息骚扰技术,切勿用作违法!
今天我来讲讲我最近找到的一个小思路,利用python进行短信轰炸 首先 这是一个网站,他的业务存在短信发送 这个时候,我们打开神器burp或者其他抓包工具(最好用burp,因为repeater模块可以 ...
- PHP 正则匹配h1的数据报错 preg_match(): Unknown modifier 'h' in
问题: $str = "<h1>this is test msg</h1>"; $ruler = "/^<h1>(.*?)</h ...
- Bash脚本编程之变量与多命令执行
变量基础知识 程序由指令加数据所组成,而变量可以理解为数据来源的一种. 变量名可以理解为指向了某个内存空间的地址,对于变量的赋值可理解为向内存空间写入数据,对于变量的引用可理解为从内存空间读取数据. ...
- Linux 实验 [Day 01]
目录 1. Linux 简介(略过) 2. Linux 基本概念及操作:命令.快捷键与通配符 2.1 基础命令 2.2 终端快捷键 2.3 通配符 2.4 帮助命令 3. 用户及文件权限管理 3.1 ...
- 2019/12/11学习内容摘要(Linux系统用户与用户组管理①)
一,认识/etc/passwd和/etc/shadow 1. /etc/passwd :文件中保存系统中所有的用户和用户的主要信息. 在命令行输入 cat /etc/passwd | head ( ...
- 通过requestAnimationFrame判断浏览器帧率
/** ** 得到浏览器每秒帧数fps ** ** @Date Mar 13 2013 **/ var showFPS = (function(){ var requestAnimationFrame ...