ScrollBarRendererとは? わかりやすく解説

ScrollBarRenderer クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

visual スタイルを使用してスクロール バー コントロールを描画するメソッドを用意します。このクラスは継承できません。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public NotInheritable Class
 ScrollBarRenderer
Dim instance As ScrollBarRenderer
public sealed class ScrollBarRenderer
public ref class ScrollBarRenderer sealed
public final class ScrollBarRenderer
public final class ScrollBarRenderer
解説解説

ScrollBarRenderer クラスには、オペレーティング システムの現在の visual スタイルを使用してスクロール バー コントロールを描画するために使用できる、一連の static メソッドが用意されています。コントロールの描画とは、コントロールのユーザー インターフェイスを描画することです。これは、現在の visual スタイルの外観を持つカスタム コントロールを描画する場合に役立ちます。スクロール バーを描画するには、このクラスのメソッドを組み合わせて使用することにより、スクロール バーの各部分を描画します。

オペレーティング システムで visual スタイルが有効にされており、visual スタイルがアプリケーション ウィンドウのクライアント領域に適用されている場合、このクラスのメソッドは現在の visual スタイルを使用してスクロール バーを描画します。それ以外の場合、このクラスのメソッドは InvalidOperationException をスローします。このクラスのメンバを使用できるかどうかを判断するには、IsSupported プロパティの値を確認します。

このクラスは、System.Windows.Forms.VisualStyles.VisualStyleElement.ScrollBar クラスが公開する要素のいずれかに設定された System.Windows.Forms.VisualStyles.VisualStyleRenderer の機能をラップします。詳細については、「visual スタイルが使用されているコントロールのレンダリング」を参照してください。

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 プラットフォームメモ : visual スタイルは、これらのプラットフォームでのみサポートされます。

使用例使用例

ScrollBarRenderer メソッドを使用するカスタム コントロールを記述することによって、マウス クリックに応答するスクロール バーを描画する方法を、次のコード例に示します。

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Namespace ScrollBarRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            Me.Size = New Size(500, 500)

            Dim Bar1 As New
 CustomScrollBar()
            Bar1.Location = New Point(50, 100)
            Bar1.Size = New Size(200, 20)

            Controls.Add(Bar1)
        End Sub

        <STAThread()> _
        Shared Sub 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())
        End Sub
    End Class

    Public Class CustomScrollBar
        Inherits Control

        Private clickedBarRectangle As Rectangle
        Private thumbRectangle As Rectangle
        Private leftArrowRectangle As Rectangle
        Private rightArrowRectangle As Rectangle
        Private leftArrowClicked As Boolean
 = False
        Private rightArrowClicked As Boolean
 = False
        Private leftBarClicked As Boolean
 = False
        Private rightBarClicked As Boolean
 = False
        Private thumbClicked As Boolean
 = False
        Private thumbState As ScrollBarState
 = ScrollBarState.Normal
        Private leftButtonState As ScrollBarArrowButtonState
 = _
            ScrollBarArrowButtonState.LeftNormal
        Private rightButtonState As ScrollBarArrowButtonState
 = _
            ScrollBarArrowButtonState.RightNormal

        ' This control does not allow these widths to be altered.
        Private thumbWidth As Integer
 = 15
        Private arrowWidth As Integer
 = 17

        ' Set the right limit of the thumb's right border.
        Private thumbRightLimitRight As Integer
 = 0

        ' Set the right limit of the thumb's left border.
        Private thumbRightLimitLeft As Integer
 = 0

        ' Set the left limit of thumb's left border.
        Private thumbLeftLimit As Integer
 = 0

        ' Set the distance from the left edge of the thumb to the 
        ' cursor tip.
        Private thumbPosition As Integer
 = 0

        ' Set the distance from the left edge of the scroll bar track
 to 
        ' the cursor tip.
        Private trackPosition As Integer
 = 0

        ' This timer draws the moving thumb while the scroll arrows
 or 
        ' track are pressed.
        Private WithEvents progressTimer As
 New Timer()

        Public Sub New()
            With Me
                .Location = New Point(10, 10)
                .Width = 200
                .Height = 20
                .DoubleBuffered = True
            End With
            SetUpScrollBar()
            progressTimer.Interval = 20
        End Sub

        ' Calculate the sizes of the scroll bar elements.
        Private Sub SetUpScrollBar()

            clickedBarRectangle = Me.ClientRectangle
            thumbRectangle = New Rectangle(ClientRectangle.X +
 _
                Me.ClientRectangle.Width / 2, Me.ClientRectangle.Y,
 _
                thumbWidth, Me.ClientRectangle.Height)
            leftArrowRectangle = New Rectangle(Me.ClientRectangle.X,
 _
                Me.ClientRectangle.Y, arrowWidth, Me.ClientRectangle.Height)
            rightArrowRectangle = New Rectangle(Me.ClientRectangle.Right
 - _
                arrowWidth, Me.ClientRectangle.Y, arrowWidth,
 _
                Me.ClientRectangle.Height)

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

            ' Set the right limit of the thumb's right border.
            thumbRightLimitRight = Me.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 = Me.ClientRectangle.X + arrowWidth
        End Sub

        ' Draw the scroll bar in its normal state.
        Protected Overrides Sub
 OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            ' Visual styles are not enabled.
            If Not ScrollBarRenderer.IsSupported
 Then
                Me.Parent.Text = "CustomScrollBar
 Disabled"
                Return
            End If

            Me.Parent.Text = "CustomScrollBar
 Enabled"

            ' Draw the scroll bar track.
            ScrollBarRenderer.DrawRightHorizontalTrack(e.Graphics, _
                Me.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 Then
                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.
            ElseIf rightBarClicked Then
                clickedBarRectangle.X = thumbRectangle.X + _
                    thumbRectangle.Width
                clickedBarRectangle.Width = thumbRightLimitRight - _
                    clickedBarRectangle.X
                ScrollBarRenderer.DrawRightHorizontalTrack(e.Graphics, _
                    clickedBarRectangle, ScrollBarState.Pressed)
            End If
        End Sub

        ' Handle a mouse click in the scroll bar.
        Protected Overrides Sub
 OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)

            If Not ScrollBarRenderer.IsSupported
 Then
                Return
            End If

            ' When the thumb is clicked, update the distance from the
 left
            ' edge of the thumb to the cursor tip.
            If thumbRectangle.Contains(e.Location) Then
                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.
            ElseIf leftArrowRectangle.Contains(e.Location) Then
                leftArrowClicked = True
                leftButtonState = ScrollBarArrowButtonState.LeftPressed
                progressTimer.Start()

            ' When the right arrow is clicked, start the timer to scroll
 
            ' while arrow is held down.
            ElseIf rightArrowRectangle.Contains(e.Location) Then
                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 < Me.thumbRectangle.X
 Then
                    leftBarClicked = True
                Else
                    rightBarClicked = True
                End If
                progressTimer.Start()
            End If

            Invalidate()
        End Sub

        ' Draw the track.
        Protected Overrides Sub
 OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseUp(e)

            If Not ScrollBarRenderer.IsSupported
 Then
                Return
            End If

            ' Update the thumb position, if the new location is within
 
            ' the bounds.
            If thumbClicked Then
                thumbClicked = False
                thumbState = ScrollBarState.Normal
                If e.Location.X > thumbLeftLimit + thumbPosition
 And _
                    e.Location.X < thumbRightLimitLeft + thumbPosition Then
                    thumbRectangle.X = e.Location.X - thumbPosition
                    thumbClicked = False
                End If

            ' If one of the four thumb movement areas was clicked, 
            ' stop the timer.
            ElseIf leftArrowClicked Then
                leftArrowClicked = False
                leftButtonState = ScrollBarArrowButtonState.LeftNormal
                progressTimer.Stop()

            ElseIf rightArrowClicked Then
                rightArrowClicked = False
                rightButtonState = ScrollBarArrowButtonState.RightNormal
                progressTimer.Stop()

            ElseIf leftBarClicked Then
                leftBarClicked = False
                progressTimer.Stop()

            ElseIf rightBarClicked Then
                rightBarClicked = False
                progressTimer.Stop()
            End If

            Invalidate()
        End Sub

        ' Track mouse movements if the user clicks on the scroll bar
 thumb.
        Protected Overrides Sub
 OnMouseMove(ByVal e As MouseEventArgs)
            MyBase.OnMouseMove(e)

            If Not ScrollBarRenderer.IsSupported
 Then
                Return
            End If

            ' Update the thumb position, if the new location is 
            ' within the bounds.
            If thumbClicked Then

                ' The thumb is all the way to the left.
                If e.Location.X <= thumbLeftLimit + thumbPosition
 Then
                    thumbRectangle.X = thumbLeftLimit

                ' The thumb is all the way to the right.
                ElseIf e.Location.X >= thumbRightLimitLeft
 + thumbPosition Then
                    thumbRectangle.X = thumbRightLimitLeft

                ' The thumb is between the ends of the track.
                Else
                    thumbRectangle.X = e.Location.X - thumbPosition
                End If

                Invalidate()

            End If
        End Sub

        ' Recalculate the sizes of the scroll bar elements.
        Protected Overrides Sub
 OnSizeChanged(ByVal e As EventArgs)
            MyBase.OnSizeChanged(e)
            SetUpScrollBar()
        End Sub

        ' Handle the timer tick by updating the thumb position.
        Private Sub progressTimer_Tick(ByVal
 sender As Object, _
            ByVal myEventArgs As EventArgs)
 Handles progressTimer.Tick

            If Not ScrollBarRenderer.IsSupported
 Then
                Return
            End If

            ' If an arrow is clicked, move the thumb in small increments.
            If rightArrowClicked And thumbRectangle.X
 < thumbRightLimitLeft Then
                thumbRectangle.X += 1

            ElseIf leftArrowClicked And thumbRectangle.X
 > thumbLeftLimit Then
                    thumbRectangle.X -= 1

            ' 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.
            ElseIf rightBarClicked And thumbRectangle.X
 < thumbRightLimitLeft _
                And thumbRectangle.X + thumbRectangle.Width <
 trackPosition Then
                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.
            ElseIf leftBarClicked And thumbRectangle.X
 > thumbLeftLimit And _
                thumbRectangle.X > trackPosition Then
                thumbRectangle.X -= 3
            End If

            Invalidate()
        End Sub

    End Class
End Namespace
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();
        }
    }
}
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::VisualStyles;

namespace ScrollBarRendererSample
{
    public ref class CustomScrollBar : public
 Control
    {
    private:
        Rectangle clickedBarRectangle;
    private:
        Rectangle thumbRectangle;
    private:
        Rectangle leftArrowRectangle;
    private:
        Rectangle rightArrowRectangle;
    private:
        bool leftArrowClicked;
    private:
        bool rightArrowClicked;
    private:
        bool leftBarClicked;
    private:
        bool rightBarClicked;
    private:
        bool thumbClicked;
    private:
        ScrollBarState thumbState;
    private:
        ScrollBarArrowButtonState leftButtonState;
    private:
        ScrollBarArrowButtonState rightButtonState;
    private:
        int thumbWidth;
    private:
        int arrowWidth;

        // The right limit of the thumb's right border.
    private:
        int thumbRightLimitRight;

        // The right limit of the thumb's left border.
    private:
        int thumbRightLimitLeft;

        // The left limit of thumb's left border.
    private:
        int thumbLeftLimit;

        // The distance from the left edge of the thumb to the
        // cursor tip.
    private:
        int thumbPosition;

        // The distance from the left edge of the scroll bar track
        // to the cursor tip.
    private:
        int trackPosition;

        // This timer draws the moving thumb while the scroll arrows
 or
        // track are pressed.
    private:
        Timer^ progressTimer;

    public:
        CustomScrollBar() : Control()
        {
            thumbState = ScrollBarState::Normal;
            leftButtonState = ScrollBarArrowButtonState::LeftNormal;
            rightButtonState = ScrollBarArrowButtonState::RightNormal;

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

            progressTimer = gcnew Timer();
            this->Location = Point(10, 10);
            this->Width = 200;
            this->Height = 20;
            this->DoubleBuffered = true;

            SetUpScrollBar();
            progressTimer->Interval = 20;
            progressTimer->Tick += 
                gcnew EventHandler(this, &CustomScrollBar::progressTimer_Tick);
        }

        // Calculate the sizes of the scroll bar elements.
    private:
        void SetUpScrollBar()
        {
            clickedBarRectangle = ClientRectangle;
            thumbRectangle = Rectangle(
                ClientRectangle.X + ClientRectangle.Width / 2,
                ClientRectangle.Y, thumbWidth,
                ClientRectangle.Height);
            leftArrowRectangle = Rectangle(
                ClientRectangle.X, ClientRectangle.Y,
                arrowWidth, ClientRectangle.Height);
            rightArrowRectangle = 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:
        virtual void OnPaint(PaintEventArgs^ e) override
        {
            __super::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:
        virtual void OnMouseDown(MouseEventArgs^ e) override 
        {
            __super::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:
        virtual void OnMouseUp(MouseEventArgs^ e) override
        {
            __super::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;
                }
            }

            // 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:
        virtual void OnMouseMove(MouseEventArgs^ e) override
        {
            __super::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:
        virtual void OnSizeChanged(EventArgs^ e) override
        {
            __super::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();
        }
    };


    public ref class Form1 : public
 Form
    {
    public:
        Form1() : Form()
        {
            this->Size = System::Drawing::Size(500, 500);

            CustomScrollBar^ sampleCustomScrollBar = gcnew CustomScrollBar();
            sampleCustomScrollBar->Location = Point(50, 100);
            sampleCustomScrollBar->Size = System::Drawing::Size(200, 20);

            Controls->Add(sampleCustomScrollBar);
        }
    };
}

[STAThread]
int 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(gcnew ScrollBarRendererSample::Form1());
}
継承階層継承階層
System.Object
  System.Windows.Forms.ScrollBarRenderer
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ScrollBarRenderer メンバ
System.Windows.Forms 名前空間
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement

ScrollBarRenderer プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ IsSupported visual スタイルを使用してスクロール バーを描画するために、ScrollBarRenderer クラスを使用できるかどうかを示す値を取得または設定します。
参照参照

関連項目

ScrollBarRenderer クラス
System.Windows.Forms 名前空間
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement

ScrollBarRenderer メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド DrawArrowButton visual スタイルを使用してスクロール バーの矢印ボタンを描画します。
パブリック メソッド DrawHorizontalThumb visual スタイルを使用して、水平方向のスクロール ボックス (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawHorizontalThumbGrip visual スタイルを使用して、水平方向のスクロール ボックス (つまみとも呼ばれる) 上にグリップを描画します。
パブリック メソッド DrawLeftHorizontalTrack visual スタイルを使用して水平スクロール バーのトラックを描画します。
パブリック メソッド DrawLowerVerticalTrack visual スタイルを使用して垂直スクロール バーのトラックを描画します。
パブリック メソッド DrawRightHorizontalTrack visual スタイルを使用して水平スクロール バーのトラックを描画します。
パブリック メソッド DrawSizeBox visual スタイルを使用してスクロール バーのサイズ変更ハンドルを描画します。
パブリック メソッド DrawUpperVerticalTrack visual スタイルを使用して垂直スクロール バーのトラックを描画します。
パブリック メソッド DrawVerticalThumb visual スタイルを使用して、垂直方向のスクロール ボックス (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawVerticalThumbGrip visual スタイルを使用して、垂直方向のスクロール ボックス (つまみとも呼ばれる) 上にグリップを描画します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetSizeBoxSize サイズ変更ハンドルのサイズを返します。
パブリック メソッド GetThumbGripSize スクロール ボックスのグリップのサイズを返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

ScrollBarRenderer クラス
System.Windows.Forms 名前空間
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement

ScrollBarRenderer メンバ

visual スタイルを使用してスクロール バー コントロールを描画するメソッドを用意します。このクラスは継承できません。

ScrollBarRenderer データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ IsSupported visual スタイルを使用してスクロール バーを描画するために、ScrollBarRenderer クラスを使用できるかどうかを示す値を取得または設定します。
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド DrawArrowButton visual スタイルを使用してスクロール バーの矢印ボタンを描画します。
パブリック メソッド DrawHorizontalThumb visual スタイルを使用して、水平方向のスクロール ボックス (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawHorizontalThumbGrip visual スタイルを使用して、水平方向のスクロール ボックス (つまみとも呼ばれる) 上にグリップを描画します。
パブリック メソッド DrawLeftHorizontalTrack visual スタイルを使用して水平スクロール バーのトラックを描画します。
パブリック メソッド DrawLowerVerticalTrack visual スタイルを使用して垂直スクロール バーのトラックを描画します。
パブリック メソッド DrawRightHorizontalTrack visual スタイルを使用して水平スクロール バーのトラックを描画します。
パブリック メソッド DrawSizeBox visual スタイルを使用してスクロール バーのサイズ変更ハンドルを描画します。
パブリック メソッド DrawUpperVerticalTrack visual スタイルを使用して垂直スクロール バーのトラックを描画します。
パブリック メソッド DrawVerticalThumb visual スタイルを使用して、垂直方向のスクロール ボックス (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawVerticalThumbGrip visual スタイルを使用して、垂直方向のスクロール ボックス (つまみとも呼ばれる) 上にグリップを描画します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetSizeBoxSize サイズ変更ハンドルのサイズを返します。
パブリック メソッド GetThumbGripSize スクロール ボックスのグリップのサイズを返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

ScrollBarRenderer クラス
System.Windows.Forms 名前空間
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ScrollBarRenderer」の関連用語

ScrollBarRendererのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



ScrollBarRendererのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS