using System;
using System.Drawing;
using System.Windows.Forms;namespace NewsSpider
{
/// <summary>
/// ProgressStatusBarPanel 的摘要说明。
/// </summary>
public class ProgressStatusBarPanel : System.Windows.Forms.StatusBarPanel
{
private float _value = 0;
private float _Maximum=100;
private object _tag;
private StatusBarDrawItemEventHandler drawHandler;

public ProgressStatusBarPanel()

base.Style = StatusBarPanelStyle.OwnerDraw;
this.drawHandler =
new StatusBarDrawItemEventHandler(this.ParentStatusBar_DrawItem);
} /// <summary>
/// 用 new 操作符将样式固定为 OwnerDraw。
/// 不能进行设置
/// </summary>
public new StatusBarPanelStyle Style
{
get
{
return base.Style;
}
} /// <summary>
/// 进度值,此处使用最小值为 0,最大值为 100。
/// 可以自行增加 Min,Max的定义 。
/// </summary>
  public object Tag
{
get
{
return _tag;
}
set
{
_tag=value;
} }
public float Maximum
{
get
{
return _Maximum;
}
set
{
_Maximum=value;
}
} public float Value
{
get
{
return this._value;
}
set
{
if (this._value == value)
return;
if (value < 0)
this._value = 0;
else if (value >this._Maximum)
this._value = this._Maximum;
else
this._value = value;
this.ToolTipText = (this._value/this._Maximum).ToString("p2"); // 此处最好通过查询父状态条的 Panels 集合来确定当前进度板所在的实际矩形
// 在 Invalidate 时将此矩形传入。可以减少不必要的闪烁并提高性能。
// 以下会重画整个状态条。
if (this.Parent != null)
this.Parent.Invalidate();
}
}

// 能过此方法的增强和修改可以达到你所想要的效果
// 譬如:
// 可以准备一图形来描述进度值,
// 可以使用渐变刷来增强进度颜色效果,
// 可以画出进度条的分隔条,
// 可以在背景上填充一图形,
// 可以画出进度百分比值,
// ...
// 等等,充分发挥你的想像力吧。
private void ParentStatusBar_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
if (sbdevent.Panel == this)
{
if (this.Value == 0)
return;
Graphics g = sbdevent.Graphics;
//sbdevent.DrawBackground();
sbdevent.DrawBackground(); Rectangle rc = new Rectangle(sbdevent.Bounds.Left, sbdevent.Bounds.Top, 
(int)(this.Width * this._value / this._Maximum), sbdevent.Bounds.Height);
g.FillRectangle(Brushes.Tomato, rc);
}
} // 绑定绘图事件处理到父状态条。
// 此方法在进度状态面板加入到状态条后执行,否则无效。
// 如果从父状态条中移除时,记得
public void BindDrawHandlerToParent()
{
if (this.Parent != null)
{
this.Parent.DrawItem += drawHandler;
}
} // 在应用时,应注意在更改其父状态条,将已绑定绘图事件处理委托移除。
// 此方法从父状态条移除事件处理委托
public void RemoveDrawHandlerFromParent()
{
if (this.Parent != null)
{
this.Parent.DrawItem -= drawHandler;
}
}
}
}