Create a smoothed progress bar in Visual C # .NET

xiaoxiao2021-03-06  54

This article summarizes the establishment of a customized schedule control to establish a simple client application

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

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

In the previous schedule control version, for example, in the Microsoft Windows Common Controls ActiveX control, you can see two different views of the progress bar. You can set a Standard view or a Smooth view by setting the scrolling property. The SMOoth view provides a region to smoothing the progress, and the Standard tries to indicate the progress by one block.

The schedule control provided in Visual C # .NET supports the Standard view.

The code sample of this article reveals how to establish a control with the following properties:

Minimum. This attribute represents the minimum value of the progress bar. By default 0; you cannot set this property to a negative value. Maximum. This attribute represents the maximum value of the progress bar. By default 100. Value. This attribute represents the current value of the progress bar. This value must be between Minimum and Maximum. Progressbarcolor. This attribute represents the color of the progress bar. return------------------------------------------------- -------------------------------

Create a custom schedule control 1, press the following steps, create a Windows Control Library project in Visual C # .NET:

a, open Microsoft Visual Studio .NET.

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

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

D. In the Name box, fill in SmoothProgressBar and click OK.

e, in Project Explorer, rename the default class module, change userControl1.cs to SmoothProgressBar.cs.

f, in the Property 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 add new features. However, ProgressBar is a seal, and it cannot be inherited. Therefore, you must build this control from the beginning.

Add the following code to the UserControl module, followed by "Windows Form Designer Generated Code":

INT min = 0; // minimum value for progress Rangeint Max = 100; // maximum value for progress Rangeint Val = 0; // Current ProgressColor 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 what the minimum value is never set higher Than THE MAXIMUM VALUE. IF (Value> max) {min = value; min = Value;} // ensure value is still in in Range if (val

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

Public int maximum {get {return max;}

Set {// make sure what the maximum value is never set limited to the minimum value. if (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 max) {val = max;} else {val = value;}

// invalidate online; 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) NEWVALECT.WIDTH * Percent;

// Use an old value to calculate the rectangle for progress. Percent = (float) / (float) (MAX - min); OldValueRect.width = ((float) olDValueeect.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; updatecture.width = OldValueRect.Width;}

Updateect.height = this.height;

// invalidate the intertion region only. This.invalidate (UpdateERECT);}}

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 Build Solution to compile the entire project.

return------------------------------------------------- -------------------------------

Create 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 Type, click on Windows Application in 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, then select the SmoothProgressBar.dll file created in the Create A Custom ProgressBar Control section.

D, click OK. You can see that there is already a SmoothProgressBar control in Toolbox.

E. Drag from Toolbox's instances of the SMOothProgressBar control to the default form in the Windows Application project.

4. Drag a Timer Control to the Form from the Toolbox page.

5. Add the following code to the TIMER control Tick event:

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 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 item.

9. Click on Button. Pay attention to the two progress indicators. One gradually decreased, and the other gradually increased.

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

New Post(0)