程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> C#在狀態欄中,自繪進度條,

C#在狀態欄中,自繪進度條,

編輯:.NET實例教程

別的都沒什麼好說的了,說說這個在狀態欄中,畫進度條的辦法吧.
偶是做網站的,一直很羨慕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 .

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved