官网

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. ServiceFabric极简文档-5.1 编程模型选择

    项目中:actor用的服务是无状态服务:ASP.NET Core用的是无状态ASP.NET Core模板. ​

  2. 嵌入式Linux开发环境搭建,问题ping、nfs的解决

    一. 嵌入式软件层次 1) Bootloader->引导加载程序 整个嵌入式系统的加载启动任务完全交给Bootloader完成,它的主要任务是将内核映象从硬盘读到RAM中,然后跳转到内核入口启动 ...

  3. Lucene03--字段属性

    Lucene03--字段属性 1.Field 1.1  Field相当于Javabean的属性. 1.2  不同的Field的构造方法参数不一样: 大多数Field的构造函数有三个参数: a)第一个参 ...

  4. 【Arduino】66种传感器系列实验(1)---干簧管传感器模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和各种模块,依照实践(动手试试)出真知的理念,以学习和交流为目的,这里 ...

  5. Dubbo源码学习之-Adaptive自适应扩展

    前言 最近三周基本处于9-10-6与9-10-7之间,忙碌的节奏机会丢失了自己.除了之前干施工的那段经历,只看参加软件开发以来,前段时间是最繁忙的了.忙的原因,不是要完成的工作量大,而是各种环境问题, ...

  6. 172. 阶乘后的零 Java解法

    https://leetcode-cn.com/problems/factorial-trailing-zeroes/ 172. 阶乘后的零 这题要完成其实要知道一个很巧妙的思想,就是阶乘里面,后面的 ...

  7. LeetCode 138:复制带随机指针的链表 Copy List with Random Pointer

    给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. A linked list is given such that each no ...

  8. iOS Xcode6 新建OC Category文件

    首先:File -> New File 接下来界面如下,选择Objective-C File,然后Next 在这里选择 Category 即可

  9. Flutter学习笔记(13)--表单组件

    如需转载,请注明出处:Flutter学习笔记(13)--表单组件 表单组件是个包含表单元素的区域,表单元素允许用户输入内容,比如:文本区域,下拉表单,单选框.复选框等,常见的应用场景有:登陆.注册.输 ...

  10. PHP xdebug API接口优化揪出了getimagesize这个鬼

    在API优化list中,公司客户系统的服务号客服有个获取聊天消息的接口getHistory请求时间很长,就去优化了下,记下过程. 一,配置环境,追踪使用Xdebug: 1.在https://xdebu ...