.NET TextBoxの枠線の色を変えるには

TextBoxの枠線の色を変えるサンプルです。
BorderStyleプロパティがWindows.Forms.BorderStyle.FixedSingleの場合のみ枠線の色を変えます。

Imports System.ComponentModel

Public Class CsTextBox
    Inherits System.Windows.Forms.TextBox

    Private _BorderColor As Color = System.Drawing.SystemColors.ControlText

    <EditorBrowsable(EditorBrowsableState.Always), Browsable(True), _
      DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
      DefaultValue(GetType(Color), "ControlText"), Category("カスタム"), _
      Description("境界線の色です。BorderStyleプロパティがWindows.Forms.BorderStyle.FixedSingleの場合のみ有効です。")> _
      Public Property BorderColor() As Color
        Get
            Return Me._BorderColor
        End Get
        Set(ByVal value As Color)
            Me._BorderColor = value
            Me.Invalidate()
        End Set
    End Property

    Private Const WM_PAINT As Integer = &HF

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_PAINT
                MyBase.WndProc(m)
                Call DrawRectangle()

            Case Else
                MyBase.WndProc(m)

        End Select
    End Sub

    Protected Overridable Sub DrawRectangle()
        If Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle Then
            Dim g As Graphics = Me.CreateGraphics()
            Dim rect As Rectangle = Me.ClientRectangle
            Dim LinePen As New Pen(Me.BorderColor)
            Try
                g.DrawRectangle(LinePen, rect.X, rect.Y, rect.Width - 1, rect.Height - 1)
            Finally
                LinePen.Dispose()
            End Try
        End If
    End Sub
End Class

0 件のコメント: