我们经常看见有些软件在执行某个较长时间的运算时,会在窗体的状态栏中出现动态的动画或状态条。如图1是VS.Net 2003在编译整个方案时,出现的状态栏效果。那这个效果是如何制作的呢?
图1
其实,这些动画或状态条是作为控件嵌入到StatusBar控件中的Panels集合中的,然后再重画此Panels的区域。
现以嵌入进度条控件为例说明如何制作:
1. 拉一个进度条,一个状态栏、一个按钮到窗体中;如图2:
图2
2. 在状态栏中添加两个Panel;
……
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1,
this.statusBarPanel2});
……
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1,
this.statusBarPanel2});
……
3. 设置this.statusBarPanel1的Style = OwnerDraw;
4. 编写如下代码:
private void statusBar1_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
//重设progressBar1的边框大小
if(sbdevent.Panel.Equals(this.statusBarPanel1))
{
Rectangle r = sbdevent.Bounds;
r.Inflate(1,1);
this.progressBar1.Bounds = r;
}
}
private void button3_Click(object sender, System.EventArgs e)
{
this.statusBar1.Controls.Add(this.progressBar1);//把进度条控件加入到状态栏中
for(int i = 1 ;i<=100;i++)
{
System.Threading.Thread.Sleep(10);
this.progressBar1.Value = i;
this.statusBarPanel2.Text = i.ToString() + "%";
}
}
{
//重设progressBar1的边框大小
if(sbdevent.Panel.Equals(this.statusBarPanel1))
{
Rectangle r = sbdevent.Bounds;
r.Inflate(1,1);
this.progressBar1.Bounds = r;
}
}
private void button3_Click(object sender, System.EventArgs e)
{
this.statusBar1.Controls.Add(this.progressBar1);//把进度条控件加入到状态栏中
for(int i = 1 ;i<=100;i++)
{
System.Threading.Thread.Sleep(10);
this.progressBar1.Value = i;
this.statusBarPanel2.Text = i.ToString() + "%";
}
}
5. 运行程序,单出按钮,进度条在增长。效果如图3:
图3