ScrollBarRenderer 类

定义

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

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

示例

下面的代码示例演示如何创建自定义控件,该控件使用 ScrollBarRenderer 方法来绘制响应鼠标单击的滚动条。

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

namespace ScrollBarRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            this.Size = new Size(500, 500);

            CustomScrollBar Bar1 = new CustomScrollBar();
            Bar1.Location = new Point(50, 100);
            Bar1.Size = new Size(200, 20);

            Controls.Add(Bar1);
        }

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

    public class CustomScrollBar : Control
    {
        private Rectangle clickedBarRectangle;
        private Rectangle thumbRectangle;
        private Rectangle leftArrowRectangle;
        private Rectangle rightArrowRectangle;
        private bool leftArrowClicked = false;
        private bool rightArrowClicked = false;
        private bool leftBarClicked = false;
        private bool rightBarClicked = false;
        private bool thumbClicked = false;
        private ScrollBarState thumbState = ScrollBarState.Normal;
        private ScrollBarArrowButtonState leftButtonState =
            ScrollBarArrowButtonState.LeftNormal;
        private ScrollBarArrowButtonState rightButtonState =
            ScrollBarArrowButtonState.RightNormal;

        // This control does not allow these widths to be altered.
        private int thumbWidth = 15;
        private int arrowWidth = 17;

        // Set the right limit of the thumb's right border.
        private int thumbRightLimitRight = 0;

        // Set the right limit of the thumb's left border.
        private int thumbRightLimitLeft = 0;

        // Set the left limit of thumb's left border.
        private int thumbLeftLimit = 0;

        // Set the distance from the left edge of the thumb to the 
        // cursor tip.
        private int thumbPosition = 0;

        // Set the distance from the left edge of the scroll bar track 
        // to the cursor tip.
        private int trackPosition = 0;

        // This timer draws the moving thumb while the scroll arrows or 
        // track are pressed.
        private Timer progressTimer = new Timer();

        public CustomScrollBar()
            : base()
        {
            this.Location = new Point(10, 10);
            this.Width = 200;
            this.Height = 20;
            this.DoubleBuffered = true;

            SetUpScrollBar();
            progressTimer.Interval = 20;
            progressTimer.Tick += new EventHandler(progressTimer_Tick);
        }

        // Calculate the sizes of the scroll bar elements.
        private void SetUpScrollBar()
        {
            clickedBarRectangle = ClientRectangle;
            thumbRectangle = new Rectangle(
                ClientRectangle.X + ClientRectangle.Width / 2,
                ClientRectangle.Y, thumbWidth,
                ClientRectangle.Height);
            leftArrowRectangle = new Rectangle(
                ClientRectangle.X, ClientRectangle.Y,
                arrowWidth, ClientRectangle.Height);
            rightArrowRectangle = new Rectangle(
                ClientRectangle.Right - arrowWidth,
                ClientRectangle.Y, arrowWidth,
                ClientRectangle.Height);

            // Set the default starting thumb position.
            thumbPosition = thumbWidth / 2;

            // Set the right limit of the thumb's right border.
            thumbRightLimitRight = ClientRectangle.Right - arrowWidth;

            // Set the right limit of the thumb's left border.
            thumbRightLimitLeft = thumbRightLimitRight - thumbWidth;

            // Set the left limit of the thumb's left border.
            thumbLeftLimit = ClientRectangle.X + arrowWidth;
        }

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

            // Visual styles are not enabled.
            if (!ScrollBarRenderer.IsSupported)
            {
                this.Parent.Text = "CustomScrollBar Disabled";
                return;
            }

            this.Parent.Text = "CustomScrollBar Enabled";

            // Draw the scroll bar track.
            ScrollBarRenderer.DrawRightHorizontalTrack(e.Graphics,
                ClientRectangle, ScrollBarState.Normal);

            // Draw the thumb and thumb grip in the current state.
            ScrollBarRenderer.DrawHorizontalThumb(e.Graphics,
                thumbRectangle, thumbState);
            ScrollBarRenderer.DrawHorizontalThumbGrip(e.Graphics,
                thumbRectangle, thumbState);

            // Draw the scroll arrows in the current state.
            ScrollBarRenderer.DrawArrowButton(e.Graphics,
                    leftArrowRectangle, leftButtonState);
            ScrollBarRenderer.DrawArrowButton(e.Graphics,
                    rightArrowRectangle, rightButtonState);

            // Draw a highlighted rectangle in the left side of the scroll 
            // bar track if the user has clicked between the left arrow 
            // and thumb.
            if (leftBarClicked)
            {
                clickedBarRectangle.X = thumbLeftLimit;
                clickedBarRectangle.Width = thumbRectangle.X - thumbLeftLimit;
                ScrollBarRenderer.DrawLeftHorizontalTrack(e.Graphics,
                    clickedBarRectangle, ScrollBarState.Pressed);
            }

            // Draw a highlighted rectangle in the right side of the scroll 
            // bar track if the user has clicked between the right arrow 
            // and thumb.
            else if (rightBarClicked)
            {
                clickedBarRectangle.X =
                    thumbRectangle.X + thumbRectangle.Width;
                clickedBarRectangle.Width =
                    thumbRightLimitRight - clickedBarRectangle.X;
                ScrollBarRenderer.DrawRightHorizontalTrack(e.Graphics,
                    clickedBarRectangle, ScrollBarState.Pressed);
            }
        }

        // Handle a mouse click in the scroll bar.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (!ScrollBarRenderer.IsSupported)
                return;

            // When the thumb is clicked, update the distance from the left
            // edge of the thumb to the cursor tip.
            if (thumbRectangle.Contains(e.Location))
            {
                thumbClicked = true;
                thumbPosition = e.Location.X - thumbRectangle.X;
                thumbState = ScrollBarState.Pressed;
            }

            // When the left arrow is clicked, start the timer to scroll 
            // while the arrow is held down.
            else if (leftArrowRectangle.Contains(e.Location))
            {
                leftArrowClicked = true;
                leftButtonState = ScrollBarArrowButtonState.LeftPressed;
                progressTimer.Start();
            }

            // When the right arrow is clicked, start the timer to scroll 
            // while the arrow is held down.
            else if (rightArrowRectangle.Contains(e.Location))
            {
                rightArrowClicked = true;
                rightButtonState = ScrollBarArrowButtonState.RightPressed;
                progressTimer.Start();
            }

            // When the scroll bar is clicked, start the timer to move the
            // thumb while the mouse is held down.
            else
            {
                trackPosition = e.Location.X;

                if (e.Location.X < this.thumbRectangle.X)
                {
                    leftBarClicked = true;
                }
                else
                {
                    rightBarClicked = true;
                }
                progressTimer.Start();
            }

            Invalidate();
        }

        // Draw the track.
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (!ScrollBarRenderer.IsSupported)
                return;

            // Update the thumb position, if the new location is within 
            // the bounds.
            if (thumbClicked)
            {
                thumbClicked = false;
                thumbState = ScrollBarState.Normal;

                if (e.Location.X > (thumbLeftLimit + thumbPosition) &&
                    e.Location.X < (thumbRightLimitLeft + thumbPosition))
                {
                    thumbRectangle.X = e.Location.X - thumbPosition;
                    thumbClicked = false;
                }
            }

            // If one of the four thumb movement areas was clicked, 
            // stop the timer.
            else if (leftArrowClicked)
            {
                leftArrowClicked = false;
                leftButtonState = ScrollBarArrowButtonState.LeftNormal;
                progressTimer.Stop();
            }

            else if (rightArrowClicked)
            {
                rightArrowClicked = false;
                rightButtonState = ScrollBarArrowButtonState.RightNormal;
                progressTimer.Stop();
            }

            else if (leftBarClicked)
            {
                leftBarClicked = false;
                progressTimer.Stop();
            }

            else if (rightBarClicked)
            {
                rightBarClicked = false;
                progressTimer.Stop();
            }

            Invalidate();
        }

        // Track mouse movements if the user clicks on the scroll bar thumb.
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (!ScrollBarRenderer.IsSupported)
                return;

            // Update the thumb position, if the new location is 
            // within the bounds.
            if (thumbClicked)
            {
                // The thumb is all the way to the left.
                if (e.Location.X <= (thumbLeftLimit + thumbPosition))
                {
                    thumbRectangle.X = thumbLeftLimit;
                }

                // The thumb is all the way to the right.
                else if (e.Location.X >= (thumbRightLimitLeft + thumbPosition))
                {
                    thumbRectangle.X = thumbRightLimitLeft;
                }

                // The thumb is between the ends of the track.
                else
                {
                    thumbRectangle.X = e.Location.X - thumbPosition;
                }

                Invalidate();
            }
        }

        // Recalculate the sizes of the scroll bar elements.
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            SetUpScrollBar();
        }

        // Handle the timer tick by updating the thumb position.
        private void progressTimer_Tick(object sender, EventArgs myEventArgs)
        {
            if (!ScrollBarRenderer.IsSupported)
                return;

            // If an arrow is clicked, move the thumb in small increments.
            if (rightArrowClicked && thumbRectangle.X < thumbRightLimitLeft)
            {
                thumbRectangle.X++;
            }
            else if (leftArrowClicked && thumbRectangle.X > thumbLeftLimit)
            {
                thumbRectangle.X--;
            }

            // If the track bar to right of the thumb is clicked, move the 
            // thumb to the right in larger increments until the right edge 
            // of the thumb hits the cursor.
            else if (rightBarClicked &&
                thumbRectangle.X < thumbRightLimitLeft &&
                thumbRectangle.X + thumbRectangle.Width < trackPosition)
            {
                thumbRectangle.X += 3;
            }

            // If the track bar to left of the thumb is clicked, move the 
            // thumb to the left in larger increments until the left edge 
            // of the thumb hits the cursor.
            else if (leftBarClicked &&
                thumbRectangle.X > thumbLeftLimit &&
                thumbRectangle.X > trackPosition)
            {
                thumbRectangle.X -= 3;
            }

            Invalidate();
        }
    }
}

注解

ScrollBarRenderer 提供了一组 static 方法,可用于呈现具有操作系统的当前视觉样式的滚动条控件。 呈现控件是指绘制控件的用户界面。 如果要绘制应具有当前视觉样式外观的自定义控件,这非常有用。 若要绘制滚动条,请结合使用此类中的方法来呈现滚动条的每个部分。

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

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

属性

IsSupported

获取一个值,该值指示是否可使用 ScrollBarRenderer 类以视觉样式绘制滚动条。

方法

DrawArrowButton(Graphics, Rectangle, ScrollBarArrowButtonState)

使用视觉样式绘制滚动箭头。

DrawHorizontalThumb(Graphics, Rectangle, ScrollBarState)

以视觉样式绘制水平滚动框(又称滚动块)。

DrawHorizontalThumbGrip(Graphics, Rectangle, ScrollBarState)

以视觉样式在水平滚动框(又称滚动块)上绘制手柄。

DrawLeftHorizontalTrack(Graphics, Rectangle, ScrollBarState)

以视觉样式绘制水平滚动条轨道。

DrawLowerVerticalTrack(Graphics, Rectangle, ScrollBarState)

以视觉样式绘制垂直滚动条轨道。

DrawRightHorizontalTrack(Graphics, Rectangle, ScrollBarState)

以视觉样式绘制水平滚动条轨道。

DrawSizeBox(Graphics, Rectangle, ScrollBarSizeBoxState)

以视觉样式绘制滚动条大小调整手柄。

DrawUpperVerticalTrack(Graphics, Rectangle, ScrollBarState)

以视觉样式绘制垂直滚动条轨道。

DrawVerticalThumb(Graphics, Rectangle, ScrollBarState)

以视觉样式绘制垂直滚动框(又称滚动块)。

DrawVerticalThumbGrip(Graphics, Rectangle, ScrollBarState)

以视觉样式在垂直滚动框(又称滚动块)上绘制手柄。

GetSizeBoxSize(Graphics, ScrollBarState)

返回大小调整手柄的大小。

GetThumbGripSize(Graphics, ScrollBarState)

返回滚动框手柄的大小。

适用于

产品 版本
.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

另请参阅