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

TextBoxRenderer クラス

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

visual スタイルを使用してテキスト ボックス コントロールをレンダリングするメソッドを用意します。このクラスは継承できません。

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

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

TextBoxRenderer クラスには、オペレーティング システムの現在の visual スタイルでテキスト ボックス コントロールをレンダリングするために使用できる一連の static メソッドが用意されています。コントロールのレンダリングとは、コントロールのユーザー インターフェイスを描画することです。現在の visual スタイルの外観を持つカスタム コントロールを描画する場合に有用です。テキスト ボックスを描画するには、DrawTextBox メソッドのうちいずれかを使用します。これらのメソッドは、テキストの書式設定の適用、テキストの境界の指定などの、さまざまなオプションを提供します。

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

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

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

使用例使用例

DrawTextBox メソッドを使用してテキスト ボックスを描画するカスタム コントロールを作成する方法を次のコード例に示します。このコントロールを使用すると、ユーザーは TextFormatFlags 値のいずれかを選択して、テキスト ボックス テキストに適用することもできます。

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

Namespace TextBoxRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            Me.Size = New Size(350, 200)
            Dim TextBox1 As New
 CustomTextBox()
            Controls.Add(TextBox1)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' The call to EnableVisualStyles below does not affect whether
 
            ' TextBoxRenderer draws the text box; as long as visual
 styles 
            ' are enabled by the operating system, TextBoxRenderer will
 
            ' draw the text box.
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub
    End Class

    Public Class CustomTextBox
        Inherits Control

        Private textFlags As TextFormatFlags
 = TextFormatFlags.Default
        Private WithEvents comboBox1 As
 New ComboBox()
        Private textBorder As New
 Rectangle()
        Private textRectangle As New
 Rectangle()
        Private textMeasurements As New
 StringBuilder()

        Public Sub New()

            With Me
                .Location = New Point(10, 10)
                .Size = New Size(300, 200)
                .Font = SystemFonts.IconTitleFont
                .Text = "This is a long sentence that will exceed
 " + _
                    "the text box bounds"
            End With

            textBorder.Location = New Point(10, 10)
            textBorder.Size = New Size(200, 50)
            textRectangle.Location = New Point(textBorder.X +
 2, _
                textBorder.Y + 2)
            textRectangle.Size = New Size(textBorder.Size.Width
 - 4, _
                textBorder.Height - 4)

            comboBox1.Location = New Point(10, 100)
            comboBox1.Size = New Size(150, 20)

            ' Populate the combo box with the TextFormatFlags value
 names.
            Dim name As String
            For Each name In
 [Enum].GetNames(GetType(TextFormatFlags))
                comboBox1.Items.Add(name)
            Next name

            comboBox1.SelectedIndex = 0
            Me.Controls.Add(comboBox1)
        End Sub

        ' Use DrawText with the current TextFormatFlags.
        Protected Overrides Sub
 OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            If TextBoxRenderer.IsSupported Then
                TextBoxRenderer.DrawTextBox(e.Graphics, textBorder, Me.Text,
 _
                    Me.Font, textRectangle, textFlags, TextBoxState.Normal)
                Me.Parent.Text = "CustomTextBox
 Enabled"
            Else
                Me.Parent.Text = "CustomTextBox
 Disabled"
            End If
        End Sub

        ' Assign the combo box selection to the display text.
        Private Sub comboBox1_SelectedIndexChanged(ByVal
 sender As Object, _
            ByVal e As EventArgs) Handles
 comboBox1.SelectedIndexChanged

            Me.textFlags = CType([Enum].Parse(GetType(TextFormatFlags),
 _
                CStr(comboBox1.Items(comboBox1.SelectedIndex))), _
                TextFormatFlags)
            Invalidate()
        End Sub

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

namespace TextBoxRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            this.Size = new Size(350, 200);
            CustomTextBox TextBox1 = new CustomTextBox();
            Controls.Add(TextBox1);
        }

        [STAThread]
        static void Main()
        {
            // The call to EnableVisualStyles below does not affect
 whether 
            // TextBoxRenderer draws the text box; as long as visual
 styles 
            // are enabled by the operating system, TextBoxRenderer
 will 
            // draw the text box.
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

    public class CustomTextBox : Control
    {
        private TextFormatFlags textFlags = TextFormatFlags.Default;
        ComboBox comboBox1 = new ComboBox();
        Rectangle textBorder = new Rectangle();
        Rectangle textRectangle = new Rectangle();
        StringBuilder textMeasurements = new StringBuilder();

        public CustomTextBox()
            : base()
        {
            this.Location = new Point(10, 10);
            this.Size = new Size(300, 200);
            this.Font = SystemFonts.IconTitleFont;
            this.Text = "This is a long sentence that will
 exceed " +
                "the text box bounds";

            textBorder.Location = new Point(10, 10);
            textBorder.Size = new Size(200, 50);
            textRectangle.Location = new Point(textBorder.X +
 2,
                textBorder.Y + 2);
            textRectangle.Size = new Size(textBorder.Size.Width
 - 4,
                textBorder.Height - 4);

            comboBox1.Location = new Point(10, 100);
            comboBox1.Size = new Size(150, 20);
            comboBox1.SelectedIndexChanged +=
                new EventHandler(comboBox1_SelectedIndexChanged);

            // Populate the combo box with the TextFormatFlags value
 names.
            foreach (string name in
 Enum.GetNames(typeof(TextFormatFlags)))
            {
                comboBox1.Items.Add(name);
            }

            comboBox1.SelectedIndex = 0;
            this.Controls.Add(comboBox1);
        }

        // Use DrawText with the current TextFormatFlags.
        protected override void OnPaint(PaintEventArgs
 e)
        {
            base.OnPaint(e);

            if (TextBoxRenderer.IsSupported)
            {
                TextBoxRenderer.DrawTextBox(e.Graphics, textBorder, this.Text
,
                    this.Font, textRectangle, textFlags, TextBoxState.Normal);

                this.Parent.Text = "CustomTextBox Enabled";
            }
            else
            {
                this.Parent.Text = "CustomTextBox Disabled";
            }
        }

        // Assign the combo box selection to the display text.
        void comboBox1_SelectedIndexChanged(object sender, EventArgs
 e)
        {
            this.textFlags = (TextFormatFlags)Enum.Parse(
                typeof(TextFormatFlags),
                (string)comboBox1.Items[comboBox1.SelectedIndex]);
            Invalidate();
        }
    }
}
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>

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

namespace TextBoxRendererSample
{
    public ref class CustomTextBox : public
 Control
    {
    private:
        TextFormatFlags textFlags;
        ComboBox^ textFormatFlagsComboBox;
        Rectangle textBorder;
        Rectangle textRectangle;
        StringBuilder^ textMeasurements;

    public:
        CustomTextBox():Control()
        {
            textFlags = TextFormatFlags::Default;
            textFormatFlagsComboBox = gcnew ComboBox();

            textMeasurements = gcnew StringBuilder();

            this->Location = Point(10, 10);
            this->Size = System::Drawing::Size(300, 200);
            this->Font = SystemFonts::IconTitleFont;
            this->Text = "This is a long sentence that
 will exceed " +
                "the text box bounds";

            textBorder.Location = Point(10, 10);
            textBorder.Size = System::Drawing::Size(200, 50);
            textRectangle.Location = Point(textBorder.X + 2,
                textBorder.Y + 2);
            textRectangle.Size =  System::Drawing::Size(textBorder.Size.Width - 4
,
                textBorder.Height - 4);

            textFormatFlagsComboBox->Location = Point(10, 100);
            textFormatFlagsComboBox->Size = System::Drawing::Size(150, 20);
            textFormatFlagsComboBox->SelectedIndexChanged +=
                gcnew EventHandler(this, 
                &CustomTextBox::textFormatFlagsComboBox_SelectedIndexChanged);

            // Populate the combo box with the TextFormatFlags value
 names.
            for each (String^ name in Enum::GetNames(TextFormatFlags::typeid))
            {
                textFormatFlagsComboBox->Items->Add(name);
            }

            textFormatFlagsComboBox->SelectedIndex = 0;
            this->Controls->Add(textFormatFlagsComboBox);
        }

        // Use DrawText with the current TextFormatFlags.

    protected:
        virtual void OnPaint(PaintEventArgs^ e) override
        {
            __super::OnPaint(e);

            if (TextBoxRenderer::IsSupported)
            {
                TextBoxRenderer::DrawTextBox(e->Graphics, textBorder, this->Text
,
                    this->Font, textRectangle, textFlags, TextBoxState::Normal);

                this->Parent->Text = "CustomTextBox
 Enabled";
            }
            else
            {
                this->Parent->Text = "CustomTextBox
 Disabled";
            }
        }

        // Assign the combo box selection to the display text.
    private:
        void textFormatFlagsComboBox_SelectedIndexChanged(
            Object^ sender, EventArgs^ e)
        {
            this->textFlags = (TextFormatFlags)Enum::Parse(
                TextFormatFlags::typeid,
                (String^)textFormatFlagsComboBox->Items[
                    textFormatFlagsComboBox->SelectedIndex]);

                    Invalidate();
        }
    };

    public ref class Form1 : public
 Form
    {
    public:
        Form1()
        {
            __super::Form();
            this->Size = System::Drawing::Size(350, 200);
            CustomTextBox^ textBox1 = gcnew CustomTextBox();
            Controls->Add(textBox1);
        }
    };
}

using namespace TextBoxRendererSample;

[STAThread]
int main()
{     
    // The call to EnableVisualStyles below does not affect whether
 
    // TextBoxRenderer draws the text box; as long as visual styles
 
    // are enabled by the operating system, TextBoxRenderer will 
    // draw the text box.
    Application::EnableVisualStyles();
    Application::Run(gcnew Form1());
}

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

TextBoxRenderer プロパティ


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

参照参照

関連項目

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

TextBoxRenderer メソッド


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

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

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

TextBoxRenderer メンバ

visual スタイルを使用してテキスト ボックス コントロールをレンダリングするメソッドを用意します。このクラスは継承できません。

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


パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

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



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「TextBoxRenderer」の関連用語

TextBoxRendererのお隣キーワード

   

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



TextBoxRendererのページの著作権

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

©2026 GRAS Group, Inc.RSS