CheckBoxRenderer 類別

定義

提供用來呈現核取方塊控制項的方法 (不論有沒有視覺化樣式)。 此類別無法獲得繼承。

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.VisualStyleElement.Button.CheckBox 功能 System.Windows.Forms.VisualStyles.VisualStyleRenderer 。 如需詳細資訊,請參閱 使用視覺化樣式轉譯控制項

屬性

RenderMatchingApplicationState

取得或設定值,指出產生器是否會使用應用程式狀態來判斷呈現樣式。

方法

DrawCheckBox(Graphics, Point, CheckBoxState)

在指定的狀態和位置中繪製核取方塊控制項。

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

使用指定的文字以及選擇性的焦點矩形,在指定的狀態和位置中繪製核取方塊控制項。

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

使用指定的文字和影像,以及選擇性的焦點矩形,在指定的狀態和位置中繪製核取方塊控制項。

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

使用指定的文字和文字格式,以及選擇性的焦點矩形,在指定的狀態和位置中繪製核取方塊控制項。

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

使用指定的文字、文字格式和影像,以及選擇性的焦點矩形,在指定的狀態和位置中繪製核取方塊控制項。

DrawParentBackground(Graphics, Rectangle, Control)

在指定的區域中繪製控制項父代的背景。

GetGlyphSize(Graphics, CheckBoxState)

傳回核取方塊圖像 (Glyph) 的大小。

IsBackgroundPartiallyTransparent(CheckBoxState)

指出核取方塊的背景是否有半透明或 Alpha 混合片段。

適用於

另請參閱