TextBoxRenderer 클래스

정의

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

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

예제

다음 코드 예제를 사용 하는 사용자 지정 컨트롤을 만드는 방법을 보여 줍니다는 DrawTextBox 텍스트 상자를 그리는 방법입니다. 컨트롤에는 사용자가 중 하나를 선택할 수도 있습니다는 TextFormatFlags 텍스트에 적용할 값 텍스트 상자입니다.

#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

설명

합니다 TextBoxRenderer 클래스의 집합을 제공 static 운영 체제의 현재 비주얼 스타일을 사용 하 여 텍스트 상자 컨트롤을 렌더링 하는 데 사용할 수 있는 방법입니다. 컨트롤 렌더링이란 컨트롤의 사용자 인터페이스를 그리는 것을 말합니다. 현재 비주얼 스타일의 모양이 있어야 하는 사용자 지정 컨트롤을 그리는 경우에 유용 합니다. 입력란을 그리려면 중 하나를 사용 합니다 DrawTextBox 메서드. 이러한 메서드는 다양 한 텍스트 서식 적용 또는 텍스트 범위 지정과 같은 옵션을 제공합니다.

운영 체제에서 비주얼 스타일을 사용 하 고 애플리케이션 창의 클라이언트 영역에 비주얼 스타일을 적용 하는 경우 DrawTextBox 는 현재 비주얼 스타일을 사용 하 여 텍스트 상자를 그립니다. 그렇지 않으면 DrawTextBox 시킵니다는 InvalidOperationException합니다. 이 클래스의 멤버를 사용할 수 있는지 여부를 확인 하려면 값을 확인할 수 있습니다는 IsSupported 속성입니다.

이 클래스의 함수를 래핑하는 System.Windows.Forms.VisualStyles.VisualStyleRenderer 의 요소 중 하나에 설정 된는 System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox.TextEdit 클래스입니다. 자세한 내용은 비주얼 스타일을 사용 하 여 컨트롤 렌더링합니다.

속성

IsSupported

비주얼 스타일을 사용하여 텍스트 상자를 그리는 데 TextBoxRenderer 클래스를 사용할 수 있는지 여부를 나타내는 값을 가져옵니다.

메서드

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

지정된 텍스트와 텍스트 범위를 사용하여 지정된 상태와 범위로 텍스트 상자 컨트롤을 그립니다.

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

지정된 텍스트, 텍스트 범위 및 텍스트 서식을 사용하여 지정된 상태와 범위로 텍스트 상자 컨트롤을 그립니다.

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

지정된 텍스트를 사용하여 지정된 상태와 범위로 텍스트 상자 컨트롤을 그립니다.

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

지정된 텍스트와 텍스트 서식을 사용하여 지정된 상태와 범위로 텍스트 상자 컨트롤을 그립니다.

DrawTextBox(Graphics, Rectangle, TextBoxState)

지정된 상태와 범위로 텍스트 상자 컨트롤을 그립니다.

적용 대상

추가 정보