Create a smooth progress bar in Visual C #. NET

Source: Internet
Author: User
Tags min new features range client visual studio
Visual content of this article
Overview
To create a custom progress bar control
Build a simple client application

--------------------------------------------------------------------------------

Overview
This article describes how to create a simple, custom user control--a smooth progress bar.

In earlier progress bar control versions, such as those provided in the Microsoft Windows Common Controls ActiveX control, you can see that the progress bar has two different views. You can set the Standard view or Smooth view by setting the scrolling property. The Smooth view provides an area for smooth display of progress, and Standard attempts to represent progress by a single square.

The progress bar control provided in Visual C #. NET supports only the Standard view.

The code sample in this article shows how to create a control with the following properties:

Minimum. This property represents the minimum value of the progress bar. By default it is 0; You cannot set this property to a negative value.
Maximum. The property represents the maximum value of the progress bar. By default, 100 is the case.
Value. This property represents the current value of the progress bar. The value must be between Minimum and Maximum.
Progressbarcolor. This property represents the color of the progress bar.
Return
--------------------------------------------------------------------------------

To create a custom progress bar control
1, follow the steps below to establish a Windows control Library project in Visual C #. NET:

A, open Microsoft Visual Studio. NET.

b, click on the File menu, click New, and then click Project.

C, in the New Project dialog box, select Visual C # Projects in Project Types, and then select the Windows control Library in the Templates.

D, in the Name box, fill in the Smoothprogressbar and click OK.

E, in Project Explorer, rename the default class module and change UserControl1.cs to SmoothProgressBar.cs.

F, in the Properties window of the UserControl object, change its Name property from UserControl1 to Smoothprogressbar.

2. At this point, you have inherited a new class from the control class and can add new features. However, ProgressBar tired is sealed (sealed), can no longer be inherited. Therefore, you must build this control from scratch.

Add the following code to the UserControl module, just after "Windows Form Designer generated code":

int min = 0; Minimum Value for Progress range
int max = 100; Maximum Value for Progress range
int val = 0; Current progress
Color barcolor = Color.Blue; Color of Progress Meter

protected override void OnResize (EventArgs e)
{
Invalidate the control to get a repaint.
This. Invalidate ();
}

protected override void OnPaint (PaintEventArgs e)
{
Graphics g = e.graphics;
SolidBrush brush = new SolidBrush (Barcolor);
float percent = (float) (val-min)/(float) (max-min);
Rectangle rect = this. ClientRectangle;

Calculate area for drawing the progress.
Rect. Width = (int) ((float) rect. Width * percent);

Draw the progress meter.
G.fillrectangle (brush, rect);

Draw a three-dimensional border around the control.
Draw3dborder (g);

Clean up.
Brush. Dispose ();
G.dispose ();
}

public int Minimum
{
Get
{
return min;
}

Set
{
prevent a negative value.
if (Value < 0)
{
min = 0;
}

Make sure this minimum value is never set higher than the maximum value.
if (Value > Max)
{
min = value;
min = value;
}

Ensure value is still in range
if (Val < min)
{
val = min;
}

Invalidate the control to get a repaint.
This. Invalidate ();
}
}

public int Maximum
{
Get
{
return Max;
}

Set
{
Make sure this maximum value is never set lower than the minimum value.
if (Value < min)
{
min = value;
}

max = value;

Make sure this value is still in range.
if (val > Max)
{
val = max;
}

Invalidate the control to get a repaint.
This. Invalidate ();
}
}

public int Value
{
Get
{
return Val;
}

Set
{
int oldValue = val;

Make sure this value does not stray outside the valid range.
if (Value < min)
{
val = min;
}
else if (Value > Max)
{
val = max;
}
Else
{
val = value;
}

Invalidate only the changed area.
float percent;

Rectangle Newvaluerect = this. ClientRectangle;
Rectangle Oldvaluerect = this. ClientRectangle;

Use a new value to calculate the rectangle for progress.
Percent = (float) (val-min)/(float) (max-min);
Newvaluerect.width = (int) ((float) newvaluerect.width * percent);

Use a old value to calculate the rectangle for progress.
Percent = (float) (oldvalue-min)/(float) (max-min);
Oldvaluerect.width = (int) ((float) oldvaluerect.width * percent);

Rectangle updaterect = new Rectangle ();

Find only the ' part of the ' screen ' that must is updated.
if (Newvaluerect.width > Oldvaluerect.width)
{
Updaterect.x = OldValueRect.Size.Width;
Updaterect.width = Newvaluerect.width-oldvaluerect.width;
}
Else
{
Updaterect.x = NewValueRect.Size.Width;
Updaterect.width = Oldvaluerect.width-newvaluerect.width;
}

Updaterect.height = this. Height;

Invalidate the intersection region only.
This. Invalidate (Updaterect);
}
}

Public Color Progressbarcolor
{
Get
{
return barcolor;
}

Set
{
Barcolor = value;

Invalidate the control to get a repaint.
This. Invalidate ();
}
}

private void Draw3dborder (Graphics g)
{
int penwidth = (int) Pens.White.Width;

G.drawline (Pens.darkgray,
New Point (this. Clientrectangle.left, this. Clientrectangle.top),
New Point (this. Clientrectangle.width-penwidth, this. Clientrectangle.top));
G.drawline (Pens.darkgray,
New Point (this. Clientrectangle.left, this. Clientrectangle.top),
New Point (this. Clientrectangle.left, this. Clientrectangle.height-penwidth));
G.drawline (Pens.white,
New Point (this. Clientrectangle.left, this. Clientrectangle.height-penwidth),
New Point (this. Clientrectangle.width-penwidth, this. Clientrectangle.height-penwidth));
G.drawline (Pens.white,
New Point (this. Clientrectangle.width-penwidth, this. Clientrectangle.top),
New Point (this. Clientrectangle.width-penwidth, this. Clientrectangle.height-penwidth));
}

3, in the Build menu, click on the builds Solution to compile the entire project.

Return
--------------------------------------------------------------------------------

Build a simple client application
1, in the File menu, click New, and then click Project.

2. In the ADD New Project dialog box, click Visual C # Projects in Project Types, click Windows Application in the Templates, and click OK.

3, follow the steps below to add two Smoothprogressbar instances on the Form:

A, on the Tools menu, click Customize Toolbox.

b, click the. NET Framework Components page.

C, click Browse, and then select the SmoothProgressBar.dll file you created in the Create a Custom ProgressBar control section.

D, click OK. You can see that there are already smoothprogressbar controls in the Toolbox.

E, drag an instance of the two Smoothprogressbar control from toolbox to the default form in the Windows application project.

4. Drag a Timer control from the Toolbox page onto the form.

5, add the following code to the Tick event of the Timer control:

if (This.smoothProgressBar1.Value > 0)
{
this.smoothprogressbar1.value--;
this.smoothprogressbar2.value++;
}
Else
{
this.timer1.Enabled = false;
}

6. Drag a Button control from the Toolbox page onto the form.

7, add the following code to the Click event of the Button control:

This.smoothProgressBar1.Value = 100;
This.smoothProgressBar2.Value = 0;

This.timer1.Interval = 1;
This.timer1.Enabled = true;

8. In the Debug menu, click Start to run the sample project.

9, click button. Watch the two progress indicators. One gradually decreases, the other gradually increases.



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.