Override the drawing of some .Net Framework controls to change its border color?

前端 未结 2 467
野的像风
野的像风 2021-01-21 22:53

SCENARIO

I\'m using a 3rd party windows visual theme.

When I see my application, it looks like this:

2条回答
  •  情话喂你
    2021-01-21 23:24

    VB.Net solution for ComboBox with border (is not complete, it needs to be improved with a 3dBorder)

    #Region " Option Statements "
    
    Option Explicit On
    Option Strict On
    Option Infer Off
    
    #End Region
    
    #Region " Imports "
    
    Imports System.ComponentModel
    
    #End Region
    
    ''' 
    ''' A custom  user control.
    ''' 
    Public Class ElektroComboBox : Inherits ComboBox
    
    #Region " Properties "
    
        ''' 
        ''' Gets or sets the border color.
        ''' 
        ''' The border color.
        
        Public Property BorderColor() As Color
            Get
                Return borderColor1
            End Get
            Set(ByVal value As Color)
                Me.borderColor1 = value
                Me.Invalidate()
            End Set
        End Property
        ''' 
        ''' The border color.
        ''' 
        Private borderColor1 As Color = SystemColors.ControlDark
    
        ''' 
        ''' Gets or sets the border style.
        ''' 
        ''' The border color.
        
        Public Property BorderStyle As ButtonBorderStyle
            Get
                Return borderStyle1
            End Get
            Set(ByVal value As ButtonBorderStyle)
                Me.borderStyle1 = value
                Me.Invalidate()
            End Set
        End Property
        ''' 
        ''' The border style.
        ''' 
        Private borderStyle1 As ButtonBorderStyle = ButtonBorderStyle.Solid
    
    #End Region
    
    #Region " Enumerations "
    
        ''' 
        ''' Specifies a Windows Message.
        ''' 
        Private Enum WindowsMessages
    
            WM_PAINT = &HF
    
        End Enum
    
    #End Region
    
    #Region " Windows Procedure "
    
        ''' 
        ''' Processes Windows messages.
        ''' 
        ''' The Windows  to process.
        Protected Overrides Sub WndProc(ByRef m As Message)
    
            MyBase.WndProc(m)
    
            Select Case m.Msg
    
                Case WindowsMessages.WM_PAINT
                    Me.DrawBorder()
    
            End Select
    
        End Sub
    
    #End Region
    
    #Region " Private Methods "
    
        ''' 
        ''' Draws a border on the control surface.
        ''' 
        Private Sub DrawBorder()
    
            Using g As Graphics = Graphics.FromHwnd(Me.Handle)
    
                ControlPaint.DrawBorder(Graphics.FromHwnd(Me.Handle), Me.ClientRectangle, Me.borderColor1, Me.borderStyle1)
    
            End Using
    
        End Sub
    
    #End Region
    
    End Class
    

    VB.Net solution for GroupBox with border (is not complete, it redraws the contained controls in a wrong way at design mode)

    #Region " Option Statements "
    
    Option Explicit On
    Option Strict On
    Option Infer Off
    
    #End Region
    
    #Region " Imports "
    
    Imports System.ComponentModel
    
    #End Region
    
    #Region " ElektroGroupBox "
    
    ''' 
    ''' A custom  user control.
    ''' 
    Public Class ElektroGroupBox : Inherits GroupBox
    
    #Region " Properties "
    
        ''' 
        ''' Gets or sets the border color.
        ''' 
        ''' The border color.
        
        Public Property BorderColor As Color
            Get
                Return Me.borderColor1
            End Get
            Set(ByVal value As Color)
                Me.borderColor1 = value
                Me.Invalidate()
            End Set
        End Property
        ''' 
        ''' The border color.
        ''' 
        Private borderColor1 As Color = SystemColors.ControlDark
    
        ''' 
        ''' Gets or sets the border style.
        ''' 
        ''' The border color.
        
        Public Property BorderStyle As ButtonBorderStyle
            Get
                Return borderStyle1
            End Get
            Set(ByVal value As ButtonBorderStyle)
                Me.borderStyle1 = value
                Me.Invalidate()
            End Set
        End Property
        ''' 
        ''' The border style.
        ''' 
        Private borderStyle1 As ButtonBorderStyle = ButtonBorderStyle.Solid
    
    #End Region
    
    #Region " Events "
    
        ''' 
        ''' Handles the  event.
        ''' 
        ''' A  that contains the event data.
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    
            '  MyBase.OnPaint(e)
            Me.DrawBorder(e)
    
        End Sub
    
    #End Region
    
    #Region " Private Methods "
    
        ''' 
        ''' Draws a border on the control surface.
        ''' 
        Private Sub DrawBorder(ByVal e As PaintEventArgs)
    
            ' The groupbox header text size.
            Dim textSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
    
            ' The width of the blankspace drawn at the right side of the text.
            Dim blankWidthSpace As Integer = 3
    
            ' The thex horizontal offset.
            Dim textOffset As Integer = 7
    
            ' The rectangle where to draw the border.
            Dim borderRect As Rectangle = e.ClipRectangle
            With borderRect
                .Y = .Y + (textSize.Height \ 2)
                .Height = .Height - (textSize.Height \ 2)
            End With
    
            ' The rectangle where to draw the header text.
            Dim textRect As Rectangle = e.ClipRectangle
            With textRect
                .X = .X + textOffset
                .Width = (textSize.Width - blankWidthSpace)
                .Height = textSize.Height
            End With
    
            ' Draw the border.
            ControlPaint.DrawBorder(e.Graphics, borderRect, Me.borderColor1, Me.borderStyle1)
    
            ' Fill the text rectangle.
            e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
    
            ' Draw the text on the text rectangle.
            textRect.Width = textSize.Width + (blankWidthSpace * 2) ' Fix the right side space.
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect)
    
        End Sub
    
    #End Region
    
    End Class
    
    #End Region
    

提交回复
热议问题