Click here to Skip to main content
6,822,123 members and growing! (18,068 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Gradient and Boxed ProgressBar

By Sarafian

A base ProgressBar Control for the Implementation of a Gradient and Blocked
C#, Windows, .NET, Visual-Studio, Dev
Posted:12 Jan 2007
Views:18,318
Bookmarked:14 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.96 Rating: 3.25 out of 5
1 vote, 25.0%
1

2
1 vote, 25.0%
3
1 vote, 25.0%
4
1 vote, 25.0%
5

Sample Image - GradientProgressBar.jpg

Introduction



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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sarafian


Member
Alex Sarafian lives in Athens Greece and currently is working with Business scale application with .NET latest technologies

He has been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 began working for an R&D institute where he was introduced to C# which he worships ever since.

He has recently created a development blog http://sarafianalex.wordpress.com/

He loves core applications development and in his spare time he usualy "wastes" time in front of his media center pc watching sitcoms, preferable SCI-FI.
He wants to play chess but he can't find any real world players to hang out with.
Occupation: Software Developer (Senior)
Company: Orama Hellas
Location: Greece Greece

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralSorry PinmemberSarafian0:02 28 Jan '08  
Generalcannot load into VS2005, crashed Pinmemberyoke22:46 27 Jan '08  
GeneralRe: cannot load into VS2005, crashed PinmemberSarafian0:29 28 Jan '08  
GeneralNice PinmemberKarl Shifflett3:17 21 Dec '07  
GeneralRe: Nice PinmemberSarafian0:30 28 Jan '08  
GeneralGraphics Flicker Pinmemberantodona7:14 29 Mar '07  
GeneralRe: Graphics Flicker PinmemberSarafian0:31 28 Jan '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 12 Jan 2007
Editor:
Copyright 2007 by Sarafian
Everything else Copyright © CodeProject, 1999-2010
Web10 | Advertise on the Code Project