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