官网

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. So Easy - 在Linux服务器上部署 .NET Core App

    .NET Core 是微软提供的免费.跨平台和开源的开发框架,可以构建桌面应用程序.移动端应用程序.网络应用程序.物联网应用程序和游戏应用程序等.如果你是 Windows 平台下的 dotnet 开发 ...

  2. 《Dotnet9》系列-开源C# Winform控件库强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  3. Docker常用命令-全

    原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接: https://app.yinxiang.com/shard/s17/nl/19391737/9f6bf39e- ...

  4. Blog 须知

    转载 转载需通过博主同意方可 代码格式 博主遵循 \(Google\) 代码格式,代码满足以下规范: 字符数 每行代码必需不超过 80 字符 缩进 缩进不使用制表符,而是 2 个空格缩进 函数 函数左 ...

  5. injected stylesheet 谷歌扩展插件,造成样式异常

    今天在开发的时候,遇到一个问题,就是我们我在写发送广告的功能,然后我用了一个textare文本框,这个时候,发现了一个问题.这个文本框凭空消失了.不见了,我以为是自己的那个样式不小心把这个隐藏掉了后来 ...

  6. 【前端学习】网页tab键的实现 01

    友情提醒:阅读本文需要了解一些基本的html/Css/Javascript知识 前端常用tab键的实现,用到的原理是当点击一个元素时,通过javascript操作css的display属性,达到控制另 ...

  7. web 自动化遇到 shadowDOM 节点你会操作吗?

    本文转载自: http://www.lemfix.com/topics/971 近期有同学在做web自动化的时候,发现页面上有些元素,在selenium中无法通过xpath来定位,各种原因找了半天,都 ...

  8. 【每天一题】LeetCode 0028. 字符串匹配

    开源地址:https://github.com/jiauzhang/algorithms 题目描述 * https://leetcode-cn.com/problems/implement-strst ...

  9. SpringBoot系列-整合Mybatis(XML配置方式)

    目录 一.什么是 MyBatis? 二.整合方式 三.实战 四.测试 本文介绍下SpringBoot整合Mybatis(XML配置方式)的过程. 一.什么是 MyBatis? MyBatis 是一款优 ...

  10. 知道内存中一个图片的指针IntPtr大小,转换成图片显示

    //nSize 为总长度//pImageData 为总数据//nImageSize //一个图片的长度 byte[] _bytes = new byte[nImageSize];// //IntPtr ...