官网

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">

NuGet

Install-Package HZH_Controls

目录

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

准备工作

前面写过一个进度条,但是并不是太好,这次用GDI+再重绘一个,不了解GDI+的自行百度了解下先

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

开始

添加一个类,命名UCProcessLine,继承Control

添加一个枚举,用以如何显示值

  public enum ValueTextType
{
None,
/// <summary>
/// 百分比
/// </summary>
Percent,
/// <summary>
/// 数值
/// </summary>
Absolute
}

添加一些属性

 [Description("值变更事件"), Category("自定义")]
public event EventHandler ValueChanged;
int m_value = ;
[Description("当前属性"), Category("自定义")]
public int Value
{
set
{
if (value > m_maxValue)
m_value = m_maxValue;
else if (value < )
m_value = ;
else
m_value = value;
if (ValueChanged != null)
ValueChanged(this, null);
Refresh();
}
get
{
return m_value;
}
} private int m_maxValue = ; [Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value < m_value)
m_maxValue = m_value;
else
m_maxValue = value;
Refresh();
}
} Color m_valueColor = Color.FromArgb(, , ); [Description("值进度条颜色"), Category("自定义")]
public Color ValueColor
{
get { return m_valueColor; }
set
{
m_valueColor = value;
Refresh();
}
} private Color m_valueBGColor = Color.White; [Description("值背景色"), Category("自定义")]
public Color ValueBGColor
{
get { return m_valueBGColor; }
set
{
m_valueBGColor = value;
Refresh();
}
} private Color m_borderColor = Color.FromArgb(, , ); [Description("边框颜色"), Category("自定义")]
public Color BorderColor
{
get { return m_borderColor; }
set
{
m_borderColor = value;
Refresh();
}
} [Description("值字体"), Category("自定义")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
Refresh();
}
} [Description("值字体颜色"), Category("自定义")]
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
Refresh();
}
}
private ValueTextType m_valueTextType = ValueTextType.Percent; [Description("值显示样式"), Category("自定义")]
public ValueTextType ValueTextType
{
get { return m_valueTextType; }
set
{
m_valueTextType = value;
Refresh();
}
}

重绘

  protected override void OnPaint(PaintEventArgs e)
{
Console.WriteLine(DateTime.Now);
base.OnPaint(e);
Graphics g = e.Graphics;
g.SetGDIHigh(); Brush sb = new SolidBrush(m_valueBGColor);
g.FillRectangle(sb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.ClientRectangle.Width - , base.ClientRectangle.Height - ));
GraphicsPath path1 = ControlHelper.CreateRoundedRectanglePath(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + , base.ClientRectangle.Width - , base.ClientRectangle.Height - ), );
g.DrawPath(new Pen(m_borderColor, ), path1);
LinearGradientBrush lgb = new LinearGradientBrush(new Point(, ), new Point(, base.ClientRectangle.Height - ), m_valueColor, Color.FromArgb(, m_valueColor.R, m_valueColor.G, m_valueColor.B));
g.FillPath(lgb, ControlHelper.CreateRoundedRectanglePath(new Rectangle(, (base.ClientRectangle.Height - (base.ClientRectangle.Height - )) / , (base.ClientRectangle.Width - ) * Value / m_maxValue, base.ClientRectangle.Height - ), ));
string strValue = string.Empty;
if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Percent)
strValue = ((float)Value / (float)m_maxValue).ToString("0%");
else if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Absolute)
strValue = Value + "/" + m_maxValue;
if (!string.IsNullOrEmpty(strValue))
{
System.Drawing.SizeF sizeF = g.MeasureString(strValue, Font);
g.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / , (this.Height - 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 class UCProcessLine : Control
{
[Description("值变更事件"), Category("自定义")]
public event EventHandler ValueChanged;
int m_value = ;
[Description("当前属性"), Category("自定义")]
public int Value
{
set
{
if (value > m_maxValue)
m_value = m_maxValue;
else if (value < )
m_value = ;
else
m_value = value;
if (ValueChanged != null)
ValueChanged(this, null);
Refresh();
}
get
{
return m_value;
}
} private int m_maxValue = ; [Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value < m_value)
m_maxValue = m_value;
else
m_maxValue = value;
Refresh();
}
} Color m_valueColor = Color.FromArgb(, , ); [Description("值进度条颜色"), Category("自定义")]
public Color ValueColor
{
get { return m_valueColor; }
set
{
m_valueColor = value;
Refresh();
}
} private Color m_valueBGColor = Color.White; [Description("值背景色"), Category("自定义")]
public Color ValueBGColor
{
get { return m_valueBGColor; }
set
{
m_valueBGColor = value;
Refresh();
}
} private Color m_borderColor = Color.FromArgb(, , ); [Description("边框颜色"), Category("自定义")]
public Color BorderColor
{
get { return m_borderColor; }
set
{
m_borderColor = value;
Refresh();
}
} [Description("值字体"), Category("自定义")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
Refresh();
}
} [Description("值字体颜色"), Category("自定义")]
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
Refresh();
}
}
private ValueTextType m_valueTextType = ValueTextType.Percent; [Description("值显示样式"), Category("自定义")]
public ValueTextType ValueTextType
{
get { return m_valueTextType; }
set
{
m_valueTextType = value;
Refresh();
}
}
public UCProcessLine()
{
Size = new Size(, );
ForeColor = Color.FromArgb(, , );
Font = new Font("Arial Unicode MS", );
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)
{
Console.WriteLine(DateTime.Now);
base.OnPaint(e);
Graphics g = e.Graphics;
g.SetGDIHigh(); Brush sb = new SolidBrush(m_valueBGColor);
g.FillRectangle(sb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.ClientRectangle.Width - , base.ClientRectangle.Height - ));
GraphicsPath path1 = ControlHelper.CreateRoundedRectanglePath(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + , base.ClientRectangle.Width - , base.ClientRectangle.Height - ), );
g.DrawPath(new Pen(m_borderColor, ), path1);
LinearGradientBrush lgb = new LinearGradientBrush(new Point(, ), new Point(, base.ClientRectangle.Height - ), m_valueColor, Color.FromArgb(, m_valueColor.R, m_valueColor.G, m_valueColor.B));
g.FillPath(lgb, ControlHelper.CreateRoundedRectanglePath(new Rectangle(, (base.ClientRectangle.Height - (base.ClientRectangle.Height - )) / , (base.ClientRectangle.Width - ) * Value / m_maxValue, base.ClientRectangle.Height - ), ));
string strValue = string.Empty;
if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Percent)
strValue = ((float)Value / (float)m_maxValue).ToString("0%");
else if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Absolute)
strValue = Value + "/" + m_maxValue;
if (!string.IsNullOrEmpty(strValue))
{
System.Drawing.SizeF sizeF = g.MeasureString(strValue, Font);
g.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / , (this.Height - sizeF.Height) / + ));
}
} } public enum ValueTextType
{
None,
/// <summary>
/// 百分比
/// </summary>
Percent,
/// <summary>
/// 数值
/// </summary>
Absolute
}
}

用处及效果

最后的话

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

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

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

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

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

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

  3. winform异步进度条LongTime

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

  4. 第二百四十一节,Bootstrap进度条媒体对象和 Well 组件

    第二百四十一节,Bootstrap进度条媒体对象和 Well 组件 学习要点: 1.Well 组件 2.进度条组件 3.媒体对象组件 本节课我们主要学习一下 Bootstrap 的三个组件功能:Wel ...

  5. C#编程总结(四)多线程应用(进度条的编程问题)——转自http://www.cnblogs.com/yank/p/3232955.html

    多线程应用 多线程应用很广泛,简单总结了一下: 1)不阻断主线程,实现即时响应,由后台线程完成特定操作2)多个线程,完成同类任务,提高并发性能3)一个任务有多个独立的步骤,多个线程并发执行各子任务,提 ...

  6. 关于C# WinForm中进度条的实现方法

    http://www.cnblogs.com/Sue_/articles/2024932.html 进度条是一个软件人性化考虑之一,他给用户的感觉就是程序内部在不停的动作,执行到了什么程度,而不是整个 ...

  7. C#winform使用进度条

    在用c#做WinFrom开发的过程中.我们经常需要用到进度条(ProgressBar)用于显示进度信息.这时候我们可能就需要用到多线程,如果不采用多线程控制进度条,窗口很容易假死(无法适时看到进度信息 ...

  8. C# winform带进度条的图片下载

    代码如下: public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private void ...

  9. C#自定义控件 ————进度条

    先看看样式 一个扇形的进度条 对外公开的方法和属性 事件 value_change;//值改变时触发的事件progress_finshed;//进度条跑完时触发的事件 属性 Max_value//获取 ...

随机推荐

  1. 学习使用Quartz,java

    报名立减200元.暑假直降6888. 邀请链接:http://www.jnshu.com/login/1/20535344 邀请码:20535344 Quartz官网 添加quartz到Java应用中 ...

  2. Prim算法与Kruskal(没有代码)

    两个最小生成树算法, 都有一个共同的思想: 这棵树是一点一点长大的; 并且每次生长, 都是贪心的. 不同之处是: Kruscal算法是以边为中心的, 每次找最小的并且有用的边添加到树上; Prim算法 ...

  3. 【基础算法-模拟-例题-*校长的问题】-C++

    为什么在题目前面打上星号呢? 这道题的正解不是模拟! 正解树状数组! 正解树状数组! 正解树状数组! 重要的事情说够三遍了! 但是,歪解模拟因为数据水都能AC! 因为这道题放在模拟专题中,所以我们就讨 ...

  4. ASP.NET 前端数据绑定---<%#%>及Eval()的使用

    ASP.NET 前端html代码中会经常出现的<%%>的代码,里面的文本其实就是不能直接输出到客户端浏览器的文本,是需要服务器解释的. 在ASP中,<%%>里面的文本是vbsc ...

  5. c++小游戏:洛谷彩票

    #include <cstdlib> #include <iostream> #include <cstdio> #include <cmath> #i ...

  6. matlab考试重点详解

    此帖是根据期末考试复习重点补充完成, 由于使用word编辑引用图片和链接略有不便, 所以开此贴供复习及学习使用.侵删 复习要点 第一章 Matlab的基本概念,名称的来源,基本功能,帮助的使用方法 1 ...

  7. 分布式Streaming Data Processing - Samza

    ​ 现在的主流的互联网应用越来越依赖streaming data来提供用户一些interesting statistics insights.以linkedin为例,最近90天有多少人看过你的link ...

  8. 在springboot中使用swagger2

    1.在springboot中使用swagger的话,首先在pom文件中引入依赖 <!-- https://mvnrepository.com/artifact/io.springfox/spri ...

  9. python-if条件判断与while/for循环

    条件判断if 让计算机像人一样,能判断是非对错,根据条件做一些事情. if ''' ------ if代码结构:------- if 条件: 代码体 tips:同一缩进范围内的代码被视作同一代码体,p ...

  10. Linux AUFS 文件系统

    AUFS 的英文全称为 Advanced Mult-Layered Unification Filesystem,曾经是 Another Mult-Layered Unification Filesy ...