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 build 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 in Visual C # A Windows Control Library item is created in .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, 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) (max - min); rectangle Rect = this.clientRectangle; // Calculate Area For Drawing The Progress. Rect.width = (int); // Draw the program (brushRectangle (brush, rect); // Draw A Three-Dimensional Border Around THE CONTOL 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 stock in Range if (Val // Make Sure. If (val> max) {val = max;} // invalidate the control to get a repaint. This.invalidate ();}} public int value {get {return val } Set {int} {val {val {val} else {val} else {val}} else} {val}} else} {val} 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); newValueRect.width = (int)); // use an old value to calculate the Rectangle for Progress. percent = (float) (Float) OldValue - min) / (float) (MAX - min); OldValueRect.width = (int) ((float) OldValueRect.Width * P ercent); 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, 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, 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.