다음을 통해 공유


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 의 클라이언트 영역에 적용되는 경우 현재 비주얼 스타일로 텍스트 상자를 그립니다. 그렇지 않으면 . DrawTextBoxInvalidOperationException 이 클래스의 멤버를 사용할 수 있는지 여부를 확인 하려면 속성의 IsSupported 값을 확인할 수 있습니다.

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

속성

Name Description
IsSupported

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

메서드

Name Description
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)

지정된 상태 및 범위에서 텍스트 상자 컨트롤을 그립니다.

적용 대상

추가 정보