ProgressBarRenderer 类

定义

提供用于以视觉样式呈现进度栏控件的方法。 此类不能被继承。

C#
public sealed class ProgressBarRenderer
C#
public static class ProgressBarRenderer
继承
ProgressBarRenderer

示例

下面的代码示例演示如何创建一个自定义控件,该控件使用 DrawVerticalBarDrawVerticalChunks 方法来绘制垂直进度条。 控件使用 来 Timer 重新绘制进度栏,每秒添加一个部分。 方法 SetupProgressBar 使用 ChunkThicknessChunkSpaceThickness 属性来计算所绘制的每个逐渐较大的矩形的高度。

C#
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");
            }
        }
    }
}

注解

ProgressBarRenderer 提供了一组 static 方法,可用于呈现具有操作系统的当前视觉样式的进度栏控件。 呈现控件是指绘制控件的用户界面。 如果要绘制应具有当前视觉样式外观的自定义控件,这非常有用。 若要绘制进度栏,请使用 DrawHorizontalBarDrawVerticalBar 方法绘制空条形图,然后使用 DrawHorizontalChunksDrawVerticalChunks 方法绘制填充该条的元素。

如果在操作系统中启用了视觉样式,并且视觉样式应用于应用程序窗口的工作区,则此类的方法将使用当前视觉样式绘制进度栏。 否则,此类的方法和属性将引发 InvalidOperationException。 若要确定是否可以使用此类的成员,可以检查 属性的值 IsSupported

此类包装 System.Windows.Forms.VisualStyles.VisualStyleRenderer 设置为 、System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.BarVerticalSystem.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.ChunkSystem.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.ChunkVertical 类公开的元素之一的 System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.Bar的功能。 有关详细信息,请参阅使用视觉样式呈现控件

属性

ChunkSpaceThickness

获取进度栏每个内部格之间的间距的宽度(以像素为单位)。

ChunkThickness

获取进度栏的单个内部格的宽度(以像素为单位)。

IsSupported

获取一个值,该值指示 ProgressBarRenderer 类是否可用于以视觉样式绘制进度栏控件。

方法

DrawHorizontalBar(Graphics, Rectangle)

绘制一个水平填充的空白进度栏控件。

DrawHorizontalChunks(Graphics, Rectangle)

绘制填充水平进度栏的一组进度栏格。

DrawVerticalBar(Graphics, Rectangle)

绘制一个垂直填充的空白进度栏控件。

DrawVerticalChunks(Graphics, Rectangle)

绘制填充垂直进度栏的一组进度栏格。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅