C#自定义控件 ————进度条
先看看样式




一个扇形的进度条
对外公开的方法和属性
事件
value_change;//值改变时触发的事件
progress_finshed;//进度条跑完时触发的事件
属性
Max_value//获取最大值
Min_value//获取最小值
Current_value//设置或获取当前值设置
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication19
{
public partial class CustomControl1 : Control
{
Bitmap background;
//进度条的最大值,最小值,和当前值
;
;
private int current_value;
;//用户保护max和min变量只赋值一次的值
;//
//控件的最小吃寸
private int min_size;
//三个颜色,最外层,中间层和最内填充层
, , );
, , );
, , );
//字体颜色
private Color str_color = Color.Black;
//最外层和中间层的宽度
private float edge_line_width = 5f;
private float edge_2_line_width = 5f;
private DockStyle dock=DockStyle.None;
//事件
public delegate void vaule_change_invoke(EventArgs args);
public event vaule_change_invoke value_change;//值改变时触发的事件
public event vaule_change_invoke progress_finshed;//进度条跑完时触发的事件
//这里我暂时不对外开放接口,控件样式固定
/*
public Color Edge_line_color
{
get { return edge_line_color; }
set { edge_line_color = value; }
}
public Color Edge_2_line_color
{
get { return edge_2_line_color; }
set { edge_2_line_color = value; }
}
public Color Inside_fill_color
{
get { return inside_fill_color; }
set { inside_fill_color = value; }
}
public Color Str_color
{
get { return str_color; }
set { str_color = value; }
}
public float Edge_line_width
{
get { return edge_line_width; }
set
{
if (value > 0)
edge_line_width = value;
}
}
public float Edge_2_line_width
{
get { return edge_2_line_width; }
set
{
if (value > 0)
edge_2_line_width = value;
}
}
*/
[Browsable(false)]
public new DockStyle Dock
{
get { return dock; }
set { dock = value; }
}
/// <summary>
/// 获取最小值
/// </summary>
[Browsable(false)]
public int Min_value
{
get { return min_value; }
private set
{
&&value<max_value && min_value_protect_value == )
{
min_value = value;
current_value = value;
min_value_protect_value = ;
}
}
}
/// <summary>
/// 获取最大值
/// </summary>
[Browsable(false)]
public int Max_value
{
get { return max_value; }
private set
{
&& value > Min_value && max_value_protect_value == )
{
max_value = value;
max_value_protect_value=;
}
}
}
/// <summary>
/// 设置或获取当前值,范围0-100
/// </summary>
public int Current_value
{
get { return current_value; }
set
{
if (value >= min_value && value <= max_value)
{
current_value = value;
//触发派生重写方法
this.onvaluechange(new EventArgs());
//重绘控件
this.Invalidate();
if (progress_finshed != null)
{
//进度条跑完时触发
if (max_value == current_value)
this.progress_finshed(new EventArgs());
}
if (value_change != null)
{
//触发事件
this.value_change(new EventArgs());
}
}
}
}
public CustomControl1()
{
InitializeComponent();
setinfo();
//这里触发一次OnInvalidated,保证在onpaint前建立背景位图
this.Invalidate();
min_size = ( + edge_line_width * + );
}
/// <summary>
/// 绘图优化
/// </summary>
private void setinfo()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
/// <summary>
/// 值修改触发的方法
/// </summary>
/// <param name="eventArgs"></param>
protected virtual void onvaluechange(EventArgs eventArgs)
{
}
protected override void OnResize(EventArgs e)
{
//保证控件最小尺寸 //防止拖拽时崩毁
int temp = Math.Min(Width, Height);
temp = temp > min_size ? temp : min_size;
Width = Height = temp;
build_bitmap_buffer();
this.Invalidate();
base.OnResize(e);
}
/// <summary>
/// 控件及子控件重绘
/// </summary>
/// <param name="pe"></param>
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
/// <summary>
/// 控件重绘
/// </summary>
/// <param name="e"></param>
protected override void OnInvalidated(InvalidateEventArgs e)
{
if (background != null)
update_the_progress_bar();
else
{
build_bitmap_buffer();
}
base.OnInvalidated(e);
}
/// <summary>
/// 创建背景缓冲位图
/// </summary>
private void build_bitmap_buffer()
{
if (background != null)
{
background.Dispose();
}
background = new Bitmap(Width, Width);
Graphics g = Graphics.FromImage(background);
g.Clear(Color.Transparent);
g.Dispose();
}
/// <summary>
/// 根据当前值更新进度条
/// </summary>
private void update_the_progress_bar()
{
Graphics g = Graphics.FromImage(background);
Draw_with_HighQuality(g);
g.Clear(Color.Transparent);
Pen p = new Pen(edge_line_color, edge_line_width);
#region 绘制最外部圆
);
int w_h = Width - (int)edge_line_width;
Rectangle rect = new Rectangle(x_y, x_y, w_h, w_h);
g.DrawEllipse(p, rect);
#endregion
#region 绘制第二层圆
p.Width = edge_2_line_width;
p.Color = edge_2_line_color;
x_y += ( + edge_2_line_width / );
w_h = Width - ( + edge_2_line_width);
rect = new Rectangle(x_y, x_y, w_h, w_h);
g.DrawEllipse(p, rect);
#endregion
p.Dispose();
#region 绘制扇形
Brush sb = new SolidBrush(inside_fill_color);
x_y = (int)(edge_line_width + edge_2_line_width);
w_h = Width - x_y * ;
rect = new Rectangle(x_y, x_y, w_h, w_h);
float rad = ((float)current_value - min_value) / ((float)max_value - min_value);
* rad);
g.FillPie(sb, rect, -, frad);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString((rad * ).ToString() + , FontStyle.Bold), new SolidBrush(str_color), this.ClientRectangle, sf);
#endregion
g.Dispose();
this.BackgroundImage = background;
}
/// <summary>
/// 高质量绘图
/// </summary>
/// <param name="g">绘图工具graphics</param>
private void Draw_with_HighQuality(Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
}
}
}
源码地址: http://pan.baidu.com/s/1i3N2vlz
压缩文件为测试控件的demo;
cs是控件的源码文件;直接添加到项目中和其他控件一样拖进去就可以了
C#自定义控件 ————进度条的更多相关文章
- (四十一)c#Winform自定义控件-进度条
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (四十二)c#Winform自定义控件-进度条扩展
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (七)c#Winform自定义控件-进度条
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- 自定义控件 进度条 ProgressBar-2
使用 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bqt ...
- Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)
最近一直在学习自定义控件,搜了许多大牛们Blog里分享的小教程,也上GitHub找了一些类似的控件进行学习.发现读起来都不太好懂,就想写这么一篇东西作为学习笔记吧. 一.控件介绍: 进度条在App中非 ...
- Android自定义控件系列之应用篇——圆形进度条
一.概述 在上一篇博文中,我们给大家介绍了Android自定义控件系列的基础篇.链接:http://www.cnblogs.com/jerehedu/p/4360066.html 这一篇博文中,我们将 ...
- Qt自定义控件系列(一) --- 圆形进度条
本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...
- c# 自定义控件之小小进度条
先看效果图: 非常简洁的一个进度条. 完整项目源码下载:http://files.cnblogs.com/files/tuzhiyuan/%E8%BF%9B%E5%BA%A6%E6%9D%A1%E6% ...
- (三十八)c#Winform自定义控件-圆形进度条-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
随机推荐
- cordova 获取地理位置
第一步,引入插件 cordova plugin add cordova-plugin-geolocation 第二步, <!DOCTYPE html> <html> <h ...
- python学习-4-类的使用
class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): ...
- Linux 3 -grep
七. grep家族: 1. grep退出状态: 0: 表示成功: 1: 表示在所提供的文件无法找到匹配的pattern: 2: 表示参数中提供的文件不存在. 见如下示例: /> grep 'ro ...
- Linux vim 中文显示乱码解决方法
因为在windows下默认是gb编码,而我的vim默认是utf-8(gedit默认也是utf-8),所以打开会成乱码.改动了一下配置文件,使vi支持gb编码就好了.$vi ~/.vimrclet &a ...
- 为什么要用redux?component自身管理自己的state难道不更解耦吗?
这是前几天百度2面的时候,面试官问我的一个问题.说实话当时有点紧张,其实也没去细想,本身react我就学了2个多星期,虽然看过redux这些源码,不过这个问题好像我从来没想过. 那其实react官网本 ...
- 《python基础教程(第二版)》学习笔记 字符串(第3章)
<python基础教程(第二版)>学习笔记 字符串(第3章)所有的基本的序列操作(索引,分片,乘法,判断成员资格,求长度,求最大最小值)对字符串也适用.字符串是不可以改变的:格式化输出字符 ...
- retry.RetryInvocationHandler (RetryInvocationHandler.java:invoke(140)) - Exception while invoking getFileInfo of class ClientNamenodeProtocolTranslatorPB over bdata236/192.168.1.236:9000 after 3 fail
报错信息如下 -- ::, INFO [main]: retry.RetryInvocationHandler (RetryInvocationHandler.java:invoke()) - Exc ...
- python_unittest详解
一 整体结构概览 unittest原名为PyUnit,是由java的JUnit衍生而来.对于单元测试,需要设置预先条件,对比预期结果和实际结果. 整体结构:unittest库提供了test cases ...
- 从request中读数据流
ServletInputStream servletInputStream = reqeust.getInputStream(); int len=0; int size=reqeust.getCon ...
- 京东自营,你TM太坑了。
双12来了,京东自营好坑.昨天(12月6日)看的一条秋裤,89元,今天准备买,居然涨到了119,他大爷的. 京东你大爷的.