ComboBoxRenderer ComboBoxRenderer ComboBoxRenderer ComboBoxRenderer Class

定義

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

public ref class ComboBoxRenderer sealed
public sealed class ComboBoxRenderer
type ComboBoxRenderer = class
Public NotInheritable Class ComboBoxRenderer
継承
ComboBoxRendererComboBoxRendererComboBoxRendererComboBoxRenderer

次のコード例は、メソッドDrawTextBoxDrawDropDownButtonメソッドを使用してマウスクリックに応答するコンボボックスを描画するカスタムコントロールを作成する方法を示しています。The following code example demonstrates how to create a custom control that uses the DrawTextBox and DrawDropDownButton methods to draw a combo 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 ComboBoxRendererSample
{
    ref class CustomComboBox: public Control
    {
    private:
        System::Drawing::Size arrowSize;
        Rectangle arrowRectangle;
        Rectangle topTextBoxRectangle;
        Rectangle bottomTextBoxRectangle;
        ComboBoxState textBoxState;
        ComboBoxState arrowState;
        String^ bottomText;
        bool isActivated;
        int minHeight;
        int minWidth;

    public:
        CustomComboBox() : Control()

          {
              minHeight = 38;
              minWidth = 40; 
              this->Location = Point(10, 10);
              this->Size = System::Drawing::Size(140, 38);
              this->Font = SystemFonts::IconTitleFont;
              this->Text = "Click the button";
              textBoxState = ComboBoxState::Normal;
              bottomText = "Using ComboBoxRenderer";
              arrowState = ComboBoxState::Normal;

              // Initialize the rectangles to look like the standard combo
              // box control.
              arrowSize = System::Drawing::Size(18, 20);
              arrowRectangle = Rectangle(ClientRectangle.X +
                  ClientRectangle.Width - arrowSize.Width - 1,
                  ClientRectangle.Y + 1,
                  arrowSize.Width,
                  arrowSize.Height);
              topTextBoxRectangle = Rectangle(ClientRectangle.X,
                  ClientRectangle.Y,
                  ClientRectangle.Width,
                  arrowSize.Height + 2);
              bottomTextBoxRectangle = Rectangle(ClientRectangle.X,
                  ClientRectangle.Y + topTextBoxRectangle.Height,
                  ClientRectangle.Width,
                  topTextBoxRectangle.Height - 6);
          }

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

            if (!ComboBoxRenderer::IsSupported)
            {
                this->Parent->Text = "Visual Styles Disabled";
                return;
            }

            this->Parent->Text = "CustomComboBox Enabled";

            // Always draw the main text box and drop down arrow in their
            // current states
            ComboBoxRenderer::DrawTextBox(e->Graphics, topTextBoxRectangle,
                this->Text, this->Font, textBoxState);
            ComboBoxRenderer::DrawDropDownButton(e->Graphics, arrowRectangle,
                arrowState);

            // Only draw the bottom text box if the arrow has been clicked
            if (isActivated)
            {
                ComboBoxRenderer::DrawTextBox(e->Graphics,
                    bottomTextBoxRectangle, bottomText, this->Font,
                    textBoxState);
            }
        }

    protected:
        virtual void OnMouseDown(MouseEventArgs^ e) override 
        {
            Control::OnMouseDown(e);

            // Check whether the user clicked the arrow.
            if (arrowRectangle.Contains(e->Location) &&
                ComboBoxRenderer::IsSupported)
            {
                // Draw the arrow in the pressed state.
                arrowState = ComboBoxState::Pressed;

                // The user has activated the combo box.
                if (!isActivated)
                {
                    this->Text = "Clicked!";
                    textBoxState = ComboBoxState::Pressed;
                    isActivated = true;
                }

                // The user has deactivated the combo box.
                else
                {
                    this->Text = "Click here";
                    textBoxState = ComboBoxState::Normal;
                    isActivated = false;
                }

                // Redraw the control.
                Invalidate();
            }
        }

    protected:
        virtual void OnMouseUp(MouseEventArgs^ e) override 
        {
            Control::OnMouseUp(e);

            if (arrowRectangle.Contains(e->Location) &&
                ComboBoxRenderer::IsSupported)
            {
                arrowState = ComboBoxState::Normal;
                Invalidate();
            }
        }
    };

    ref class Form1 : public Form
    {
    public:
        Form1() : Form()
        {
            this->Size = System::Drawing::Size(300, 300);
            CustomComboBox^ ComboBox1 = gcnew CustomComboBox();
            Controls->Add(ComboBox1);
        }

    };

}

[STAThread]
int main()
{
    // The call to EnableVisualStyles below does not affect
    // whether ComboBoxRenderer.IsSupported is true; as long as visual
    // styles are enabled by the operating system, IsSupported is true.
    Application::EnableVisualStyles();
    Application::Run(gcnew ComboBoxRendererSample::Form1());

}
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace ComboBoxRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            this.Size = new Size(300, 300);
            CustomComboBox ComboBox1 = new CustomComboBox();
            Controls.Add(ComboBox1);
        }

        [STAThread]
        static void Main()
        {
            // The call to EnableVisualStyles below does not affect
            // whether ComboBoxRenderer.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 CustomComboBox : Control
    {
        private Size arrowSize;
        private Rectangle arrowRectangle;
        private Rectangle topTextBoxRectangle;
        private Rectangle bottomTextBoxRectangle;
        private ComboBoxState textBoxState = ComboBoxState.Normal;
        private ComboBoxState arrowState = ComboBoxState.Normal;
        private String bottomText = "Using ComboBoxRenderer";
        private bool isActivated = false;
        private const int minHeight = 38;
        private const int minWidth = 40;

        public CustomComboBox()
            : base()
        {
            this.Location = new Point(10, 10);
            this.Size = new Size(140, 38);
            this.Font = SystemFonts.IconTitleFont;
            this.Text = "Click the button";

            // Initialize the rectangles to look like the standard combo 
            // box control.
            arrowSize = new Size(18, 20);
            arrowRectangle = new Rectangle(ClientRectangle.X +
                ClientRectangle.Width - arrowSize.Width - 1,
                ClientRectangle.Y + 1,
                arrowSize.Width,
                arrowSize.Height);
            topTextBoxRectangle = new Rectangle(ClientRectangle.X,
                ClientRectangle.Y,
                ClientRectangle.Width,
                arrowSize.Height + 2);
            bottomTextBoxRectangle = new Rectangle(ClientRectangle.X,
                ClientRectangle.Y + topTextBoxRectangle.Height,
                ClientRectangle.Width,
                topTextBoxRectangle.Height - 6);
        }

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

            if (!ComboBoxRenderer.IsSupported)
            {
                this.Parent.Text = "Visual Styles Disabled";
                return;
            }

            this.Parent.Text = "CustomComboBox Enabled";

            // Always draw the main text box and drop down arrow in their 
            // current states
            ComboBoxRenderer.DrawTextBox(e.Graphics, topTextBoxRectangle,
                this.Text, this.Font, textBoxState);
            ComboBoxRenderer.DrawDropDownButton(e.Graphics, arrowRectangle,
                arrowState);

            // Only draw the bottom text box if the arrow has been clicked
            if (isActivated)
            {
                ComboBoxRenderer.DrawTextBox(e.Graphics,
                    bottomTextBoxRectangle, bottomText, this.Font,
                    textBoxState);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            // Check whether the user clicked the arrow.
            if (arrowRectangle.Contains(e.Location) &&
                ComboBoxRenderer.IsSupported)
            {
                // Draw the arrow in the pressed state.
                arrowState = ComboBoxState.Pressed;

                // The user has activated the combo box.
                if (!isActivated)
                {
                    this.Text = "Clicked!";
                    textBoxState = ComboBoxState.Pressed;
                    isActivated = true;
                }

                // The user has deactivated the combo box.
                else
                {
                    this.Text = "Click here";
                    textBoxState = ComboBoxState.Normal;
                    isActivated = false;
                }

                // Redraw the control.
                Invalidate();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (arrowRectangle.Contains(e.Location) &&
                ComboBoxRenderer.IsSupported)
            {
                arrowState = ComboBoxState.Normal;
                Invalidate();
            }
        }
    }
}
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Namespace ComboBoxRendererSample
    Class Form1
        Inherits Form

        Public Sub New()
            Me.Size = New Size(300, 300)
            Dim ComboBox1 As New CustomComboBox()
            Controls.Add(ComboBox1)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' The call to EnableVisualStyles below does not affect
            ' whether ComboBoxRenderer.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 CustomComboBox
        Inherits Control

        Private arrowSize As Size
        Private arrowRectangle As Rectangle
        Private topTextBoxRectangle As Rectangle
        Private bottomTextBoxRectangle As Rectangle
        Private textBoxState As ComboBoxState = ComboBoxState.Normal
        Private arrowState As ComboBoxState = ComboBoxState.Normal
        Private bottomText As String = "Using ComboBoxRenderer"
        Private isActivated As Boolean = False
        Private minHeight As Integer = 38
        Private minWidth As Integer = 40

        Public Sub New()
            Me.Location = New Point(10, 10)
            Me.Size = New Size(140, 38)
            Me.Font = SystemFonts.IconTitleFont
            Me.Text = "Click the button"

            ' Initialize the rectangles to look like the standard combo 
            ' box control.
            arrowSize = New Size(18, 20)
            arrowRectangle = New Rectangle(Me.ClientRectangle.X + _
                Me.ClientRectangle.Width - arrowSize.Width - 1, _
                Me.ClientRectangle.Y + 1, arrowSize.Width, _
                arrowSize.Height)
            topTextBoxRectangle = New Rectangle(Me.ClientRectangle.X, _
                Me.ClientRectangle.Y, Me.ClientRectangle.Width, _
                arrowSize.Height + 2)
            bottomTextBoxRectangle = New Rectangle(Me.ClientRectangle.X, _
                Me.ClientRectangle.Y + topTextBoxRectangle.Height, _
                Me.ClientRectangle.Width, topTextBoxRectangle.Height - 6)
        End Sub

        ' Draw the combo box in the current state.
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            If Not ComboBoxRenderer.IsSupported Then
                Me.Parent.Text = "Visual Styles	Disabled"
                Return
            End If

            Me.Parent.Text = "CustomComboBox Enabled"

            ' Always draw the main text box and drop down arrow in their 
            ' current states.
            ComboBoxRenderer.DrawTextBox(e.Graphics, topTextBoxRectangle, _
                Me.Text, Me.Font, textBoxState)
            ComboBoxRenderer.DrawDropDownButton(e.Graphics, arrowRectangle, _
                arrowState)

            ' Only draw the bottom text box if the arrow has been clicked.
            If isActivated Then
                ComboBoxRenderer.DrawTextBox(e.Graphics, _
                    bottomTextBoxRectangle, bottomText, Me.Font, textBoxState)
            End If
        End Sub

        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)

            ' Check whether the user clicked the arrow.
            If arrowRectangle.Contains(e.Location) And _
                ComboBoxRenderer.IsSupported Then

                ' Draw the arrow in the pressed state.
                arrowState = ComboBoxState.Pressed

                ' The user has activated the combo box.
                If Not isActivated Then
                    Me.Text = "Clicked!"
                    textBoxState = ComboBoxState.Pressed
                    isActivated = True

                ' The user has deactivated the combo box.
                Else
                    Me.Text = "Click here"
                    textBoxState = ComboBoxState.Normal
                    isActivated = False
                End If

                ' Redraw the control.
                Invalidate()
            End If
        End Sub

        Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseUp(e)
            If arrowRectangle.Contains(e.Location) And _
                ComboBoxRenderer.IsSupported Then
                arrowState = ComboBoxState.Normal
                Invalidate()
            End If
        End Sub

    End Class
End Namespace

注釈

クラスComboBoxRendererには、オペレーティングシステムstaticの現在の視覚スタイルを使用してコンボボックスコントロールを表示するために使用できる一連のメソッドが用意されています。The ComboBoxRenderer class provides a set of static methods that can be used to render a combo box control with the current visual style of the operating system. コントロールのレンダリングとは、コントロールのユーザー インターフェイスを描画することを意味します。Rendering a control refers to drawing the user interface of a control. これは、現在の visual スタイルの外観を持つ必要があるカスタムコントロールを描画する場合に便利です。This is useful if you are drawing a custom control that should have the appearance of the current visual style. コンボボックスを描画するには、 DrawTextBoxメソッドを使用してテキストボックスDrawDropDownButtonを描画し、メソッドを使用してドロップダウン矢印を描画します。To draw a combo box, use the DrawTextBox method to draw the text box and the DrawDropDownButton method to draw the drop-down arrow.

オペレーティングシステムで visual スタイルが有効になっていて、visual スタイルがアプリケーションウィンドウDrawTextBox DrawDropDownButtonのクライアント領域に適用されている場合は、現在の visual スタイルを使用してコンボボックスが描画されます。If visual styles are enabled in the operating system and visual styles are applied to the client area of application windows, DrawTextBox and DrawDropDownButton will draw the combo box with the current visual style. それ以外の場合、これらのInvalidOperationExceptionメソッドはをスローします。Otherwise, these methods will throw an InvalidOperationException. このクラスのメンバーを使用できるかどうかを判断するには、 IsSupportedプロパティの値を確認します。To determine whether the members of this class can be used, you can check the value of the IsSupported property.

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

プロパティ

IsSupported IsSupported IsSupported IsSupported

visual スタイルを使用してコンボ ボックスを描画するために、ComboBoxRenderer クラスを使用できるかどうかを示す値を取得します。Gets a value indicating whether the ComboBoxRenderer class can be used to draw a combo box with visual styles.

メソッド

DrawDropDownButton(Graphics, Rectangle, ComboBoxState) DrawDropDownButton(Graphics, Rectangle, ComboBoxState) DrawDropDownButton(Graphics, Rectangle, ComboBoxState) DrawDropDownButton(Graphics, Rectangle, ComboBoxState)

オペレーティング システムの現在の visual スタイルを使用してドロップダウン矢印を描画します。Draws a drop-down arrow with the current visual style of the operating system.

DrawTextBox(Graphics, Rectangle, ComboBoxState) DrawTextBox(Graphics, Rectangle, ComboBoxState) DrawTextBox(Graphics, Rectangle, ComboBoxState) DrawTextBox(Graphics, Rectangle, ComboBoxState)

状態と境界を指定してテキスト ボックスを描画します。Draws a text box in the specified state and bounds.

DrawTextBox(Graphics, Rectangle, String, Font, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, ComboBoxState)

状態、境界、およびテキストを指定してテキスト ボックスを描画します。Draws a text box in the specified state and bounds, with the specified text.

DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, ComboBoxState)

状態、境界、テキスト、およびテキストの境界を指定してテキスト ボックスを描画します。Draws a text box in the specified state and bounds, with the specified text and text bounds.

DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, TextFormatFlags, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, TextFormatFlags, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, TextFormatFlags, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, Rectangle, TextFormatFlags, ComboBoxState)

状態、境界、テキスト、テキストの書式、およびテキストの境界を指定してテキスト ボックスを描画します。Draws a text box in the specified state and bounds, with the specified text, text formatting, and text bounds.

DrawTextBox(Graphics, Rectangle, String, Font, TextFormatFlags, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, TextFormatFlags, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, TextFormatFlags, ComboBoxState) DrawTextBox(Graphics, Rectangle, String, Font, TextFormatFlags, ComboBoxState)

状態、境界、テキスト、およびテキストの書式を指定してテキスト ボックスを描画します。Draws a text box in the specified state and bounds, with the specified text and text formatting.

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

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

(Inherited from Object)
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)
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)

適用対象

こちらもご覧ください