![]() |
Desktop Development »
Miscellaneous »
General
Intermediate
Gradient and Boxed ProgressBarBy SarafianA base ProgressBar Control for the Implementation of a Gradient and Blocked |
C#, Windows, .NET, Visual Studio, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

One basic limitation of ProgressBar Control provided by .Net is that
it can't show the percentage on top of its rendered Background. Given this
limitation i decided to create my own Control as an exersize. But
System.Windows.Forms.ProgressBar does not allow extra drawing to be done so the
new control must be created from scratch.
So I created a basic
ProgressBar which has the basic Properties
Minimum,Maximum,Value,Step and an inner function PerformsStep
which advances the Value appropriatly.
Apearance
Properties are provided for the aspects of Color,Border and the Percentage Text.
The TextColorType specifies whether the text color used is the
ForeColor or it is automatically generated based relatively to the
background color that will be behind it.
Also Rolling functionality is
supported but it should not be used because it uses a Timer
rather than a thread.I was created for testing purposes only.
Also based on a tip I read, a Custom Dictionary of Brush and Pen is used
from MyComponents.Objects namespace in order to maintain in memory every Bursh
and Pen used. These dictionary's are optional to use and are automatically
disposed.
This base control can be used in forms but its drawing
is premetiv and resembles progressbars of Win3.1.
Rendering is done
by the following functions.
DrawBackgroundRolling,DrawBackground,DrawText,DrawBorder
DrawBackgroundRolling
instead of DrawBackground is called when Rolling is enabled.
Any
Custom ProgressBar that wants to use the functionality must override the
implementation of these functions which are called from the OnPaint
event in specific order and only if they are needed based on the relevant
properties.
The first custom Progressbar I build was one with gradient between the
color of the progress and the background.
Two Properties are introduced
for this ProgressBarGradient
PercentageWidth specifies how much of the control's width will
be used for the gradience transition. GradientType specifies
whether the PercentageWidth is 100% or specified.
Here is
the Code for DrawBackground
protected override void DrawBackground(Graphics g)
{
if (_iPercent == 0)
{
FillRectangle(g, ClientRectangle, xBrushes);
//return;
}
Rectangle[] xRecs = null;
xRecs = Prepare3Parts();
Rectangle xRectLeft = xRecs[0];
Rectangle xRectMiddle = xRecs[1];
Rectangle xRectRight = xRecs[2];
xRectLeft.Width++;
BrushAndFill(g, xRectMiddle, _ColorProgress, BackColor);
FillRectangle(g, xRectLeft, xBrushes);
FillRectangle(g, xRectRight, xBrushes);
}
Prepare3Parts creates the left,middle and right rectangles
needed to draw the control.
The second Custom ProgressBar called ProgressBarBoxedand splits
the control to boxes and fills them with the appropriate color relative to
current percent.The properties introduced are NumberOfBlocks
specifies the number of blocks to split the Progressbar.
ActiveBlockColor specifies the last color of the box if the
percantage is between 1 and 99. InnerGridType specifies the
type of grid used to separate the boxes.
Here is the implementation of
DrawBackground
protected override void DrawBackground(Graphics g)
{
if (_iPercent == 0)
{
FillRectangle(g, ClientRectangle, xBrushes);
}
for (int i = 0; i < _MaxBlockToDraw; i++)
{
FillRectangle(g, _BlockRects[i], xBrushes);
}
if(_MaxBlockToDraw<_NumberOfBlocks&&_MaxBlockToDraw>-1)
FillRectangle(g, _BlockRects[_MaxBlockToDraw], xBrushes);
DrawInnerGrid(g);
}
Because the appearance of this control wan't change at every change of
Value a trick is implemented to speed up the control.The base
ProgressBar provides the functionallity by implementing the
Invalide() of the control like this
protected bool _TurnOffInvalidation;
protected new void Invalidate()
{
if (!_TurnOffInvalidation)
base.Invalidate();
_TurnOffInvalidation = false;
}
The FormProgressBarsTest created to test the three
variations of these ProgressBars is derived form
<CODE><CODE>MyComponents.Windows.BaseForms.PropertyGridForm
which is a form i have created to provide functionallity for test
forms of controls i create.
Finally in the Solution there is Project name Functions which i store every function that may be resud in my projects.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 Jan 2007 Editor: |
Copyright 2007 by Sarafian Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |