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

TrackBarRenderer クラス

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

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

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

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

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

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

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

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

使用例使用例

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

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


Namespace TrackBarRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            Dim TrackBar1 As New
 CustomTrackBar(19, New Size(300, 50))
            Me.Width = 500
            Me.Controls.Add(TrackBar1)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' Note that the call to EnableVisualStyles below does
            ' not affect whether TrackBarRenderer.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

    Class CustomTrackBar
        Inherits Control
        Private numberTicks As Integer
 = 10
        Private trackRectangle As New
 Rectangle()
        Private ticksRectangle As New
 Rectangle()
        Private thumbRectangle As New
 Rectangle()
        Private currentTickPosition As Integer
 = 0
        Private tickSpace As Single
 = 0
        Private thumbClicked As Boolean
 = False
        Private thumbState As TrackBarThumbState
 = TrackBarThumbState.Normal

        Public Sub New(ByVal
 ticks As Integer, ByVal
 trackBarSize As Size)

            With Me
                .Location = New Point(10, 10)
                .Size = trackBarSize
                .numberTicks = ticks
                .BackColor = Color.DarkCyan
                .DoubleBuffered = True
            End With

            ' Calculate the initial sizes of the bar, 
            ' thumb and ticks.
            SetupTrackBar()
        End Sub

        ' Calculate the sizes of the bar, thumb, and ticks rectangle.
        Private Sub SetupTrackBar()
            If Not TrackBarRenderer.IsSupported
 Then
                Return
            End If
            Using g As Graphics = Me.CreateGraphics()
                ' Calculate the size of the track bar.
                trackRectangle.X = ClientRectangle.X + 2
                trackRectangle.Y = ClientRectangle.Y + 28
                trackRectangle.Width = ClientRectangle.Width - 4
                trackRectangle.Height = 4

                ' Calculate the size of the rectangle in which to 
                ' draw the ticks.
                ticksRectangle.X = trackRectangle.X + 4
                ticksRectangle.Y = trackRectangle.Y - 8
                ticksRectangle.Width = trackRectangle.Width - 8
                ticksRectangle.Height = 4

                tickSpace = (CSng(ticksRectangle.Width) - 1) / _
                    (CSng(numberTicks) - 1)

                ' Calculate the size of the thumb.
                thumbRectangle.Size = _
                    TrackBarRenderer.GetTopPointingThumbSize( _
                    g, TrackBarThumbState.Normal)

                thumbRectangle.X = CurrentTickXCoordinate()
                thumbRectangle.Y = trackRectangle.Y - 8
            End Using
        End Sub

        Private Function CurrentTickXCoordinate()
 As Integer
            If tickSpace = 0 Then
                Return 0
            Else
                Return CInt(Math.Round(tickSpace)) * currentTickPosition
            End If
        End Function

        ' Draw the track bar.
        Protected Overrides Sub
 OnPaint(ByVal e As PaintEventArgs)
            If Not TrackBarRenderer.IsSupported
 Then
                Me.Parent.Text = "CustomTrackBar
 Disabled"
                Return
            End If

            Me.Parent.Text = "CustomTrackBar
 Enabled"
            TrackBarRenderer.DrawHorizontalTrack(e.Graphics, _
                trackRectangle)
            TrackBarRenderer.DrawTopPointingThumb(e.Graphics, _
                thumbRectangle, thumbState)
            TrackBarRenderer.DrawHorizontalTicks(e.Graphics, _
                ticksRectangle, numberTicks, EdgeStyle.Raised)
        End Sub

        ' Determine whether the user has clicked the track bar thumb.
        Protected Overrides Sub
 OnMouseDown(ByVal e As MouseEventArgs)
            If Not TrackBarRenderer.IsSupported
 Then
                Return
            End If
            If Me.thumbRectangle.Contains(e.Location)
 Then
                thumbClicked = True
                thumbState = TrackBarThumbState.Pressed
            End If

            Me.Invalidate()
        End Sub

        ' Redraw the track bar thumb if the user has moved it.
        Protected Overrides Sub
 OnMouseUp(ByVal e As MouseEventArgs)
            If Not TrackBarRenderer.IsSupported
 Then
                Return
            End If
            If thumbClicked = True Then
                If e.Location.X > trackRectangle.X And
 _
                    e.Location.X < trackRectangle.X + _
                    trackRectangle.Width - thumbRectangle.Width Then

                    thumbClicked = False
                    thumbState = TrackBarThumbState.Hot
                    Me.Invalidate()
                End If
                thumbClicked = False
            End If
        End Sub

        ' Track cursor movements.
        Protected Overrides Sub
 OnMouseMove(ByVal e As MouseEventArgs)
            If Not TrackBarRenderer.IsSupported
 Then
                Return
            End If
            ' The user is moving the thumb.
            If thumbClicked = True Then

                ' Track movements to the next tick to the right, if
 the
                ' cursor has moved halfway to the next tick.
                If currentTickPosition < numberTicks - 1 And
 _
                    e.Location.X > CurrentTickXCoordinate() + _
                    CInt(tickSpace) Then
                    currentTickPosition += 1

                ' Track movements to the next tick to the left, if 
                ' the cursor has moved halfway to the next tick.
                Else
                    If currentTickPosition > 0 And
 _
                        e.Location.X < CurrentTickXCoordinate() - _
                        CInt(tickSpace / 2) Then
                        currentTickPosition -= 1
                    End If
                End If
                thumbRectangle.X = CurrentTickXCoordinate()

            ' The cursor is passing over the track.
            Else
                If thumbRectangle.Contains(e.Location) Then
                    thumbState = TrackBarThumbState.Hot
                Else
                    thumbState = TrackBarThumbState.Normal
                End If
            End If

            Invalidate()
        End Sub

    End Class
End Namespace
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace TrackBarRendererSample
{
    class Form1 : Form
    {
        public Form1()
        {
            CustomTrackBar TrackBar1 = new CustomTrackBar(19,
                new Size(300, 50));
            this.Width = 500;
            this.Controls.Add(TrackBar1);
        }

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

    class CustomTrackBar : Control
    {
        private int numberTicks = 10;
        private Rectangle trackRectangle = new
 Rectangle();
        private Rectangle ticksRectangle = new
 Rectangle();
        private Rectangle thumbRectangle = new
 Rectangle();
        private int currentTickPosition = 0;
        private float tickSpace = 0;
        private bool thumbClicked = false;
        private TrackBarThumbState thumbState =
            TrackBarThumbState.Normal;

        public CustomTrackBar(int ticks, Size
 trackBarSize)
        {
            this.Location = new Point(10, 10);
            this.Size = trackBarSize;
            this.numberTicks = ticks;
            this.BackColor = Color.DarkCyan;
            this.DoubleBuffered = true;

            // Calculate the initial sizes of the bar, 
            // thumb and ticks.
            SetupTrackBar();
        }

        // Calculate the sizes of the bar, thumb, and ticks rectangle.
        private void SetupTrackBar()
        {
            if (!TrackBarRenderer.IsSupported)
                return;

            using (Graphics g = this.CreateGraphics())
            {
                // Calculate the size of the track bar.
                trackRectangle.X = ClientRectangle.X + 2;
                trackRectangle.Y = ClientRectangle.Y + 28;
                trackRectangle.Width = ClientRectangle.Width - 4;
                trackRectangle.Height = 4;

                // Calculate the size of the rectangle in which to 
                // draw the ticks.
                ticksRectangle.X = trackRectangle.X + 4;
                ticksRectangle.Y = trackRectangle.Y - 8;
                ticksRectangle.Width = trackRectangle.Width - 8;
                ticksRectangle.Height = 4;

                tickSpace = ((float)ticksRectangle.Width - 1)
 /
                    ((float)numberTicks - 1);

                // Calculate the size of the thumb.
                thumbRectangle.Size =
                    TrackBarRenderer.GetTopPointingThumbSize(g,
                    TrackBarThumbState.Normal);

                thumbRectangle.X = CurrentTickXCoordinate();
                thumbRectangle.Y = trackRectangle.Y - 8;
            }
        }

        private int CurrentTickXCoordinate()
        {
            if (tickSpace == 0)
            {
                return 0;
            }
            else
            {
                return ((int)Math.Round(tickSpace)
 *
                    currentTickPosition);
            }
        }

        // Draw the track bar.
        protected override void OnPaint(PaintEventArgs
 e)
        {
            if (!TrackBarRenderer.IsSupported)
            {
                this.Parent.Text = "CustomTrackBar Disabled";
                return;
            }

            this.Parent.Text = "CustomTrackBar Enabled";
            TrackBarRenderer.DrawHorizontalTrack(e.Graphics,
                trackRectangle);
            TrackBarRenderer.DrawTopPointingThumb(e.Graphics,
                thumbRectangle, thumbState);
            TrackBarRenderer.DrawHorizontalTicks(e.Graphics,
                ticksRectangle, numberTicks, EdgeStyle.Raised);
        }

        // Determine whether the user has clicked the track bar thumb.
        protected override void OnMouseDown(MouseEventArgs
 e)
        {
            if (!TrackBarRenderer.IsSupported)
                return;

            if (this.thumbRectangle.Contains(e.Location))
            {
                thumbClicked = true;
                thumbState = TrackBarThumbState.Pressed;
            }

            this.Invalidate();
        }

        // Redraw the track bar thumb if the user has moved it.
        protected override void OnMouseUp(MouseEventArgs
 e)
        {
            if (!TrackBarRenderer.IsSupported)
                return;

            if (thumbClicked == true)
            {
                if (e.Location.X > trackRectangle.X &&
                    e.Location.X < (trackRectangle.X +
                    trackRectangle.Width - thumbRectangle.Width))
                {
                    thumbClicked = false;
                    thumbState = TrackBarThumbState.Hot;
                    this.Invalidate();
                }

                thumbClicked = false;
            }
        }

        // Track cursor movements.
        protected override void OnMouseMove(MouseEventArgs
 e)
        {
            if (!TrackBarRenderer.IsSupported)
                return;

            // The user is moving the thumb.
            if (thumbClicked == true)
            {
                // Track movements to the next tick to the right, if
 
                // the cursor has moved halfway to the next tick.
                if (currentTickPosition < numberTicks - 1 &&
                    e.Location.X > CurrentTickXCoordinate() +
                    (int)(tickSpace))
                {
                    currentTickPosition++;
                }

                // Track movements to the next tick to the left, if
 
                // cursor has moved halfway to the next tick.
                else if (currentTickPosition
 > 0 &&
                    e.Location.X < CurrentTickXCoordinate() -
                    (int)(tickSpace / 2))
                {
                    currentTickPosition--;
                }

                thumbRectangle.X = CurrentTickXCoordinate();
            }

            // The cursor is passing over the track.
            else
            {
                thumbState = thumbRectangle.Contains(e.Location) ?
                    TrackBarThumbState.Hot : TrackBarThumbState.Normal;
            }

            Invalidate();
        }
    }
}
継承階層継承階層
System.Object
  System.Windows.Forms.TrackBarRenderer
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TrackBarRenderer メンバ
System.Windows.Forms 名前空間
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement

TrackBarRenderer プロパティ


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

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

関連項目

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

TrackBarRenderer メソッド


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

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド DrawBottomPointingThumb visual スタイルを使用して、下向きのトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawHorizontalThumb visual スタイルを使用して、水平方向のトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawHorizontalTicks visual スタイルを使用して、指定した数の水平トラック バーの目盛りを描画します。
パブリック メソッド DrawHorizontalTrack visual スタイルを使用して、水平トラック バーのトラックを描画します。
パブリック メソッド DrawLeftPointingThumb visual スタイルを使用して、左向きのトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawRightPointingThumb visual スタイルを使用して、右向きのトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawTopPointingThumb visual スタイルを使用して、上向きのトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawVerticalThumb visual スタイルを使用して、垂直方向のトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawVerticalTicks visual スタイルを使用して、指定した数の垂直トラック バーの目盛りを描画します。
パブリック メソッド DrawVerticalTrack visual スタイルを使用して、垂直トラック バーのトラックを描画します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド GetBottomPointingThumbSize 下向きのトラック バーのスライダ (つまみとも呼ばれる) のサイズをピクセル単位で返します。
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetLeftPointingThumbSize 左向きのトラック バーのスライダ (つまみとも呼ばれる) のサイズをピクセル単位で返します。
パブリック メソッド GetRightPointingThumbSize 右向きのトラック バーのスライダ (つまみとも呼ばれる) のサイズをピクセル単位で返します。
パブリック メソッド GetTopPointingThumbSize 上向きのトラック バーのスライダ (つまみとも呼ばれる) のサイズをピクセル単位で返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

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

TrackBarRenderer メンバ

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

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


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ IsSupported visual スタイルを使用してトラック バーを描画するために、TrackBarRenderer クラスを使用できるかどうかを示す値を取得または設定します。
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド DrawBottomPointingThumb visual スタイルを使用して、下向きのトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawHorizontalThumb visual スタイルを使用して、水平方向のトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawHorizontalTicks visual スタイルを使用して、指定した数の水平トラック バーの目盛りを描画します。
パブリック メソッド DrawHorizontalTrack visual スタイルを使用して、水平トラック バーのトラックを描画します。
パブリック メソッド DrawLeftPointingThumb visual スタイルを使用して、左向きのトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawRightPointingThumb visual スタイルを使用して、右向きのトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawTopPointingThumb visual スタイルを使用して、上向きのトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawVerticalThumb visual スタイルを使用して、垂直方向のトラック バーのスライダ (つまみとも呼ばれる) を描画します。
パブリック メソッド DrawVerticalTicks visual スタイルを使用して、指定した数の垂直トラック バーの目盛りを描画します。
パブリック メソッド DrawVerticalTrack visual スタイルを使用して、垂直トラック バーのトラックを描画します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド GetBottomPointingThumbSize 下向きのトラック バーのスライダ (つまみとも呼ばれる) のサイズをピクセル単位で返します。
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetLeftPointingThumbSize 左向きのトラック バーのスライダ (つまみとも呼ばれる) のサイズをピクセル単位で返します。
パブリック メソッド GetRightPointingThumbSize 右向きのトラック バーのスライダ (つまみとも呼ばれる) のサイズをピクセル単位で返します。
パブリック メソッド GetTopPointingThumbSize 上向きのトラック バーのスライダ (つまみとも呼ばれる) のサイズをピクセル単位で返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

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



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「TrackBarRenderer」の関連用語

TrackBarRendererのお隣キーワード

   

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



TrackBarRendererのページの著作権

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

©2026 GRAS Group, Inc.RSS