TextBoxRenderer TextBoxRenderer TextBoxRenderer TextBoxRenderer Class

定义

提供用于呈现具有视觉样式的文本框控件的方法。Provides methods used to render a text box control with visual styles. 此类不能被继承。This class cannot be inherited.

public ref class TextBoxRenderer sealed
public sealed class TextBoxRenderer
type TextBoxRenderer = class
Public NotInheritable Class TextBoxRenderer
继承
TextBoxRendererTextBoxRendererTextBoxRendererTextBoxRenderer

示例

下面的代码示例演示如何创建使用DrawTextBox方法绘制文本框的自定义控件。The following code example demonstrates how to create a custom control that uses the DrawTextBox method to draw a text box. 该控件还允许用户选择要应用于文本框文本TextFormatFlags的值之一。The control also allows the user to select one of the TextFormatFlags values to apply to the text box text.

#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());
}

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();
        }
    }
}
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

注解

类提供一static组方法, 这些方法可用于呈现具有操作系统的当前视觉样式的文本框控件。 TextBoxRendererThe TextBoxRenderer class provides a set of static methods that can be used to render a text box control with the current visual style of the operating system. 呈现控件是指绘制控件的用户界面。Rendering a control refers to drawing the user interface of a control. 如果要绘制的自定义控件应具有当前视觉样式的外观, 此方法非常有用。This is useful if you are drawing a custom control that should have the appearance of the current visual style. 若要绘制文本框, 请使用其中一DrawTextBox种方法。To draw a text box, use one of the DrawTextBox methods. 这些方法提供了各种选项, 例如应用文本格式设置或指定文本边界。These methods provide a variety of options, such as applying text formatting or specifying text bounds.

如果在操作系统中启用了视觉样式, 并将视觉样式应用于应用程序窗口的工作区, DrawTextBox将绘制具有当前视觉样式的文本框。If visual styles are enabled in the operating system and visual styles are applied to the client area of application windows, DrawTextBox will draw the text box with the current visual style. 否则, DrawTextBoxInvalidOperationException引发。Otherwise, DrawTextBox 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.TextBox.TextEdit类的元素之一的的功能。This class wraps the functionality of a System.Windows.Forms.VisualStyles.VisualStyleRenderer that is set to one of the elements of the System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox.TextEdit class. 有关详细信息,请参阅使用视觉样式呈现控件For more information, see Rendering Controls with Visual Styles.

属性

IsSupported IsSupported IsSupported IsSupported

获取一个值,该值指示 TextBoxRenderer 类是否可用于绘制具有视觉样式的文本框。Gets a value indicating whether the TextBoxRenderer class can be used to draw a text box with visual styles.

方法

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

绘制具有指定的状态和边界以及指定的文本和文本边界的文本框控件。Draws a text box control in the specified state and bounds, and with the specified text and text bounds.

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

绘制具有指定的状态和边界以及指定的文本、文本边界和文本格式的文本框控件。Draws a text box control in the specified state and bounds, and with the specified text, text bounds, and text formatting.

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

绘制具有指定的状态和边界以及指定的文本的文本框控件。Draws a text box control in the specified state and bounds, and with the specified text.

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

绘制具有指定的状态和边界以及指定的文本和文本格式的文本框控件。Draws a text box control in the specified state and bounds, and with the specified text and text formatting.

DrawTextBox(Graphics, Rectangle, TextBoxState) DrawTextBox(Graphics, Rectangle, TextBoxState) DrawTextBox(Graphics, Rectangle, TextBoxState) DrawTextBox(Graphics, Rectangle, TextBoxState)

绘制具有指定状态和边界的文本框控件。Draws a text box control in the specified state and bounds.

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()

获取当前实例的 TypeGets 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)

适用于

另请参阅