|
|
C#在状态栏中,自绘进度条, |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
下面即是软件运行时的效果图....

别的都没什么好说的了,说说这个在状态栏中,画进度条的办法吧. 偶是做网站的,一直很羡慕FTP软件中,地址栏中的进度条,那么酷.... 一直在猜想,人家是怎么把进度条控件..放到地址栏上的???????
- -!! 汗...前几天因为工作需要,用UTF格式来写WEB程序,还要成批处理. 写了这软件.一时灵感,试了试居然成功的自绘了进度条.
下面是源码:
(另:状态栏name: stat 三个item 分别是: stat_1 stat_2 stat_3 ,stat_3属性设置成自绘. ************************************************************************** Convert 按钮事件: **************** private void FileConvert_Click(object sender, System.EventArgs e) { //Stat Files Count And Fill Array(files), Change Stat.Text, //Begin Process FileConvert.Enabled=false; sOK.Items.Clear(); Files.Clear(); //Show The Layer ..... Stating Files, Please Wait... StatFiles(sDrive.Text); stat1.Text=" Files Count:"+Files.Count.ToString(); stat2.Text=" Current:Null"; FileCount=Files.Count; Processing=true; BeginConvert(Files); MessageBox.Show("Processed "+Files.Count.ToString()+" Files.","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); Processing=false; FileConvert.Enabled=true; } ************************************************************************ BeginConvert()方法代码 ******************** private void BeginConvert(ArrayList al) { string t=""; for(int n=0;n<al.Count;n++) { try { if(File.Exists(al[n].ToString())) { //===================== stat2.Text=" Current:"+FormatShortFile(al[n].ToString()); FileCurrent=n+1; stat.Refresh(); Application.DoEvents(); //===================== t=IO.Read(al[n].ToString()); StreamWriter sw=new StreamWriter(al[n].ToString(),false,Encoding.Unicode); sw.Write(t); sw.Close(); sw=null; sOK.Items.Add((object)al[n].ToString().Replace(sDrive.Text,"")); } } catch(Exception e) { MessageBox.Show(e.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); } } } ************************************************************************** (上面有一个 stat.Refresh()...是让状态栏刷新.) 下面是stat的 DrawItem事件代码. ***************************** private void stat_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent) { if(Processing) //判断是否正在处理中.... { Graphics g=stat.CreateGraphics(); int x,y,w,wall,h; x=stat1.Width+stat2.Width+4; y=4; wall=stat.Width-stat1.Width-stat2.Width-6; w=wall * FileCurrent / FileCount; h=stat.Height-7; int sWidth=w-x; RectangleF rect=new RectangleF(x,y,wall,h); //得到整个 stat_3 的Bound RectangleF rectDraw=new RectangleF(x,y,w,h); //当前处理的进度的 Bound //g.Clip=new Region(rect); //计算 xx% 的坐标 string sb=Convert.ToInt32((((float)FileCurrent/(float)FileCount)*100)).ToString()+"%"; SizeF sf=g.MeasureString(sb,new Font("Arial",9)); PointF pf=new PointF(stat1.Width+stat2.Width+6+(wall-sf.Width)/2,(h-sf.Height)/2+3); if(Processing) { g.DrawString(sb,new Font("Arial",9),new SolidBrush(Color.Black),pf); //写上黑体的 xx % g.FillRectangle(new SolidBrush(Color.FromArgb(21,29,193)),rectDraw); // 填充当前进度的暗色方块. g.Clip=new Region(rectDraw); //重新设置 Clip为 当前进度的Bound , g.DrawString(sb,new Font("Arial",9),new SolidBrush(Color.White),pf); //再写白色的 xx% //这样就可以保证进度条不管到哪里,暗色方块上面的字,是白色,正常地方则是黑色. } g.Dispose(); }
能力有限,自觉在取 stat_3 的 Bound的时候,用的办法甚笨.. 但找了半天,不知道还有什么办法可以直接得到.stat_3的Bound .
若谁知道,请给偶留言告知....谢谢~!~!~!~! 或者谁有更好的办法,也请一并.....嘿.   
|
|
相关文章:相关软件: |
|