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;
}
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 ();
}
}
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.
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.