官网

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. 计算机等级考试真题1(JAVA)

    答案: 01-05 C D A A C   06-10 B/D    C C C B 11-15 A C A C A 16-20 C B     C    21-25 D D C D D 26-30 ...

  2. CURL命令学习二

    -a, --append 用于上传文件时,如果服务器上该文件不存在则创建,如果存在则追加到源文件. -K, --config <file> 指定从某个文件读取curl参数.如果指定-为文件 ...

  3. windows cmd 生成文件目录树

    一.背景 之前逛GitHub的时候看到有大佬在描述项目结构的时候使用了一种文件目录树的格式 │ └─student_information_management_system │ │ ├─build ...

  4. IDEA开发、测试、生产环境pom配置及使用

    pom文件 一般放在最下面,project里 <!--开发环境.测试环境.生产环境--> <!--生产环境--> <profiles> <profile> ...

  5. MySQL数据库~~~~初识、基础数据类型

    一 数据库初识 1.1 什么是数据库 数据库(DataBase,简称DB),简而言之可视为电子化的文件柜----存储电子文件的处所,用户可以对文件中的数据运行新增,截取,更新,删除等操作. 所谓数据库 ...

  6. 新手Linux之路之Deepin

    用了很久的Window,心血来潮想换个系统,于是就开始踩坑Linux之路. 系统为deepin 首先基本的 设置root密码 $:sudo passwd root [sudo] password fo ...

  7. docker学习笔记---基本命令

    [root@docker ~]# docker Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Opt ...

  8. CodeForces - 5C(思维+括号匹配)

    题意 https://vjudge.net/problem/CodeForces-5C 给出一个括号序列,求出最长合法子串和它的数量. 合法的定义:这个序列中左右括号匹配. 思路 这个题和普通的括号匹 ...

  9. javascript中常见的几种循环遍历

    项目开发中,不管是建立在哪个框架基础上,对数据的处理都是必须的,而处理数据离不开各种遍历循环.javascript中循环遍历有很多种方式,记录下几种常见的js循环遍历. 一.for循环 for循环应该 ...

  10. JavaWeb 错误/异常时页面提示

    经常我们会遇到发生页面404错误,服务器 500 异常,如果默认方式处理,则是将异常捕获之后跳到 Tomcat 缺省的异常页面,如下图所示.