C# WinForm进度条实现:首先是进度条窗体,需要在上面添加进度条,然后去掉ControlBox。除此外,还要增加一个方法,用来控制进度条的增加幅 度,具体如下:
代码
1
.
///
﹤summary﹥
2 . /// Increase process bar
3 . /// ﹤/summary﹥ //C# WinForm进度条实现
4 . /// ﹤param name="nValue"﹥the value increased﹤/param﹥
5 . /// ﹤returns﹥﹤/returns﹥
6 . public bool Increase( int nValue )
7 . {
8 . if ( nValue ﹥ 0 )
9 . {
10 . if ( prcBar.Value + nValue ﹤ prcBar.Maximum )
11 . {
12 . prcBar.Value += nValue;
13 . return true ;
14 . }
15 . else
16 . {
17 . prcBar.Value = prcBar.Maximum;
18 . this .Close();
19 . return false ;
20 . }
21 . }
22 . return false ;
23 . }
2 . /// Increase process bar
3 . /// ﹤/summary﹥ //C# WinForm进度条实现
4 . /// ﹤param name="nValue"﹥the value increased﹤/param﹥
5 . /// ﹤returns﹥﹤/returns﹥
6 . public bool Increase( int nValue )
7 . {
8 . if ( nValue ﹥ 0 )
9 . {
10 . if ( prcBar.Value + nValue ﹤ prcBar.Maximum )
11 . {
12 . prcBar.Value += nValue;
13 . return true ;
14 . }
15 . else
16 . {
17 . prcBar.Value = prcBar.Maximum;
18 . this .Close();
19 . return false ;
20 . }
21 . }
22 . return false ;
23 . }
C# WinForm进度条实现之主窗体:
如何进行操作了,首先需要定义两个私有成员,一个委托。其中一个私有成员是保存当前进度条窗体对象,另一个是保存委托方法(即增加进度条尺度),具 体如下:
1
.
private
frmProcessBar myProcessBar
=
null
;
2 . private delegate bool IncreaseHandle( int nValue );
3 . private IncreaseHandle myIncrease = null ;
2 . private delegate bool IncreaseHandle( int nValue );
3 . private IncreaseHandle myIncrease = null ;
C# WinForm进度条实现之在主窗体中提供函数来打开进度条窗体,如下:
代码
1
.
///
﹤summary﹥
2 . /// Open process bar window
3 . /// ﹤/summary﹥
4 . private void ShowProcessBar()
5 . {
6 . myProcessBar = new frmProcessBar();
7 .
8 . // Init increase event
9 . myIncrease =
10 . new IncreaseHandle( myProcessBar.Increase );
11 . myProcessBar.ShowDialog();
12 . myProcessBar = null ;
13 . }
2 . /// Open process bar window
3 . /// ﹤/summary﹥
4 . private void ShowProcessBar()
5 . {
6 . myProcessBar = new frmProcessBar();
7 .
8 . // Init increase event
9 . myIncrease =
10 . new IncreaseHandle( myProcessBar.Increase );
11 . myProcessBar.ShowDialog();
12 . myProcessBar = null ;
13 . }
C# WinForm进度条实现之开始创建线程来运行,具体如下:
代码
1
.
///
﹤summary﹥
2 . /// Sub thread function
3 . /// ﹤/summary﹥
4 . private void ThreadFun()
5 . {
6 . MethodInvoker mi =
7 . new MethodInvoker( ShowProcessBar );
8 . this .BeginInvoke( mi );
9 .
10 . Thread.Sleep( 1000 );
11 . // Sleep a while to show window
12 .
13 . bool blnIncreased = false ;
14 . object objReturn = null ;
15 . do
16 . {
17 . Thread.Sleep( 50 );
18 . objReturn = this .Invoke(
19 . this .myIncrease,
20 . new object []{ 2 } );
21 . blnIncreased = ( bool )objReturn ;
22 . }
23 . while ( blnIncreased );
24 . }
2 . /// Sub thread function
3 . /// ﹤/summary﹥
4 . private void ThreadFun()
5 . {
6 . MethodInvoker mi =
7 . new MethodInvoker( ShowProcessBar );
8 . this .BeginInvoke( mi );
9 .
10 . Thread.Sleep( 1000 );
11 . // Sleep a while to show window
12 .
13 . bool blnIncreased = false ;
14 . object objReturn = null ;
15 . do
16 . {
17 . Thread.Sleep( 50 );
18 . objReturn = this .Invoke(
19 . this .myIncrease,
20 . new object []{ 2 } );
21 . blnIncreased = ( bool )objReturn ;
22 . }
23 . while ( blnIncreased );
24 . }
注意以上,在打开进度条窗体和增加进度条进度的时候,一个用的是BeginInvoke,一个是Invoke,这里的区别是BeginInvoke 不需要等待方法运行完毕,而Invoke是要等待方法运行完毕。还有一点,此处用返回值来判断进度条是否到头了,如果需要有其他的控制,可以类似前面的方 法来进行扩展。
C# WinForm进度条实现之启动线程,可以如下:
1
. Thread thdSub
=
new
Thread(
2 . new ThreadStart( ThreadFun ) );
3 . thdSub.Start();
2 . new ThreadStart( ThreadFun ) );
3 . thdSub.Start();
这样,一个用模式打开进度条窗体就做完了。
C# WinForm进度条实现的基本情况就向你介绍到这里,希望对你学习和了解C# WinForm进度条实现有所帮助。