CheckBoxRenderer 클래스

정의

비주얼 스타일을 사용하거나 사용하지 않고 CheckBox 컨트롤을 렌더링하는 데 사용되는 메서드를 제공합니다. 이 클래스는 상속될 수 없습니다.

public ref class CheckBoxRenderer sealed
public ref class CheckBoxRenderer abstract sealed
public sealed class CheckBoxRenderer
public static class CheckBoxRenderer
type CheckBoxRenderer = class
Public NotInheritable Class CheckBoxRenderer
Public Class CheckBoxRenderer
상속
CheckBoxRenderer

예제

다음 코드 예제를 사용 하는 사용자 지정 컨트롤을 작성 하는 방법에 설명 합니다 DrawCheckBox 메서드를 마우스 클릭에 응답 하는 확인란을 그립니다.

#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.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 확인란 컨트롤을 렌더링 하는 데 사용할 수 있는 방법입니다. 컨트롤 렌더링이란 컨트롤의 사용자 인터페이스를 그리는 것을 말합니다. 확인란을 그릴 중 하나를 사용 합니다 DrawCheckBox 메서드. 이러한 메서드는 다양 한 텍스트 그리기 또는 확인란을 사용 하 여 이미지와 같은 옵션을 제공합니다.

운영 체제에서 비주얼 스타일을 사용 하 고 현재 애플리케이션에 비주얼 스타일을 적용 하는 경우 DrawCheckBox 현재 비주얼 스타일을 사용 하 여 확인란을 그립니다. 그렇지 않으면 DrawCheckBox 클래식 Windows 스타일을 사용 하 여 확인란을 그립니다. 운영 체제의 현재 비주얼 스타일 설정을 자동으로 일치 해야 하는 사용자 지정 컨트롤을 그리는 경우에 유용 합니다.

이 클래스의 함수를 래핑하는 System.Windows.Forms.VisualStyles.VisualStyleRenderer 에서 노출 한 요소 중 하나에 설정 된는 System.Windows.Forms.VisualStyles.VisualStyleElement.Button.CheckBox 클래스입니다. 자세한 내용은 시각적 스타일을 사용하여 컨트롤 렌더링을 참조하세요.

속성

RenderMatchingApplicationState

렌더러에서 애플리케이션 상태를 사용하여 렌더링 스타일을 결정하는지 여부를 나타내는 값을 가져오거나 설정합니다.

메서드

DrawCheckBox(Graphics, Point, CheckBoxState)

지정된 위치에 지정된 상태의 CheckBox 컨트롤을 그립니다.

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

지정된 텍스트와 선택적 포커스 영역을 사용하여 지정된 위치에 지정된 상태의 CheckBox 컨트롤을 그립니다.

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

지정된 텍스트 및 이미지와 선택적 포커스 영역을 사용하여 지정된 위치에 지정된 상태의 CheckBox 컨트롤을 그립니다.

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

지정된 텍스트 및 텍스트 형식과 선택적 포커스 영역을 사용하여 지정된 위치에 지정된 상태의 CheckBox 컨트롤을 그립니다.

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

지정된 텍스트, 텍스트 형식 및 이미지와 선택적 포커스 영역을 사용하여 지정된 위치에 지정된 상태의 CheckBox 컨트롤을 그립니다.

DrawParentBackground(Graphics, Rectangle, Control)

지정된 영역에 컨트롤 부모의 배경을 그립니다.

GetGlyphSize(Graphics, CheckBoxState)

확인란 기호의 크기를 반환합니다.

IsBackgroundPartiallyTransparent(CheckBoxState)

확인란의 배경에 반투명 또는 알파 혼합 부분이 있는지 여부를 나타냅니다.

적용 대상

추가 정보