WebCast听课录(8)

课程名:Windows应用程序开发入门到精通五:Windows应用程序界面美化<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

1, 当要进行长时间的计算工作时,不应该在用户界面(UI)主线程中进行从而阻塞主主线程,而应该开一个新的线程来进行。

2, 可以使用ThreadPool.QueueUserWorkItem()来进行异步调用。

3, 从其他线程中更新用户界面中的控件时,需要使用BeginInvokedelegate来进行。

None.gif private delegate void UpdateProgressDelegate(SinglePercentDone);
None.gif
protected void UpdateProgress(SinglePercentDone)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(InvokeRequired)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//marshallthecallintotheMainUIthreaddot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
BeginInvoke(newUpdateProgressDelegate(UpdateProgress),newobject[]dot.gif{PercentDone});//来自外部的调用
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
if(0==PercentDone)isCanceled=true;
InBlock.gifbtnCancelProgress.Visible
=!isCanceled;
InBlock.gifpbProgress.Visible
=!isCanceled;
InBlock.gifpbProgress.Value
=(int)PercentDone;
InBlock.gifsbpStatus.Text
=pbProgress.Value+"%complete";
InBlock.gif
if(0==PercentDone)
InBlock.gifsbpStatus.Text
=string.Empty;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
private delegate void SetStatusTextDelegate( string StatusText);
None.gif
None.gif
protected void SetStatusText( string StatusText)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(InvokeRequired)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//marshallthecallintothemainUIthread
ExpandedSubBlockStart.gifContractedSubBlock.gif
BeginInvoke(newSetStatusTextDelegate(SetStatusText),newobject[]dot.gif{StatusText});//来自外部的调用
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gifsbpStatus.Text
=StatusText;
ExpandedBlockEnd.gif}

None.gif

4, 若有些操作必须是阻塞的,而且很难计算出这些操作的进度,就需要使用等待指针(WaitCursor)。使用try….catch…finally并在finally中将鼠标的指针重新设置为默认状态。 对于时间较长的操作,要给用户提示当前完成的进度。

None.gif try
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
this.Cursor=Cursors.WaitCursor;
InBlock.gifStatusBar1.Text
="Longprocessrunningdot.gif";
InBlock.gif
for(inti=1;i<50000000;i++)
InBlock.gifProgressBar1.Value
=(int)(((float)i/50000000)*100);
InBlock.gif
InBlock.gifStatusBar1.Text
="Longprocessdone";
ExpandedBlockEnd.gif}

None.gif
finally
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
this.Cursor=Cursors.Default;
ExpandedBlockEnd.gif}

None.gif

5, 使用ListBox.Items.AddRange()可以增强性能,比使用Items.Add()方法要提高大约5倍左右。

6, 可以使用IComparer接口来自定义排序的方法。

None.gif private object []GetRandomIntArray( int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
object[]aintArray=(object[])Array.CreateInstance(typeof(object),n-1);
InBlock.gifRandomrnd
=newRandom();
InBlock.gif
for(inti=0;i<aintArray.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifaintArray[i]
=Convert.ToInt32(
InBlock.gifrnd.Next(
0,System.Int32.MaxValue));
ExpandedSubBlockEnd.gif}

InBlock.gif
returnaintArray;
ExpandedBlockEnd.gif}

None.gif
object []aintArray = GetRandomIntArray(Convert.ToInt32(TextBox1.Text));
None.gif
None.gifIComparerComparer
= new DescendingIntComparer();
None.gif
None.gifArray.Sort(aintArray,Comparer);
None.gif
None.gif
private class DescendingIntComparer:IComparer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
publicintCompare(objectx,objecty)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Implementourownsortinglogichoweverwewant:
InBlock.gif
return(-1)*Convert.ToInt32(x).CompareTo(Convert.ToInt32(y));
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

7, VS安装文件夹下带了一个WizardFramework.dll,可以用于创建标准的”look and feel”向导程序。

8, 自定义绘制状态条:

None.gif private void sbStatus_DrawItem( object sender,System.Windows.Forms.StatusBarDrawItemEventArgssbdevent)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(sbdevent.Panel.Equals(sbpProgress))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifRectangler
=sbdevent.Bounds;
InBlock.gifr.Inflate(
1,1);
InBlock.gifpbProgress.Bounds
=r;
ExpandedSubBlockEnd.gif}

InBlock.gif
elseif(sbdevent.Panel.Equals(sbpCancelButton))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifRectangler
=sbdevent.Bounds;
InBlock.gifr.Inflate(
1,1);
InBlock.gifbtnCancelProgress.Bounds
=r;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
private void frmBaseForm_Load( object sender,System.EventArgse)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifsbStatus.Controls.Add(btnCancelProgress);
InBlock.gifsbStatus.Controls.Add(pbProgress);
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值