How to: Create a Smooth Progress Bar in Visual C # .NET

xiaoxiao2021-03-06  109

How to: Create a Smooth Progress Bar in Visual C # .NET

Article ID: 323116last Review: November 25, 2002Revision: 1.0

This Article Was Previously Published Under Q323116

For a Microsoft Visual Basic .NET VERSION OF This Article, See

.

In this task

Summary

• Create a Custom ProgressBar Control • Create A Sample Client Application

Summary

This Article Demonstrates How To Create A Simple, Custom

UserControl to Create a Smooth, Scroling

ProgressBar Control.

In Earlier Versions of The

ProgressBar control, such as the version that is provided with the Microsoft Windows Common Controls ActiveX control, you can view the progress in two different views. To control these views, you use the

Scrolling property, which includes standard and smooth settings. Smooth scrolling produces a solid block of color that represents the progress, and standard scrolling appears segmented and is made up of a series of small blocks or rectangles.

THE

ProgressBar Control That Is Included with Visual C # .NET Supports Only The Standard Setting.

The Sample Code in this Article Illustrates How To Create A Control That Supports the Following Properties:

. • Minimum This property obtains or sets the lower value for the range of valid values ​​for progress The default value of this property is zero (0);. You can not set this property to a negative value • Maximum This property obtains or sets.. the upper value for the range of valid values ​​for progress. The default value of this property is 100. • value. This property obtains or sets the current level of progress. The value must be in the range that the Minimum and the Maximum properties define • ProgressBarcolor. This Property Obtains Or Sets The Color of The Progress Bar.

Back to the top

Create a Custom ProgressBar Control1.Follow Thase Steps to create a New Windows Control Library Project In Visual C # .NET:

a. Start Microsoft Visual Studio .NET.b. On the File menu, point to New, and then click Project.c. In the New Project dialog box, click Visual C # Projects under Project Types, and then click Windows Control Library under Templates .d. In the Name box, type SmoothProgressBar, and then click OK.e. In Project Explorer, rename the default class module from UserControl1.cs to SmoothProgressBar.cs.f. In the Properties window for the UserControl object, change the Name property from UserControl1 to SmoothProgressBar.2.At this point, you typically inherit from the class of that control and then add the additional functionality to extend an existing control. However, the ProgressBar class is sealed and can not be inherited. Therefore, you must build THE control from the beginning.add The following code to the class module of the userControl, Just After the "Windows Form Designer Generated Code" section: 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.

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 in

IF (Val

{

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.

Value

{

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.

Value

{

Val = min;

}

Else IF (Value> max)

{

Val = max;

}

Else

{

Val = value;

}

// invalidate Only The Changed Area.

Float percent;

Rectangle newValueRect = this.clientRectangle;

Rectangle OldValueERECT = this.clientRectangle;

// Use a new value to calculate The Rectangle for Progress.

Percent = (float) (VAL - min) / (float) (MAX - min);

NEWVALUERECT.WIDTH = (INT) NEWVALEERECT.WIDTH * Percent;

// Use an old value to Calculate The Rectangle for Progress.

Percent = (float) (OldValue - MIN) / (FLOAT) (MAX - min);

OldValueRect.Width = (int) ((float) ((Float) OldValueRect.width * percent);

Rectangle updateRect = new rectangle (); // find Only the part of the screen thing must be updated.

IF (NewValueRect.Width> OldValueRect.width)

{

UpdateRect.x = OldValueERECT.SIZE.WIDTH;

UpdateRect.width = newValueRect.width - OldValueRect.width;

}

Else

{

UpdateRect.x = newValueRect.size.width;

UpdateRect.width = OldValueRect.width - newValueRect.width;

}

Updateect.height = this.height;

// invalidate the interSection region.

this.invalidate;

}

}

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.Heft, this.clientRectangle.Height - Penwidth));

g.drawline (Pens.White,

New point (this.clientRectangle.Heft, 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. On The Build Menu, Click Build Solution To Compile The Project.

Back to the top

Create a Sample Client Application

1.On the File menu, point to New, and then click Project.2.In the Add New Project dialog box, click Visual C # Projects under Project Types, click Windows Application under Templates, and then click OK.3.Follow these steps to add two instances of the SmoothProgressBar control to the form:.. a On the Tools menu, click Customize Toolbox.b Click the .NET Framework Components tab.c. Click Browse, and then locate the SmoothProgressBar.dll file, which you created in the "Create a Custom ProgressBar Control" section.d. Click OK. Notice that the SmoothProgressBar control is added to the toolbox.e. Drag two instances of the SmoothProgressBar control from the toolbox to the default form of the Windows Application project.4 .Drag a Timer Control from the Toolbox to the form.5.add The folow code to the Tick Event of the timer control: IF (this.smoothprogressbar1.value> 0)

{

THIS.SMOTHPROGRESSBAR1.VALUE -

this.smoothProgressBar2.Value ;

}

Else

{

this.timer1.enabled = false;

}

6. Drag a Button Control from the Toolbox to 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.On the Debug menu, click Start to run the sample project.9.Click the button. Notice that the two progress indicators display the text "progress". One progress indicator displays the progress in an increasing manner, and the other progress indicator Displays the progress in a Decreasing OR a countdown manner.

转载请注明原文地址:https://www.9cbs.com/read-103018.html

New Post(0)