CheckBoxRenderer CheckBoxRenderer CheckBoxRenderer CheckBoxRenderer Class

定義

visual スタイルを使用して、または使用せずに、チェック ボックス コントロールを描画するメソッドを用意します。Provides methods used to render a check box control with or without visual styles. このクラスは継承できません。This class cannot be inherited.

public ref class CheckBoxRenderer sealed
public sealed class CheckBoxRenderer
type CheckBoxRenderer = class
Public NotInheritable Class CheckBoxRenderer
継承
CheckBoxRendererCheckBoxRendererCheckBoxRendererCheckBoxRenderer

次のコード例を使用するカスタム コントロールを記述する方法を示します、DrawCheckBoxのマウス クリックに応答する チェック ボックスを描画するメソッド。The following code example demonstrates how to write a custom control that uses the DrawCheckBox method to draw a check box that responds to mouse clicks.

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

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

namespace CheckBoxRendererSample
{
    ref class CustomCheckBox : Control
    {
        private:
        Rectangle textRectangleValue;
        Point clickedLocationValue;
        bool clicked;
        CheckBoxState state;

        public :
        CustomCheckBox() : Control()
        {
            this->textRectangleValue = Rectangle();
            this->clickedLocationValue = Point();
            this->clicked = false;
            this->state = CheckBoxState::UncheckedNormal;
            this->Location = Point(50, 50);
            this->Size = System::Drawing::Size(100, 20);
            this->Text = "Click here";
            this->Font = SystemFonts::IconTitleFont;
        }

        // Calculate the text bounds, exluding the check box.
        Rectangle getTextRectangle()
        {
            Graphics ^g = this->CreateGraphics();
            textRectangleValue.X = ClientRectangle.X +
                        CheckBoxRenderer::GetGlyphSize(g,
                        CheckBoxState::UncheckedNormal).Width;
            textRectangleValue.Y = ClientRectangle.Y;
            textRectangleValue.Width = ClientRectangle.Width -
                        CheckBoxRenderer::GetGlyphSize(g,
                        CheckBoxState::UncheckedNormal).Width;
            textRectangleValue.Height = ClientRectangle.Height;

            delete g;
            return textRectangleValue;
        }

protected:
        // Draw the check box in the current state.
        virtual void OnPaint(PaintEventArgs ^e) override
        {
            Control::OnPaint(e);

            CheckBoxRenderer::DrawCheckBox(e->Graphics,
                ClientRectangle.Location, this->getTextRectangle(), this->Text,
                this->Font, TextFormatFlags::HorizontalCenter,
                clicked, state);
        }


        // Draw the check box in the checked or unchecked state, alternately.
        virtual void OnMouseDown(MouseEventArgs ^e) override
        {
            Control::OnMouseDown(e);

            if (!clicked)
            {
                clicked = true;
                this->Text = "Clicked!";
                state = CheckBoxState::CheckedPressed;
                Invalidate();
            }
            else
            {
                clicked = false;
                this->Text = "Click here";
                state = CheckBoxState::UncheckedNormal;
                Invalidate();
            }
        }

        // Draw the check box in the hot state. 
        virtual void OnMouseHover(EventArgs ^e) override
        {
            Control::OnMouseHover(e);
            state = clicked ? CheckBoxState::CheckedHot :
                CheckBoxState::UncheckedHot;
            Invalidate();
        }

        // Draw the check box in the hot state. 
        virtual void OnMouseUp(MouseEventArgs ^e) override
        {
            Control::OnMouseUp(e);
            this->OnMouseHover(e);
        }

        // Draw the check box in the unpressed state.
        virtual void OnMouseLeave(EventArgs ^e) override
        {
            Control::OnMouseLeave(e);
            state = clicked ? CheckBoxState::CheckedNormal :
                CheckBoxState::UncheckedNormal;
            Invalidate();
        } 
    }; 

    ref class Form1: public Form
    {
    public:
        Form1() : Form() 
        {
            CustomCheckBox ^CheckBox1 = gcnew CustomCheckBox();
            Controls->Add(CheckBox1);

            if (Application::RenderWithVisualStyles) 
            {
                this->Text = "Visual Styles Enabled";
            }
            else 
            {
                this->Text = "Visual Styles Disabled";
            }
        }
    };
}


[STAThread]
int main()
{
    // If you do not call EnableVisualStyles below, then 
    // CheckBoxRenderer.DrawCheckBox automatically detects 
    // this and draws the check box without visual styles.
    Application::EnableVisualStyles();
    Application::Run(gcnew CheckBoxRendererSample::Form1());
}
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace CheckBoxRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            CustomCheckBox CheckBox1 = new CustomCheckBox();
            Controls.Add(CheckBox1);

            if (Application.RenderWithVisualStyles)
                this.Text = "Visual Styles Enabled";
            else
                this.Text = "Visual Styles Disabled";
        }

        [STAThread]
        static void Main()
        {
            // If you do not call EnableVisualStyles below, then 
            // CheckBoxRenderer.DrawCheckBox automatically detects 
            // this and draws the check box without visual styles.
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

    public class CustomCheckBox : Control
    {
        private Rectangle textRectangleValue = new Rectangle();
        private Point clickedLocationValue = new Point();
        private bool clicked = false;
        private CheckBoxState state = CheckBoxState.UncheckedNormal;

        public CustomCheckBox()
            : base()
        {
            this.Location = new Point(50, 50);
            this.Size = new Size(100, 20);
            this.Text = "Click here";
            this.Font = SystemFonts.IconTitleFont;
        }

        // Calculate the text bounds, exluding the check box.
        public Rectangle TextRectangle
        {
            get
            {
                using (Graphics g = this.CreateGraphics())
                {
                    textRectangleValue.X = ClientRectangle.X +
                        CheckBoxRenderer.GetGlyphSize(g,
                        CheckBoxState.UncheckedNormal).Width;
                    textRectangleValue.Y = ClientRectangle.Y;
                    textRectangleValue.Width = ClientRectangle.Width -
                        CheckBoxRenderer.GetGlyphSize(g,
                        CheckBoxState.UncheckedNormal).Width;
                    textRectangleValue.Height = ClientRectangle.Height;
                }

                return textRectangleValue;
            }
        }

        // Draw the check box in the current state.
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            CheckBoxRenderer.DrawCheckBox(e.Graphics,
                ClientRectangle.Location, TextRectangle, this.Text,
                this.Font, TextFormatFlags.HorizontalCenter,
                clicked, state);
        }

        // Draw the check box in the checked or unchecked state, alternately.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (!clicked)
            {
                clicked = true;
                this.Text = "Clicked!";
                state = CheckBoxState.CheckedPressed;
                Invalidate();
            }
            else
            {
                clicked = false;
                this.Text = "Click here";
                state = CheckBoxState.UncheckedNormal;
                Invalidate();
            }
        }

        // Draw the check box in the hot state. 
        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            state = clicked ? CheckBoxState.CheckedHot :
                CheckBoxState.UncheckedHot;
            Invalidate();
        }

        // Draw the check box in the hot state. 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            this.OnMouseHover(e);
        }

        // Draw the check box in the unpressed state.
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            state = clicked ? CheckBoxState.CheckedNormal :
                CheckBoxState.UncheckedNormal;
            Invalidate();
        }
    }
}
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Namespace CheckBoxRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            Dim CheckBox1 As New CustomCheckBox()
            Controls.Add(CheckBox1)

            If Application.RenderWithVisualStyles Then
                Me.Text = "Visual Styles Enabled"
            Else
                Me.Text = "Visual Styles Disabled"
            End If
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' If you do not call EnableVisualStyles below, then 
            ' CheckBoxRenderer.DrawCheckBox automatically detects   
            ' this and draws the check box without visual styles.
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub
    End Class

    Public Class CustomCheckBox
        Inherits Control

        Private textRectangleValue As New Rectangle()
        Private clickedLocationValue As New Point()
        Private clicked As Boolean = False
        Private state As CheckBoxState = CheckBoxState.UncheckedNormal

        Public Sub New()
            With Me
                .Location = New Point(50, 50)
                .Size = New Size(100, 20)
                .Text = "Click here"
                .Font = SystemFonts.IconTitleFont
            End With
        End Sub

        ' Calculate the text bounds, exluding the check box.
        Public ReadOnly Property TextRectangle() As Rectangle
            Get
                Using g As Graphics = Me.CreateGraphics()
                    With textRectangleValue
                        .X = Me.ClientRectangle.X + _
                            CheckBoxRenderer.GetGlyphSize(g, _
                            CheckBoxState.UncheckedNormal).Width
                        .Y = Me.ClientRectangle.Y
                        .Width = Me.ClientRectangle.Width - _
                            CheckBoxRenderer.GetGlyphSize(g, _
                            CheckBoxState.UncheckedNormal).Width
                        .Height = Me.ClientRectangle.Height
                    End With
                End Using
                Return textRectangleValue
            End Get
        End Property

        ' Draw the check box in the current state.
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)
            CheckBoxRenderer.DrawCheckBox(e.Graphics, _
                Me.ClientRectangle.Location, TextRectangle, Me.Text, _
                Me.Font, TextFormatFlags.HorizontalCenter, _
                clicked, state)
        End Sub

        ' Draw the check box in the checked or unchecked state, alternately.
        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)
            If Not clicked Then
                With Me
                    .clicked = True
                    .Text = "Clicked!"
                    .state = CheckBoxState.CheckedPressed
                End With
                Invalidate()
            Else
                With Me
                    .clicked = False
                    .Text = "Click here"
                    .state = CheckBoxState.UncheckedNormal
                End With
                Invalidate()
            End If
        End Sub

        ' Draw the check box in the hot state. 
        Protected Overrides Sub OnMouseHover(ByVal e As EventArgs)
            MyBase.OnMouseHover(e)
            If clicked Then
                state = CheckBoxState.CheckedHot
            Else
                state = CheckBoxState.UncheckedHot
            End If
            Invalidate()
        End Sub

        ' Draw the check box in the hot state. 
        Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseUp(e)
            Me.OnMouseHover(e)
        End Sub

        ' Draw the check box in the unpressed state.
        Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
            MyBase.OnMouseLeave(e)
            If clicked Then
                state = CheckBoxState.CheckedNormal
            Else
                state = CheckBoxState.UncheckedNormal
            End If
            Invalidate()
        End Sub

    End Class
End Namespace

注釈

CheckBoxRendererクラスのセットを提供するstaticチェック ボックス コントロールを表示するために使用できるメソッド。The CheckBoxRenderer class provides a set of static methods that can be used to render a check box control. コントロールのレンダリングとは、コントロールのユーザー インターフェイスを描画することを意味します。Rendering a control refers to drawing the user interface of a control. チェック ボックスを描画するのいずれかの操作を使用して、DrawCheckBoxメソッド。To draw a check box, use one of the DrawCheckBox methods. これらのメソッドは、さまざまな描画テキストまたはイメージのチェック ボックスなどのオプションを提供します。These methods provide a variety of options, such as drawing text or an image with the check box.

オペレーティング システムで visual スタイルが有効になっていて、現在のアプリケーションに visual スタイルが適用DrawCheckBox現在の visual スタイルでチェック ボックスを描画します。If visual styles are enabled in the operating system and visual styles are applied to the current application, DrawCheckBox will draw the check box with the current visual style. それ以外の場合、DrawCheckBox従来の Windows スタイルでチェック ボックスを描画します。Otherwise, DrawCheckBox will draw the check box with the classic Windows style. これは、オペレーティング システムの現在の visual スタイルの設定を自動的に一致する必要があるカスタム コントロールを描画する場合に便利です。This is useful if you are drawing a custom control that should automatically match the current visual style setting of the operating system.

このクラスの機能をラップする、System.Windows.Forms.VisualStyles.VisualStyleRendererによって公開されている要素のいずれかに設定されている、System.Windows.Forms.VisualStyles.VisualStyleElement.Button.CheckBoxクラス。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.Button.CheckBox class. 詳細については、次を参照してください。 Visual スタイルを使用しているコントロールのレンダリングします。For more information, see Rendering Controls with Visual Styles.

プロパティ

RenderMatchingApplicationState RenderMatchingApplicationState RenderMatchingApplicationState RenderMatchingApplicationState

レンダラーがアプリケーション状態に応じて描画スタイルを決定するかどうかを示す値を取得または設定します。Gets or sets a value indicating whether the renderer uses the application state to determine rendering style.

メソッド

DrawCheckBox(Graphics, Point, CheckBoxState) DrawCheckBox(Graphics, Point, CheckBoxState) DrawCheckBox(Graphics, Point, CheckBoxState) DrawCheckBox(Graphics, Point, CheckBoxState)

状態と位置を指定してチェック ボックス コントロールを描画します。Draws a check box control in the specified state and location.

DrawCheckBox(Graphics, Point, Rectangle, String, Font, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, Boolean, CheckBoxState)

状態と位置、使用するテキスト、およびオプションでフォーカスを示す四角形を指定して、チェック ボックス コントロールを描画します。Draws a check box control in the specified state and location, with the specified text, and with an optional focus rectangle.

DrawCheckBox(Graphics, Point, Rectangle, String, Font, Image, Rectangle, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, Image, Rectangle, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, Image, Rectangle, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, Image, Rectangle, Boolean, CheckBoxState)

状態と位置、使用するテキストとイメージ、およびオプションでフォーカスを示す四角形を指定して、チェック ボックス コントロールを描画します。Draws a check box control in the specified state and location, with the specified text and image, and with an optional focus rectangle.

DrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Boolean, CheckBoxState)

状態と位置、使用するテキストとテキストの書式指定、およびオプションでフォーカスを示す四角形を指定して、チェック ボックス コントロールを描画します。Draws a check box control in the specified state and location, with the specified text and text formatting, and with an optional focus rectangle.

DrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, CheckBoxState) DrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, CheckBoxState)

状態と位置、使用するテキスト、テキストの書式指定、イメージ、およびオプションでフォーカスを示す四角形を指定して、チェック ボックス コントロールを描画します。Draws a check box control in the specified state and location; with the specified text, text formatting, and image; and with an optional focus rectangle.

DrawParentBackground(Graphics, Rectangle, Control) DrawParentBackground(Graphics, Rectangle, Control) DrawParentBackground(Graphics, Rectangle, Control) DrawParentBackground(Graphics, Rectangle, Control)

指定領域にコントロールの親の背景を描画します。Draws the background of a control's parent in the specified area.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetGlyphSize(Graphics, CheckBoxState) GetGlyphSize(Graphics, CheckBoxState) GetGlyphSize(Graphics, CheckBoxState) GetGlyphSize(Graphics, CheckBoxState)

チェック ボックスのグリフのサイズを返します。Returns the size of the check box glyph.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
IsBackgroundPartiallyTransparent(CheckBoxState) IsBackgroundPartiallyTransparent(CheckBoxState) IsBackgroundPartiallyTransparent(CheckBoxState) IsBackgroundPartiallyTransparent(CheckBoxState)

チェック ボックスの背景の一部が半透明である (アルファ ブレンドされている) かどうかを示します。Indicates whether the background of the check box has semitransparent or alpha-blended pieces.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

適用対象

こちらもご覧ください