C # in the status bar, self-drawing progress bar,

Source: Internet
Author: User
Tags count tostring
There's nothing else to say, so let's talk about this in the status bar, the way to draw a progress bar.
I do the website, has been very envious of the FTP software, the address bar in the progress bar, so cool ....
Have been guessing, people are how to put the progress bar control. Put it on the address bar???????

- -!! Khan ... A few days ago because the work needs, the UTF format to write the Web program, but also batch processing.
Wrote the software. A moment of inspiration, try to actually successfully painted the progress bar.

The following is the source code:

(another: The status bar Name:stat three item is: Stat_1 stat_2 stat_3, Stat_3 property set to self painting.)
**************************************************************************
Convert Button Event:
****************
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.", "hint", messageboxbuttons.ok,messageboxicon.information);
Processing=false;
Fileconvert.enabled=true;
}
************************************************************************
Beginconvert () Method code
********************
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, "hint", messageboxbuttons.ok,messageboxicon.exclamation);
}
}
}
**************************************************************************
(There is a stat above.) Refresh () ... is to let the status bar refresh. )
The following is the stat DrawItem event code.
*****************************
private void Stat_drawitem (object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
if (processing)//judgment is being processed ....
{
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); Get the whole stat_3 bound.
RectangleF rectdraw=new RectangleF (x,y,w,h); Bound of the progress currently being processed
G.clip=new Region (rect);
Calculate the coordinates of the 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); Write the xx% in bold
G.fillrectangle (New SolidBrush (Color.FromArgb (21,29,193)), Rectdraw); A dark square that fills the current progress.
G.clip=new Region (Rectdraw); Reset the clip to the current progress bound,
g.DrawString (sb,new Font ("Arial", 9), New SolidBrush (Color.White), PF); Then write the white xx%.
This will ensure that the progress bar no matter where, the dark box above the word, is white, normal place is black.
}
G.dispose ();
}

Ability is limited, consciously in take stat_3 of bound of time, use the method is very stupid.
But looking for a long time, do not know what other methods can be directly obtained. Stat_3 's bound.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.