Use Visual C # To create a smooth progress bar

Source: Internet
Author: User
In the earlier version of the progress bar control, for example, the version 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 attribute. The smooth view provides an area for smooth display of the Progress. Standard tries to show that the progress is represented by a block.

The progress bar control provided in Visual C #. Net only supports standard views.

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

Minimum. This attribute indicates the minimum value of the progress bar. The default value is 0. You cannot set this attribute to a negative value.

Maximum. This attribute indicates the maximum value of the progress bar. The default value is 100.

Value. This attribute indicates the current value of the progress bar. The value must be between minimum and maximum.

Progressbarcolor. This attribute indicates the color of the progress bar.

  Create a custom progress bar Control

1. Follow these steps to create a Windows control library project in Visual C #. Net:

A. Open Microsoft Visual Studio. NET.

B. Click the File menu, new, and project.

C. In the new project dialog box, select Visual C # projects in project types, and then select windows control library in 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 Property Window of the usercontrol object, change its name attribute from usercontrol1 to smoothprogressbar.

2. Now, you have inherited a new class from the control class and can add new functions. However, the progress bar is sealed and cannot be inherited. Therefore, you must create this control from scratch.

Add the following code to the usercontrol module 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-dimen=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 that the 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 that the maximum value is never set lower than the minimum value.
If (value <min)
{
Min = value;
}

Max = value;

// Make sure that 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 that the 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 an 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 be 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. Click build solution in the build menu to compile the entire project.

  Create a simple client application

1. Click new in the File menu, and then click Project.

2. In the Add new project dialog box, click Visual C # projects in project types, click windows application in templates, and click OK.

3. follow the steps below to add two smoothprogressbar instances to form:

A. On the Tools menu, click customize toolbox.

B. Click the. NET Framework components page.

C. Click Browse and select the smoothprogressbar. dll file created in the create a custom progressbar Control Section.

D. Click OK. You can see that the smoothprogressbar control already exists in the toolbox.

E. Drag two smoothprogressbar control instances from toolbox to the default form in the Windows application project.

4. Drag a timer control from the Toolbox page to 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 to 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. Click Start in the Debug menu to run the sample project.

9. click the button. Observe the two progress indicators. One gradually decreases and the other gradually increases.

Related Article

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.