Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ProgressBarRenderer Class

Provides methods used to render a progress bar control with visual styles. This class cannot be inherited.

System.Object
  System.Windows.Forms.ProgressBarRenderer

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public sealed class ProgressBarRenderer

The ProgressBarRenderer type exposes the following members.

  NameDescription
Public propertyStatic memberChunkSpaceThicknessGets the width, in pixels, of the space between each inner piece of the progress bar.
Public propertyStatic memberChunkThicknessGets the width, in pixels, of a single inner piece of the progress bar.
Public propertyStatic memberIsSupportedGets a value indicating whether the ProgressBarRenderer class can be used to draw a progress bar control with visual styles.
Top
  NameDescription
Public methodStatic memberDrawHorizontalBarDraws an empty progress bar control that fills in horizontally.
Public methodStatic memberDrawHorizontalChunksDraws a set of progress bar pieces that fill a horizontal progress bar.
Public methodStatic memberDrawVerticalBarDraws an empty progress bar control that fills in vertically.
Public methodStatic memberDrawVerticalChunksDraws a set of progress bar pieces that fill a vertical progress bar.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The ProgressBarRenderer class provides a set of static methods that can be used to render a progress bar control with the current visual style of the operating system. Rendering a control refers to drawing the user interface of a control. This is useful if you are drawing a custom control that should have the appearance of the current visual style. To draw a progress bar, use the DrawHorizontalBar or DrawVerticalBar methods to draw the empty bar, and then use the DrawHorizontalChunks or DrawVerticalChunks methods to draw the elements that fill in the bar.

If visual styles are enabled in the operating system and visual styles are applied to the client area of application windows, the methods of this class will draw the progress bar with the current visual style. Otherwise, the methods and properties of this class will throw an InvalidOperationException. To determine whether the members of this class can be used, you can check the value of the IsSupported property.

This class wraps the functionality of a System.Windows.Forms.VisualStyles.VisualStyleRenderer that is set to one of the elements exposed by the System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.Bar, System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.BarVertical, System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.Chunk, and System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.ChunkVertical classes. For more information, see Rendering Controls with Visual Styles.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are supported only on these platforms.

The following code example demonstrates how to create a custom control that uses the DrawVerticalBar and DrawVerticalChunks methods to draw a vertical progress bar. The control uses a Timer to redraw the progress bar with an added piece each second. The SetupProgressBar method uses the ChunkThickness and ChunkSpaceThickness properties to calculate the height of each progressively larger rectangle that is drawn.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace ProgressBarRendererSample
{
    public class Form1 : Form
    {
        private VerticalProgressBar bar1 = new VerticalProgressBar();
        private Button button1 = new Button();

        public Form1()
            : base()
        {
            this.Size = new Size(500, 500);
            bar1.NumberChunks = 30;
            button1.Location = new Point(150, 10);
            button1.Size = new Size(150, 30);
            button1.Text = "Start VerticalProgressBar";
            button1.Click += new EventHandler(button1_Click);
            Controls.AddRange(new Control[] { button1, bar1 });
        }

        [STAThread]
        public static void Main()
        {
            // The call to EnableVisualStyles below does not affect 
            // whether ProgressBarRenderer.IsSupported is true; as  
            // long as visual styles are enabled by the operating system,  
            // IsSupported is true.
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        // Start the VerticalProgressBar. 
        private void button1_Click(object sender, EventArgs e)
        {
            bar1.Start();
        }
    }

    public class VerticalProgressBar : Control
    {
        private int numberChunksValue;
        private int ticks;
        private Timer progressTimer = new Timer();
        private Rectangle[] progressBarRectangles;

        public VerticalProgressBar()
            : base()
        {
            this.Location = new Point(10, 10);
            this.Width = 50;

            // The progress bar will update every second.
            progressTimer.Interval = 1000;
            progressTimer.Tick += new EventHandler(progressTimer_Tick);

            // This property also calls SetupProgressBar to initialize  
            // the progress bar rectangles if styles are enabled.
            NumberChunks = 20;

            // Set the default height if visual styles are not enabled. 
            if (!ProgressBarRenderer.IsSupported)
            {
                this.Height = 100;
            }
        }

        // Specify the number of progress bar chunks to base the height on. 
        public int NumberChunks
        {
            get
            {
                return numberChunksValue;
            }

            set
            {
                if (value <= 50 && value > 0)
                {
                    numberChunksValue = value;
                }
                else
                {
                    MessageBox.Show("Number of chunks must be between " +
                        "0 and 50; defaulting to 10");
                    numberChunksValue = 10;
                }

                // Recalculate the progress bar size, if visual styles  
                // are active. 
                if (ProgressBarRenderer.IsSupported)
                {
                    SetupProgressBar();
                }
            }
        }

        // Draw the progress bar in its normal state. 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (ProgressBarRenderer.IsSupported)
            {
                ProgressBarRenderer.DrawVerticalBar(e.Graphics,
                    ClientRectangle);
                this.Parent.Text = "VerticalProgressBar Enabled";
            }
            else
            {
                this.Parent.Text = "VerticalProgressBar Disabled";
            }
        }

        // Initialize the rectangles used to paint the states of the  
        // progress bar. 
        private void SetupProgressBar()
        {
            if (!ProgressBarRenderer.IsSupported)
            {
                return;
            }

            // Determine the size of the progress bar frame. 
            this.Size = new Size(ClientRectangle.Width,
                (NumberChunks) * (ProgressBarRenderer.ChunkThickness +
                (2 * ProgressBarRenderer.ChunkSpaceThickness)) + 6);

            // Initialize the rectangles to draw each step of the  
            // progress bar.
            progressBarRectangles = new Rectangle[NumberChunks];

            for (int i = 0; i < NumberChunks; i++)
            {
                // Use the thickness defined by the current visual style  
                // to calculate the height of each rectangle. The size  
                // adjustments ensure that the chunks do not paint over  
                // the frame. 

                int filledRectangleHeight =
                    ((i + 1) * (ProgressBarRenderer.ChunkThickness +
                    (2 * ProgressBarRenderer.ChunkSpaceThickness)));

                progressBarRectangles[i] = new Rectangle(
                    ClientRectangle.X + 3,
                    ClientRectangle.Y + ClientRectangle.Height - 3
                    - filledRectangleHeight,
                    ClientRectangle.Width - 6,
                    filledRectangleHeight);
            }
        }

        // Handle the timer tick; draw each progressively larger rectangle. 
        private void progressTimer_Tick(Object myObject, EventArgs e)
        {
            if (ticks < NumberChunks)
            {
                using (Graphics g = this.CreateGraphics())
                {
                    ProgressBarRenderer.DrawVerticalChunks(g,
                        progressBarRectangles[ticks]);
                    ticks++;
                }
            }
            else
            {
                progressTimer.Enabled = false;
            }
        }

        // Start the progress bar. 
        public void Start()
        {
            if (ProgressBarRenderer.IsSupported)
            {
                progressTimer.Start();
            }
            else
            {
                MessageBox.Show("VerticalScrollBar requires visual styles");
            }
        }
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.