VB.net 改变文本框Textbox的边框线颜色

自带的Textbox是无法改变边框颜色的,所以需要自定义一下

Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
''' <summary>
''' 含有边框线的文本框
''' </summary>
Public Class CustomTextBox
    Inherits Windows.Forms.TextBox
    Private Declare Function GetWindowDC Lib "user32.dll" (ByVal hwnd As Integer) As Integer
    Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer
    Private Const WM_NCPAINT As Integer = &H85
    Private Const WM_PAINT As Integer = &HF&

    Sub New()
        InitializeComponent()
        MyBase.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
    End Sub

    Protected Overloads Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        Select Case m.Msg
            Case WM_NCPAINT, WM_PAINT
                Dim hDC As Integer
                Dim pen As New Pen(BorderColor)
                hDC = GetWindowDC(m.HWnd)
                Dim g As Graphics = Graphics.FromHdc(hDC)
                g.DrawRectangle(pen, 0, 0, Me.Width - 1, Me.Height - 1)
                ReleaseDC(m.HWnd, hDC)
                g.Dispose()
                pen.Dispose()
        End Select
    End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Private m_BorderColor As Color = Color.Black
    <Description("设置边框线颜色,注意: 当设置边框类型为None,请设置边框颜色为透明"), Category("自定义")>
    Public Property BorderColor() As Color
        Get
            Return m_BorderColor
        End Get
        Set(ByVal value As Color)
            m_BorderColor = value
            Me.Refresh()
        End Set
    End Property
End Class

效果图:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值