进度条
来源:互联网 发布:linux中string什么意思 编辑:程序博客网 时间:2024/05/21 09:12
自定义进度条控件类:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace VerticalProgressBar
{
/// <summary>
/// 枚举型进度条样式。
/// </summary>
public enum Styles
{
Classic,
Solid
}
/// <summary>
/// 枚举型进度条边框样式。
/// </summary>
public enum BorderStyles
{
Classic,
None
}
/// <summary>
/// 描画一格垂直进度条控件。
/// </summary>
[Description("Vertical Progress Bar")]
[ToolboxBitmap(typeof(ProgressBar))]
[Browsable(false)]
public sealed class VerticalProgressBar : System.Windows.Forms.UserControl
{
#region 属性默认值
private System.ComponentModel.Container components = null;
//当前值。
private int m_Value = 50;
//最小值。
private int m_Minimum = 0;
//最大值。
private int m_Maximum = 100;
//步长。
private int m_Step = 10;
//进度条样式。
private Styles m_Style = Styles.Classic;
//边框样式。
private BorderStyles m_BorderStyle = BorderStyles.Classic;
//进度条颜色。
private Color m_Color = Color.Blue;
//是否显示百分比。
private bool m_Percent;
#endregion
#region 属性
/// <summary>
/// 初始化进度条。
/// </summary>
public VerticalProgressBar()
{
InitializeComponent();
//设置样式。
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
//设置名称。
this.Name = "VerticalProgressBar";
//设置大小。
this.Size = new Size(10, 120);
}
/// <summary>
/// 百分比。
/// </summary>
[Description("VerticalProgressBar Percent")]
[Category("VerticalProgressBar")]
[RefreshProperties(RefreshProperties.All)]
public bool Percent
{
get
{
return m_Percent;
}
set
{
m_Percent = value;
Invalidate();
}
}
/// <summary>
/// 最大值。
/// </summary>
[Description( "VerticalProgressBar Maximum Value")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public int Maximum
{
get
{
return m_Maximum;
}
set
{
m_Maximum = value;
if (m_Maximum < m_Minimum)
{
m_Minimum = m_Maximum;
}
if(m_Maximum < m_Value)
{
m_Value = m_Maximum;
}
Invalidate();
}
}
/// <summary>
/// 最小值。
/// </summary>
[Description( "VerticalProgressBar Minimum Value")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public int Minimum
{
get
{
return m_Minimum;
}
set
{
m_Minimum = value;
if(m_Minimum > m_Maximum)
{
m_Maximum = m_Minimum;
}
if(m_Minimum > m_Value)
{
m_Value = m_Minimum;
}
Invalidate();
}
}
/// <summary>
/// 步长。
/// </summary>
[Description( "VerticalProgressBar Step")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public int Step
{
get
{
return m_Step;
}
set
{
m_Step = value;
}
}
/// <summary>
/// 当前值。
/// </summary>
[Description( "VerticalProgressBar Current Value")]
[Category( "VerticalProgressBar" )]
public int Value
{
get
{
return m_Value;
}
set
{
m_Value = value;
if(m_Value > m_Maximum)
{
m_Value = m_Maximum;
}
if(m_Value < m_Minimum)
{
m_Value = m_Minimum;
}
Invalidate();
}
}
/// <summary>
/// 进度条颜色。
/// </summary>
[Description( "VerticalProgressBar Color")]
[Category( "VerticalProgressBar" )]
[RefreshProperties(RefreshProperties.All)]
public System.Drawing.Color Color
{
get
{
return m_Color;
}
set
{
m_Color = value;
Invalidate();
}
}
/// <summary>
/// 进度条边框颜色。
/// </summary>
[Description( "VerticalProgressBar Border Style")]
[Category( "VerticalProgressBar" )]
public new BorderStyles BorderStyle
{
get
{
return m_BorderStyle;
}
set
{
m_BorderStyle = value;
//重画无效区域。
Invalidate();
}
}
/// <summary>
/// 进度条样式。
/// </summary>
[Description( "VerticalProgressBar Style")]
[Category( "VerticalProgressBar" )]
public Styles Style
{
get
{
return m_Style;
}
set
{
m_Style = value;
Invalidate();
}
}
/// <summary>
/// 执行步长。
/// </summary>
public void PerformStep()
{
m_Value+=m_Step;
if (m_Value > m_Maximum)
{
m_Value = m_Maximum;
}
if (m_Value < m_Minimum)
{
m_Value = m_Minimum;
}
Invalidate();
return;
}
/// <summary>
/// 改变进度。
/// </summary>
/// <param name="value"></param>
public void Increment(int value)
{
m_Value+=value;
if(m_Value > m_Maximum)
{
m_Value = m_Maximum;
}
if(m_Value < m_Minimum)
{
m_Value = m_Minimum;
}
Invalidate();
return;
}
#endregion
/// <summary>
/// 描画进度条边框。
/// </summary>
/// <param name="dc"></param>
private void drawBorder(Graphics dc)
{
if(m_BorderStyle == BorderStyles.Classic)
{
Color darkColor = ControlPaint.Dark(this.BackColor);
Pen p = new Pen(darkColor, 1);
//左侧线。
dc.DrawLine(p, this.Width, 0, 0, 0);
//上部线。
dc.DrawLine(p, 0, 0, 0, this.Height);
Color brightColor = ControlPaint.Dark(this.BackColor);
p = new Pen(brightColor, 1);
//底部线。
dc.DrawLine(p, 0, this.Height-1, this.Width, this.Height -1 );
//dc.DrawLine(p, 0, 10, 10, 10);
//右侧线。
dc.DrawLine(p, this.Width-1, this.Height, this.Width-1, 0);
}
}
/// <summary>
/// 描画进度条主体。
/// </summary>
/// <param name="dc">Graphics对象</param>
private void drawBar(Graphics dc)
{
if(m_Minimum == m_Maximum || (m_Value - m_Minimum) == 0)
return;
int width; // the bar width
int height; // the bar height
int x; // the bottom-left x pos of the bar
int y; // the bottom-left y pos of the bar
if(m_BorderStyle == BorderStyles.None)
{
width = this.Width;
x = 0;
y = this.Height;
}
else
{
if(this.Width > 4 || this.Height > 2)
{
width = this.Width - 4;
x = 2;
y = this.Height - 1;
}
else
{
//不描画。
return;
}
}
//进度条高度。
height = (m_Value - m_Minimum) * this.Height / (m_Maximum - m_Minimum);
if(m_Style == Styles.Solid)
{
drawSolidBar(dc, x, y, width, height);
}
if(m_Style == Styles.Classic)
{
drawClassicBar(dc, x, y, width, height);
}
}
/// <summary>
/// 描画进度条样式为Solid
/// </summary>
/// <param name="dc">Graphics对象</param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
private void drawSolidBar(Graphics dc, int x, int y, int width, int height)
{
dc.FillRectangle(new SolidBrush(m_Color), x, y-height, width, height);
}
/// <summary>
/// 描画进度条样式为Classic
/// </summary>
/// <param name="dc">Graphics 对象</param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
private void drawClassicBar(Graphics dc, int x, int y, int width, int height)
{
int valuepos_y = y - height; // The pos y of value
int blockheight = width * 3 / 4; // The height of the block
if (blockheight <= -1) return; // make sure blockheight is larger than -1 in order not to have the infinite loop.
for(int currentpos = y; currentpos > valuepos_y; currentpos -= blockheight+1)
{
dc.FillRectangle(new SolidBrush(m_Color), x, currentpos - blockheight, width, blockheight);
}
}
/// <summary>
/// 开始描画。
/// </summary>
/// <param name="e">PaintEventArgs 对象</param>
protected override void OnPaint(PaintEventArgs e)
{
Graphics dc = e.Graphics;
//描画进度条。
drawBar(dc);
if (this.Percent ==true )
{
DrawStringRectangleFFormat(e);
}
//描画边框。
drawBorder(dc);
base.OnPaint(e);
}
/// <summary>
/// 描画字体。
/// </summary>
/// <param name="e">PaintEventArgs 对象</param>
public void DrawStringRectangleFFormat(PaintEventArgs e)
{
//要描画的字符串。
String drawString = this.m_Value.ToString () + "%";
// 字体大小样式。
Font drawFont = new Font("Arial", 8);
//字体颜色。
SolidBrush drawBrush = new SolidBrush(Color.Red );
//描画区域。
RectangleF drawRect = new RectangleF(this.Width /20, this.Height / 2, this.Width, this.Height / 2);
//字体格式。
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
//描画字体。
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
}
/// <summary>
/// 改变大小
/// </summary>
/// <param name="e">EventArgs 对象</param>
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged (e);
Invalidate();
}
/// <summary>
/// 释放资源。
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 编辑器生成的代码。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// VerticalProgressBar
//
this.Name = "VerticalProgressBar";
this.Size = new System.Drawing.Size(170, 45);
this.ResumeLayout(false);
}
}
}
测试方法:
bool u5 = true;
private void timer1_Tick(object sender, EventArgs e)
{
if (verticalProgressBar.Value >= verticalProgressBar.Maximum )
{
u5 = false;
}
if (verticalProgressBar.Value <= verticalProgressBar.Minimum )
{
u5 = true;
}
if (u5)
{
verticalProgressBar.Value += 1;
}
else
{
verticalProgressBar.Value -= 1;
}
}
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 进度条
- 将项目打包成可执行文件(.exe)
- SQl Server中的ISNULL函数用法
- SQL Server中的OBJECTPROPERTY函数使用
- WaitForSingleObject
- Sql Server游标使用
- 进度条
- PowerPoint中如何插入MP3歌曲
- 进度条
- 进度条
- SQL Server 2005语法增强之OBJECT_ID
- ld.so.conf 文件与PKG_CONFIG_PATH变量
- B树
- SQL Server游标中的inserted和deleted临时表
- 指定目录下查找指定目录(文件)