C# 进度条状态栏面板






4.21/5 (15投票s)
2004年7月1日
1分钟阅读

295857

4426
一个可重用的进度条面板控件,用于在状态栏内显示。
引言
你不能直接将进度条控件拖放到状态栏面板中,所以我做了这个。这实际上是我第二次做这个了,所以我受够了,想把代码放到这里,这样就没人需要再做一遍了。
使用代码
首先要做的是在你的表单上放置一个状态栏,并创建一两个 Panel
。 你可以在之后调整面板设置,但最简单的方法是从一开始就在你想作为进度条的状态栏面板上选择“Owner-Draw”(所有者绘制)。
// Change the type of the panel to StatusBarProgressPanel on your Form
private StatusBarProgressPanel progressPanel;
// Initialize the measurement values
progressPanel.StartPoint = 0;
progressPanel.EndPoint = 100;
progressPanel.StepSize = 1;
progressPanel.ProgressPosition = 0;
当然,一旦你更改了类型,就可以通过在表单设计器中选择状态栏时,简单地点击 Panel
属性的“...”按钮来更改属性。 我包含了一些方便的类别属性,供 Visual Studio 精妙的反射逻辑使用。
// Sample usage:
// progress the bar by one "step"
progressPanel.Step();
// reset the progress bar to the initial state
progressPanel.Reset();
// start/stop an infinite animation thread for the bar
// this is useful when you don't know how long something
// is going to take, or if the progress is just unmeasurable
progressPanel.StartAnimation();
progressPanel.StopAnimation();
// By default the value of the percentage of progress is displayed
// setting the "ShowText" property to false disables it
progressPanel.ShowText = false;
你也可以更改控件的 AnimationStyle
。 这基本上控制着指示器的填充和清空方式。 默认值是 ProgressDisplayStyle.Infinate
,这是一种脉冲绘制样式。 可以使用更酷的图形,只需修改 Parent_DrawItem
方法即可。
/// <summary>
/// Statusbar Progress Display Styles
/// </summary>
public enum ProgressDisplayStyle
{
/// <summary>
/// A continually moving animation
/// </summary>
Infinite,
/// <summary>
/// A progress bar that fills from left to right
/// </summary>
LeftToRight,
/// <summary>
/// A progress bar that fills from right to left
/// </summary>
RightToLeft,
/// <summary>
/// A progress bar that fills from bottom to top
/// </summary>
BottomToTop,
/// <summary>
/// A progress bar that fills from top to bottom
/// </summary>
TopToBottom
}
历史
- 2004/7/1 - 初始修订版。