Windows Develop Bookmark and Share   
 index > Windows Forms Designer > change Border Color of a windows Form in vb.net
 

change Border Color of a windows Form in vb.net

Hi All
How to change the Form Border Color of a windows Form in vb.net.

I am using..

Dim borderWidth As Integer = 1

Dim borderColor As Color = Color.FromArgb(55, 96, 145) 'Color.LimeGreen

ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid)

But This code is not working for Form, this is for label,Panel etc.

So, How Can I Change the Form Border Color.
PdNet  Monday, August 24, 2009 11:21 AM

Hi PdNet,

As far as I know, there is no direct way to change the border color of the form. We can set the FormBorderStyle to none to hide the title bar and borders of the form. Then we can do some custom painting to draw the borders and title bar by ourselves. This is the code snippet:

'A form with custom border and title bar.
'Some functions, such as resize the window via mouse, are not implemented yet. 
Public Class CustomBorderColorForm

    'The color and the width of the border.
    Private borderColor As Color = Color.GreenYellow
    Private borderWidth As Integer = 3
    'The color and region of the header.
    Private headerColor As Color = Color.GreenYellow
    Private headerRect As Rectangle
    'The region of the client.
    Private clientRect As Rectangle
    'The region of the title text.
    Private titleRect As Rectangle
    'The region of the minimum button.
    Private miniBoxRect As Rectangle
    'The region of the maximum button.
    Private maxBoxRect As Rectangle
    'The region of the close button.
    Private closeBoxRect As Rectangle
    'The states of the three header buttons.
    Private miniState As ButtonState
    Private maxState As ButtonState
    Private closeState As ButtonState
    'Store the mouse down point to handle moving the form.
    Private x As Integer = 0
    Private y As Integer = 0
    'The height of the header.
    Const HEADER_HEIGHT As Integer = 25
    'The size of the header buttons.
    ReadOnly BUTTON_BOX_SIZE As Size = New Size(15, 15)

    Private Sub CustomBorderColorForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Hide the border and the title bar.
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    End Sub

    Private Sub CustomBorderColorForm_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        'Draw the header.
        Using b As Brush = New SolidBrush(borderColor)
            e.Graphics.FillRectangle(b, headerRect)
        End Using
        'Draw the title text
        Using b As Brush = New SolidBrush(Me.ForeColor)
            e.Graphics.DrawString(Me.Text, Me.Font, b, titleRect)
        End Using
        'Draw the header buttons.
        If Me.MinimizeBox Then
            ControlPaint.DrawCaptionButton(e.Graphics, miniBoxRect, CaptionButton.Minimize, miniState)
        End If
        If Me.MinimizeBox Then
            ControlPaint.DrawCaptionButton(e.Graphics, maxBoxRect, CaptionButton.Maximize, maxState)
        End If
        If Me.MinimizeBox Then
            ControlPaint.DrawCaptionButton(e.Graphics, closeBoxRect, CaptionButton.Close, closeState)
        End If
        'Draw the border.
        ControlPaint.DrawBorder(e.Graphics, clientRect, borderColor, _
            borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid)
    End Sub

    'Handle resize to adjust the region ot border, header and so on.
    Private Sub CustomBorderColorForm_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        headerRect = New Rectangle(Me.ClientRectangle.Location, New Size(Me.ClientRectangle.Width, HEADER_HEIGHT))
        clientRect = New Rectangle(New Point(Me.ClientRectangle.Location.X, Me.ClientRectangle.Y + HEADER_HEIGHT), _
            New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height - HEADER_HEIGHT))
        Dim yOffset = (headerRect.Height + borderWidth - BUTTON_BOX_SIZE.Height) / 2
        titleRect = New Rectangle(yOffset, yOffset, _
                                Me.ClientRectangle.Width - 3 * (BUTTON_BOX_SIZE.Width + 1) - yOffset, _
                                BUTTON_BOX_SIZE.Height)
        miniBoxRect = New Rectangle(Me.ClientRectangle.Width - 3 * (BUTTON_BOX_SIZE.Width + 1), _
                                    yOffset, BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
        maxBoxRect = New Rectangle(Me.ClientRectangle.Width - 2 * (BUTTON_BOX_SIZE.Width + 1), _
                                    yOffset, BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
        closeBoxRect = New Rectangle(Me.ClientRectangle.Width - 1 * (BUTTON_BOX_SIZE.Width + 1), _
                                    yOffset, BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
        Me.Invalidate()
    End Sub


    Private Sub CustomBorderColorForm_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        'Start to move the form.
        If (titleRect.Contains(e.Location)) Then
            x = e.X
            y = e.Y
        End If

        'Check and press the header buttons.
        Dim mousePos As Point = Me.PointToClient(Control.MousePosition)
        If (miniBoxRect.Contains(mousePos)) Then
            miniState = ButtonState.Pushed
        ElseIf (maxBoxRect.Contains(mousePos)) Then
            maxState = ButtonState.Pushed
        ElseIf (closeBoxRect.Contains(mousePos)) Then
            closeState = ButtonState.Pushed
        End If

    End Sub

    Private Sub CustomBorderColorForm_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        'Move and refresh.
        If (x <> 0 And y <> 0) Then
            Me.Location = New Point(Me.Left + e.X - x, Me.Top + e.Y - y)
            Me.Refresh()
        End If

    End Sub

    Private Sub CustomBorderColorForm_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        'Reset the mouse point.
        x = 0
        y = 0

        'Check the button states and modify the window state.
        If miniState = ButtonState.Pushed Then
            Me.WindowState = FormWindowState.Minimized
            miniState = ButtonState.Normal
        ElseIf maxState = ButtonState.Pushed Then
            If Me.WindowState = FormWindowState.Normal Then
                Me.WindowState = FormWindowState.Maximized
                maxState = ButtonState.Checked
            Else
                Me.WindowState = FormWindowState.Normal
                maxState = ButtonState.Normal
            End If
        ElseIf closeState = ButtonState.Pushed Then
            Me.Close()
        End If

    End Sub

    'Handle this event to maxmize/normalize the form via double clicking the title bar.
    Private Sub CustomBorderColorForm_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDoubleClick
        If (titleRect.Contains(e.Location)) Then
            If Me.WindowState = FormWindowState.Normal Then
                Me.WindowState = FormWindowState.Maximized
                maxState = ButtonState.Checked
            Else
                Me.WindowState = FormWindowState.Normal
                maxState = ButtonState.Normal
            End If
        End If
    End Sub
End Class



This is another sample shows how to create a form A with customized caption bar and flexible border:
http://www.codeproject.com/KB/cs/CustomizedFormByArijit.aspx.
If you feel it hard to read the C# code, you can follow this link to change the code to VB.Net:
http://www.developerfusion.com/tools/convert/csharp-to-vb/.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byPdNet Wednesday, August 26, 2009 1:56 PM
  •  
Aland Li  Wednesday, August 26, 2009 8:09 AM

Hi PdNet,

As far as I know, there is no direct way to change the border color of the form. We can set the FormBorderStyle to none to hide the title bar and borders of the form. Then we can do some custom painting to draw the borders and title bar by ourselves. This is the code snippet:

'A form with custom border and title bar.
'Some functions, such as resize the window via mouse, are not implemented yet. 
Public Class CustomBorderColorForm

    'The color and the width of the border.
    Private borderColor As Color = Color.GreenYellow
    Private borderWidth As Integer = 3
    'The color and region of the header.
    Private headerColor As Color = Color.GreenYellow
    Private headerRect As Rectangle
    'The region of the client.
    Private clientRect As Rectangle
    'The region of the title text.
    Private titleRect As Rectangle
    'The region of the minimum button.
    Private miniBoxRect As Rectangle
    'The region of the maximum button.
    Private maxBoxRect As Rectangle
    'The region of the close button.
    Private closeBoxRect As Rectangle
    'The states of the three header buttons.
    Private miniState As ButtonState
    Private maxState As ButtonState
    Private closeState As ButtonState
    'Store the mouse down point to handle moving the form.
    Private x As Integer = 0
    Private y As Integer = 0
    'The height of the header.
    Const HEADER_HEIGHT As Integer = 25
    'The size of the header buttons.
    ReadOnly BUTTON_BOX_SIZE As Size = New Size(15, 15)

    Private Sub CustomBorderColorForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Hide the border and the title bar.
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    End Sub

    Private Sub CustomBorderColorForm_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        'Draw the header.
        Using b As Brush = New SolidBrush(borderColor)
            e.Graphics.FillRectangle(b, headerRect)
        End Using
        'Draw the title text
        Using b As Brush = New SolidBrush(Me.ForeColor)
            e.Graphics.DrawString(Me.Text, Me.Font, b, titleRect)
        End Using
        'Draw the header buttons.
        If Me.MinimizeBox Then
            ControlPaint.DrawCaptionButton(e.Graphics, miniBoxRect, CaptionButton.Minimize, miniState)
        End If
        If Me.MinimizeBox Then
            ControlPaint.DrawCaptionButton(e.Graphics, maxBoxRect, CaptionButton.Maximize, maxState)
        End If
        If Me.MinimizeBox Then
            ControlPaint.DrawCaptionButton(e.Graphics, closeBoxRect, CaptionButton.Close, closeState)
        End If
        'Draw the border.
        ControlPaint.DrawBorder(e.Graphics, clientRect, borderColor, _
            borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid)
    End Sub

    'Handle resize to adjust the region ot border, header and so on.
    Private Sub CustomBorderColorForm_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        headerRect = New Rectangle(Me.ClientRectangle.Location, New Size(Me.ClientRectangle.Width, HEADER_HEIGHT))
        clientRect = New Rectangle(New Point(Me.ClientRectangle.Location.X, Me.ClientRectangle.Y + HEADER_HEIGHT), _
            New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height - HEADER_HEIGHT))
        Dim yOffset = (headerRect.Height + borderWidth - BUTTON_BOX_SIZE.Height) / 2
        titleRect = New Rectangle(yOffset, yOffset, _
                                Me.ClientRectangle.Width - 3 * (BUTTON_BOX_SIZE.Width + 1) - yOffset, _
                                BUTTON_BOX_SIZE.Height)
        miniBoxRect = New Rectangle(Me.ClientRectangle.Width - 3 * (BUTTON_BOX_SIZE.Width + 1), _
                                    yOffset, BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
        maxBoxRect = New Rectangle(Me.ClientRectangle.Width - 2 * (BUTTON_BOX_SIZE.Width + 1), _
                                    yOffset, BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
        closeBoxRect = New Rectangle(Me.ClientRectangle.Width - 1 * (BUTTON_BOX_SIZE.Width + 1), _
                                    yOffset, BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
        Me.Invalidate()
    End Sub


    Private Sub CustomBorderColorForm_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        'Start to move the form.
        If (titleRect.Contains(e.Location)) Then
            x = e.X
            y = e.Y
        End If

        'Check and press the header buttons.
        Dim mousePos As Point = Me.PointToClient(Control.MousePosition)
        If (miniBoxRect.Contains(mousePos)) Then
            miniState = ButtonState.Pushed
        ElseIf (maxBoxRect.Contains(mousePos)) Then
            maxState = ButtonState.Pushed
        ElseIf (closeBoxRect.Contains(mousePos)) Then
            closeState = ButtonState.Pushed
        End If

    End Sub

    Private Sub CustomBorderColorForm_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        'Move and refresh.
        If (x <> 0 And y <> 0) Then
            Me.Location = New Point(Me.Left + e.X - x, Me.Top + e.Y - y)
            Me.Refresh()
        End If

    End Sub

    Private Sub CustomBorderColorForm_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        'Reset the mouse point.
        x = 0
        y = 0

        'Check the button states and modify the window state.
        If miniState = ButtonState.Pushed Then
            Me.WindowState = FormWindowState.Minimized
            miniState = ButtonState.Normal
        ElseIf maxState = ButtonState.Pushed Then
            If Me.WindowState = FormWindowState.Normal Then
                Me.WindowState = FormWindowState.Maximized
                maxState = ButtonState.Checked
            Else
                Me.WindowState = FormWindowState.Normal
                maxState = ButtonState.Normal
            End If
        ElseIf closeState = ButtonState.Pushed Then
            Me.Close()
        End If

    End Sub

    'Handle this event to maxmize/normalize the form via double clicking the title bar.
    Private Sub CustomBorderColorForm_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDoubleClick
        If (titleRect.Contains(e.Location)) Then
            If Me.WindowState = FormWindowState.Normal Then
                Me.WindowState = FormWindowState.Maximized
                maxState = ButtonState.Checked
            Else
                Me.WindowState = FormWindowState.Normal
                maxState = ButtonState.Normal
            End If
        End If
    End Sub
End Class



This is another sample shows how to create a form A with customized caption bar and flexible border:
http://www.codeproject.com/KB/cs/CustomizedFormByArijit.aspx.
If you feel it hard to read the C# code, you can follow this link to change the code to VB.Net:
http://www.developerfusion.com/tools/convert/csharp-to-vb/.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byPdNet Wednesday, August 26, 2009 1:56 PM
  •  
Aland Li  Wednesday, August 26, 2009 8:09 AM
Yehh..

Its Work But.

How do I work when My Form's Width and Height Change After Form Load (After Doing Some Work).

Paint Event Fire at the Form Load. If I change my Form's Size Then The Form Border little Shorter.

How Can I do That? Please Help Me
PdNet  Wednesday, August 26, 2009 1:59 PM
Hi PdNet,

In my code snippet, I add a handler named CustomBorderColorForm_Resizeto the Resize event of the Form. The handler adjust the size of the borders according to the form size, so it ought to be shown correctly. Could you please provide more information about your issue?

Regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 27, 2009 2:03 AM

You can use google to search for other answers

Custom Search

More Threads

• Stored procedure vs Query in VB.Net and backend - SQL Server 2000?
• Custom ControlDesigner for a user control hosted in Internet Explorer
• mimic MaskedTextBox.TextMaskFormat
• Wanted: Textbox listing links (ala the Outlook "To" field)
• Adjusting Combo Box Height Property ... when combo box is embedded in a control
• How can I get my Windows Forms Designer (in vb2005/.net Framework 2) started?
• Docked/Anchored UserControl in Vista -- child controls don't size properly
• Databinding
• removing inherited properties
• Deleting Child controls when the Parent Control is deleted???