官网

http://www.hzhcontrols.com

前提

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

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

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

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

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

NuGet

Install-Package HZH_Controls

目录

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

用处及效果

准备工作

这个是基于(四十一)c#Winform自定义控件-进度条 扩展的,如果你还没有了解,请先移步了解一下

开始

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

属性

   [Description("值变更事件"), Category("自定义")]
public event EventHandler ValueChanged; [Description("当前属性"), Category("自定义")]
public int Value
{
set
{
ucProcessLine1.Value = value;
Refresh();
}
get
{
return ucProcessLine1.Value;
}
} [Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return ucProcessLine1.MaxValue; }
set
{
ucProcessLine1.MaxValue = value;
Refresh();
}
} [Description("值进度条颜色"), Category("自定义")]
public Color ValueColor
{
get { return ucProcessLine1.ValueColor; }
set
{
ucProcessLine1.ValueColor = value;
Refresh();
}
} [Description("值背景色"), Category("自定义")]
public Color ValueBGColor
{
get { return ucProcessLine1.ValueBGColor; }
set
{
ucProcessLine1.ValueBGColor = value;
Refresh();
}
} [Description("边框颜色"), Category("自定义")]
public Color BorderColor
{
get { return ucProcessLine1.BorderColor; }
set
{
ucProcessLine1.BorderColor = value;
Refresh();
}
} [Description("值字体"), Category("自定义")]
public override Font Font
{
get
{
return ucProcessLine1.Font;
}
set
{
ucProcessLine1.Font = value;
Refresh();
}
} [Description("值块颜色"), Category("自定义")]
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
Refresh();
}
}

重绘

  protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SetGDIHigh();
float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue; int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - ) - ;
GraphicsPath path = new GraphicsPath();
Rectangle rect = new Rectangle(x, , , );
int cornerRadius = ;
path.AddArc(rect.X, rect.Y, cornerRadius * , cornerRadius * , , );
path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * , rect.Y);
path.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y, cornerRadius * , cornerRadius * , , );
path.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height - cornerRadius * );
path.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y + rect.Height - cornerRadius * , cornerRadius * , cornerRadius * , , );
path.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.Right - cornerRadius * - , rect.Bottom);//下
path.AddLine(rect.Right - cornerRadius * - , , x + , ucProcessLine1.Location.Y);
path.AddLine(x + , ucProcessLine1.Location.Y, rect.X + cornerRadius * + , );
path.AddLine(rect.X + cornerRadius * + , , rect.X + cornerRadius * , rect.Bottom);//下
path.AddArc(rect.X, rect.Bottom - cornerRadius * , cornerRadius * , cornerRadius * , , );
path.AddLine(rect.X, rect.Bottom - cornerRadius * , rect.X, rect.Y + cornerRadius * );//上
path.CloseFigure(); e.Graphics.FillPath(new SolidBrush(ForeColor), path); string strValue = ((float)Value / (float)MaxValue).ToString("0%");
System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + ( - sizeF.Width) / +, ( - sizeF.Height) / + ));
}

全部代码

 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 UCProcessLineExt : UserControl
{
[Description("值变更事件"), Category("自定义")]
public event EventHandler ValueChanged; [Description("当前属性"), Category("自定义")]
public int Value
{
set
{
ucProcessLine1.Value = value;
Refresh();
}
get
{
return ucProcessLine1.Value;
}
} [Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return ucProcessLine1.MaxValue; }
set
{
ucProcessLine1.MaxValue = value;
Refresh();
}
} [Description("值进度条颜色"), Category("自定义")]
public Color ValueColor
{
get { return ucProcessLine1.ValueColor; }
set
{
ucProcessLine1.ValueColor = value;
Refresh();
}
} [Description("值背景色"), Category("自定义")]
public Color ValueBGColor
{
get { return ucProcessLine1.ValueBGColor; }
set
{
ucProcessLine1.ValueBGColor = value;
Refresh();
}
} [Description("边框颜色"), Category("自定义")]
public Color BorderColor
{
get { return ucProcessLine1.BorderColor; }
set
{
ucProcessLine1.BorderColor = value;
Refresh();
}
} [Description("值字体"), Category("自定义")]
public override Font Font
{
get
{
return ucProcessLine1.Font;
}
set
{
ucProcessLine1.Font = value;
Refresh();
}
} [Description("值块颜色"), Category("自定义")]
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
Refresh();
}
} public UCProcessLineExt()
{
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);
ucProcessLine1.ValueChanged += ucProcessLine1_ValueChanged;
} void ucProcessLine1_ValueChanged(object sender, EventArgs e)
{
if (ValueChanged != null)
{
ValueChanged(this, e);
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SetGDIHigh();
float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue; int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - ) - ;
GraphicsPath path = new GraphicsPath();
Rectangle rect = new Rectangle(x, , , );
int cornerRadius = ;
path.AddArc(rect.X, rect.Y, cornerRadius * , cornerRadius * , , );
path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * , rect.Y);
path.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y, cornerRadius * , cornerRadius * , , );
path.AddLine(rect.Right, rect.Y + cornerRadius * , rect.Right, rect.Y + rect.Height - cornerRadius * );
path.AddArc(rect.X + rect.Width - cornerRadius * , rect.Y + rect.Height - cornerRadius * , cornerRadius * , cornerRadius * , , );
path.AddLine(rect.Right - cornerRadius * , rect.Bottom, rect.Right - cornerRadius * - , rect.Bottom);//下
path.AddLine(rect.Right - cornerRadius * - , , x + , ucProcessLine1.Location.Y);
path.AddLine(x + , ucProcessLine1.Location.Y, rect.X + cornerRadius * + , );
path.AddLine(rect.X + cornerRadius * + , , rect.X + cornerRadius * , rect.Bottom);//下
path.AddArc(rect.X, rect.Bottom - cornerRadius * , cornerRadius * , cornerRadius * , , );
path.AddLine(rect.X, rect.Bottom - cornerRadius * , rect.X, rect.Y + cornerRadius * );//上
path.CloseFigure(); e.Graphics.FillPath(new SolidBrush(ForeColor), path); string strValue = ((float)Value / (float)MaxValue).ToString("0%");
System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + ( - sizeF.Width) / +, ( - sizeF.Height) / + ));
}
}
}
 namespace HZH_Controls.Controls
{
partial class UCProcessLineExt
{
/// <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.ucProcessLine1 = new HZH_Controls.Controls.UCProcessLine();
this.SuspendLayout();
//
// ucProcessLine1
//
this.ucProcessLine1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ucProcessLine1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucProcessLine1.Font = new System.Drawing.Font("Arial Unicode MS", 10F);
this.ucProcessLine1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucProcessLine1.Location = new System.Drawing.Point(, );
this.ucProcessLine1.MaxValue = ;
this.ucProcessLine1.Name = "ucProcessLine1";
this.ucProcessLine1.Size = new System.Drawing.Size(, );
this.ucProcessLine1.TabIndex = ;
this.ucProcessLine1.Text = "ucProcessLine1";
this.ucProcessLine1.Value = ;
this.ucProcessLine1.ValueBGColor = System.Drawing.Color.White;
this.ucProcessLine1.ValueColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucProcessLine1.ValueTextType = HZH_Controls.Controls.ValueTextType.None;
//
// UCProcessLineExt
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.Transparent;
this.Controls.Add(this.ucProcessLine1);
this.Name = "UCProcessLineExt";
this.Size = new System.Drawing.Size(, );
this.ResumeLayout(false); } #endregion private UCProcessLine ucProcessLine1;
}
}

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

(四十二)c#Winform自定义控件-进度条扩展的更多相关文章

  1. (四十一)c#Winform自定义控件-进度条

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

  2. (七)c#Winform自定义控件-进度条

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

  3. (四十)c#Winform自定义控件-开关-HZHControls

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

  4. NeHe OpenGL教程 第四十二课:多重视口

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

  5. 网站开发进阶(四十二)巧用clear:both

    网站开发进阶(四十二)巧用clear:both 前言 我们在制作网页中用div+css或者称xhtml+css都会遇到一些很诡异的情况,明明布局正确,但是整个画面却混乱起来了,有时候在IE6下看的很正 ...

  6. winform异步进度条LongTime

    winform异步进度条LongTime,运用到回调函数 定义事件的参数类: namespace LongTime.Business { // 定义事件的参数类 public class ValueE ...

  7. Gradle 1.12用户指南翻译——第四十二章. Announce插件

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

  8. SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关

    0x1普通测试方式 (1)输入and1=1和and1=2测试,返回错误,证明存在注入 (2)union select联合查询 (3)查询表名 (4)其他 payload: ,( ,( 0x2 堆叠注入 ...

  9. “全栈2019”Java第四十二章:静态代码块与初始化顺序

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

随机推荐

  1. 掌握简单的Makefile文件编程

    Makefile描述整个程序的编译.链接规则 其中还包括了工程中用到的那些源文件及需要产生的目标文件 1)Makefile编程规则 目标(唯一):依赖(可多个) 命令... 伪目标 .PHONY:cl ...

  2. 从0系统学Android--1.2 手把手带你搭建开发环境

    要想进行程序开发,首先我们需要搭建开发环境,下面就开始搭建环境. 1.2.1 所需的工具 首先 Android 开发是基于 Java 的,因此你需要掌握简单的 Java 语法.会基础的 Java 语法 ...

  3. 洛谷P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解

    题目链接: https://www.luogu.org/problemnew/show/P2880 分析: ST表实现即可,一个最大值数组和最小值数组同时维护 代码: #include<cstd ...

  4. 洛谷:P2952 [USACO09OPEN]牛线Cow Line:题解

    题目链接:https://www.luogu.org/problemnew/show/P2952 分析: 这道题非常适合练习deque双端队列,~~既然是是练习的板子题了,建议大家还是练练deque, ...

  5. 个人永久性免费-Excel催化剂功能第92波-地理地址与经纬度互转功能

    GPS设备和手机LBS的兴起,在地理信息存储过程中,在程序.应用级别是需要用经纬度去定位,而在数据分析的级别,特别是省市区镇街的分析,用到的是人可识别的文本类型存储,从设备中采集下来的数据和人工维护的 ...

  6. springBean生命周期----来自spring实战总结

    1.Spring对bean进行实例化 2.Spring将值和bean的引用注入到bean对应的属性中(比如说注入到被依赖的bean的方法中或属性里) 3.如果bean实现了BeanNameAware接 ...

  7. restapi(2)- generic restful CRUD:通用的restful风格数据库表维护工具

    研究关于restapi的初衷是想搞一套通用的平台数据表维护http工具.前面谈过身份验证和使用权限.文件的上传下载,这次来到具体的数据库表维护.我们在这篇示范里设计一套通用的对平台每一个数据表的标准维 ...

  8. 2019杭电多校第一场hdu6581 Vacation

    Vacation 题目传送门 update(O(n)) 看了那个O(n)的方法,感觉自己想的那个O(nlogn)的好傻,awsl. 0车最终通过停车线的时候,状态一定是某个车堵住后面的所有车(这个车也 ...

  9. python课堂整理7---集合

    前面小节: sep 用来控制每个元素间的间隔符号 print("alex", "dabai", "liu", sep = "&qu ...

  10. poj 2524 Ubiquitous Religions(简单并查集)

    对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...