我正好将代码整理出来,将下面代码copy到你的程序,直接使用UpdateValue就行了public class ProgressStatusBar : System.Windows.Forms.StatusBar
{ public ProgressStatusBar()
{
this.SizingGrip = false;
this.ShowPanels = true;
}
protected override void
OnDrawItem(StatusBarDrawItemEventArgs e)
{
if
(e.Panel.GetType()==typeof(ProgressPanel))
{
ProgressPanel ProgressPanel =
(ProgressPanel) e.Panel; if (ProgressPanel.Value >
ProgressPanel.Minimum)
{
int NewWidth =
(int)(((double)ProgressPanel.Value / (double)ProgressPanel.Maximum) *
(double)ProgressPanel.Width);
Rectangle NewBounds = e.Bounds;
SolidBrush PaintBrush = new
SolidBrush(ProgressPanel.ForeColor);
NewBounds.Width = NewWidth;
e.Graphics.FillRegion(PaintBrush, new Region(NewBounds));
PaintBrush.Dispose();
}
else
{
base.OnDrawItem(e);
}
}
else
{
base.OnDrawItem(e);
}
} public void UpdateValue(ProgressPanel ProgressPanel, int
NewValue)
{
ProgressPanel.Value = NewValue;
}
} public class ProgressPanel : System.Windows.Forms.StatusBarPanel
{
private int m_Minimum = 1;
private int m_Maximum = 100;
private int m_Value = 0;
private Color m_Color; public ProgressPanel()
{
this.Style = StatusBarPanelStyle.OwnerDraw;
this.ForeColor = Color.DarkBlue;
} public int Minimum
{
get { return m_Minimum; }
set { m_Minimum = value; }
} public int Maximum
{
get { return m_Maximum; }
set { m_Maximum = value; }
}
public int Value
{
get { return m_Value; }
set { m_Value = value; }
} public Color ForeColor
{
get { return m_Color; }
set { m_Color = value; }
}
}
{ public ProgressStatusBar()
{
this.SizingGrip = false;
this.ShowPanels = true;
}
protected override void
OnDrawItem(StatusBarDrawItemEventArgs e)
{
if
(e.Panel.GetType()==typeof(ProgressPanel))
{
ProgressPanel ProgressPanel =
(ProgressPanel) e.Panel; if (ProgressPanel.Value >
ProgressPanel.Minimum)
{
int NewWidth =
(int)(((double)ProgressPanel.Value / (double)ProgressPanel.Maximum) *
(double)ProgressPanel.Width);
Rectangle NewBounds = e.Bounds;
SolidBrush PaintBrush = new
SolidBrush(ProgressPanel.ForeColor);
NewBounds.Width = NewWidth;
e.Graphics.FillRegion(PaintBrush, new Region(NewBounds));
PaintBrush.Dispose();
}
else
{
base.OnDrawItem(e);
}
}
else
{
base.OnDrawItem(e);
}
} public void UpdateValue(ProgressPanel ProgressPanel, int
NewValue)
{
ProgressPanel.Value = NewValue;
}
} public class ProgressPanel : System.Windows.Forms.StatusBarPanel
{
private int m_Minimum = 1;
private int m_Maximum = 100;
private int m_Value = 0;
private Color m_Color; public ProgressPanel()
{
this.Style = StatusBarPanelStyle.OwnerDraw;
this.ForeColor = Color.DarkBlue;
} public int Minimum
{
get { return m_Minimum; }
set { m_Minimum = value; }
} public int Maximum
{
get { return m_Maximum; }
set { m_Maximum = value; }
}
public int Value
{
get { return m_Value; }
set { m_Value = value; }
} public Color ForeColor
{
get { return m_Color; }
set { m_Color = value; }
}
}
private void statusBar1_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
if (sbdevent.Panel == statusBarPanel1)
{
Graphics g = sbdevent.Graphics;
float finishedPercent = float.Parse(statusBarPanel1.Text);// 转换为ValueType
int rectWidth = (int)( finishedPercent * (sbdevent.Bounds.Width-2) );
g.FillRectangle( new SolidBrush(Color.DarkGoldenrod), sbdevent.Bounds.Left + 1, sbdevent.Bounds.Top + 2, rectWidth, sbdevent.Bounds.Height-4);
}
}然后在你的过程中使用以下两句来实现赋值和强制发生DrawItem事件。percent :你所要指定的值。
this.statusBarPanel1.Text = percent.ToString();
this.statusBar1.Refresh();我的方案都是这么做的,挺灵的。利用渐变刷可画出更好看的任务进度条。