Share via


How can I change the Form Border Style's color?

Question

Wednesday, July 22, 2015 6:34 AM

Is there a way to change the Form Border Style's color?

All replies (4)

Wednesday, July 22, 2015 7:18 AM ✅Answered | 1 vote

Is there a way to change the Form Border Style's color?

There is some discussion and a possible solution in the link below.  As this task is managed by Windows according to options set by the user, it is not a simple procedure.

http://stackoverflow.com/questions/1445472/how-to-change-the-form-border-color-c


Wednesday, July 22, 2015 7:21 AM ✅Answered | 1 vote

It is possible, but not easy, have a look at

http://customerborderform.codeplex.com/

and

Drawing Custom Borders in Windows Forms Part 1, Part 2, and Part 3


Wednesday, July 22, 2015 9:36 AM ✅Answered | 1 vote

If you want to make the effort, you can set to a borderless form and draw the border yourself.

This example uses WndProc to make the form resizable at the lower right corner. You can move the form with the header. There are some other things drawn onto the form for fun. You can get as fancy as you can draw.

Option Strict On
Imports System.Runtime.InteropServices
Public Class Form4
    'resizeable borderless form using wndProc
    Private WithEvents button1 As New Button With {.Parent = Me, .Width = 24, .Height = 30, .Text = "X", .Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold), .ForeColor = Color.Firebrick}
    Private flagbmp As New Bitmap("c:\bitmaps\us flag2.jpg")
    Private Const cGrip As Integer = 16
    Private Const cCaption As Integer = 32
    Private metroRed As Color = Color.FromArgb(255, 240, 46, 19)
    Private metroGold As Color = Color.FromArgb(255, 249, 185, 59)
    Private metroMaroon As Color = Color.FromArgb(255, 165, 58, 84)
    Private metroBlue As Color = Color.FromArgb(255, 31, 135, 198)
    Private metroSilver As Color = Color.FromArgb(255, 189, 185, 184)
    Private metroBlack As Color = Color.FromArgb(255, 37, 37, 37)
    Private metrocharcol As Color = Color.FromArgb(255, 87, 87, 87)
    Private metroSlate As Color = Color.FromArgb(255, 117, 117, 117)


    Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
        Me.Text = String.Empty
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.BackColor = metroSilver

    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = &H84 Then
            ' Trap WM_NCHITTEST
            Dim pos As New Point(m.LParam.ToInt32() And &HFFFF, m.LParam.ToInt32() >> 16)
            pos = Me.PointToClient(pos)
            If pos.Y < cCaption Then
                m.Result = IntPtr.op_Explicit(2)
                ' HTCAPTION
                Return
            End If
            If pos.X >= Me.ClientSize.Width - cGrip AndAlso pos.Y >= Me.ClientSize.Height - cGrip Then
                m.Result = IntPtr.op_Explicit(17)
                ' HTBOTTOMRIGHT
                Return
            End If
        End If
        MyBase.WndProc(m)
    End Sub

    Private Sub Form6_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim rect As Rectangle = New Rectangle(3, 3, Me.ClientRectangle.Width - 6, Me.ClientRectangle.Height - 6)
        e.Graphics.DrawRectangle(New Pen(metrocharcol, 8), Me.ClientRectangle)
        e.Graphics.DrawRectangle(New Pen(metroMaroon, 2), rect)
        e.Graphics.FillRectangle(New SolidBrush(metroSlate), 0, 0, Me.ClientRectangle.Width, 32)
        e.Graphics.DrawString("My Form", New Font("Script MT Bold", 14), Brushes.WhiteSmoke, 100, 4)
        e.Graphics.DrawImage(flagbmp, 5, 2)
    End Sub

    Private Sub Form6_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        button1.Left = Me.ClientSize.Width - (button1.Width + 1)
        Me.Refresh()
    End Sub

    Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
        Me.Close()
    End Sub
End Class

Wednesday, July 22, 2015 8:25 PM ✅Answered | 1 vote

The code at the codeplex link would be the way to do this correctly.

This method requires a second Form for the color that will display through the aeroglass area of the primary Form. With the second Form having no FormBorderStyle. Plus the corners of the second Form are visible against the rounder corners of the primary Form, just barely on a small monitor.

The second Form's opacity is set from .1 to .7 giving it a color and making it slightly transparent. The higher the number the darker the color and less the transparency. Opacity 0 is completely opaque and 1 is non-opaque.

Also both Forms are required to be TopMost with Form1 over Form2. Which means they will cover any other app on the desktop unless minimized.

Form1 Code.

Option Strict On

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        NumericUpDown1.Minimum = 0
        NumericUpDown1.Maximum = 10
        NumericUpDown1.Value = 2
        Timer1.Interval = 600
        Timer1.Start()
    End Sub

    Private Sub Form1_Move(sender As Object, e As EventArgs) Handles Me.Move
        Form2.Location = Me.Location
        Form2.Size = Me.Size
    End Sub

    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            Form2.Hide()
        ElseIf Me.WindowState = FormWindowState.Maximized Then
            Timer1.Interval = 250
            Timer1.Start()
        ElseIf Me.WindowState = FormWindowState.Normal Then
            Timer1.Interval = 250
            Timer1.Start()
        End If
        Form2.Size = Me.Size
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using CD As New ColorDialog
            With CD
                .AllowFullOpen = True
                .AnyColor = True
                .FullOpen = True
            End With
            If CD.ShowDialog = Windows.Forms.DialogResult.OK Then
                Form2.ColorToUse = CD.Color
                Form2.Refresh()
            End If
        End Using
    End Sub

    Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
        Form2.Opacity = CDbl(NumericUpDown1.Value * 0.1)
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        Form2.Size = Me.Size
        Form2.Show()
        Form2.Refresh()
        Form2.Location = Me.Location
        Me.BringToFront()
    End Sub

    Protected Overrides ReadOnly Property CreateParams() As CreateParams ' This code keeps Form at the top of the z order
        Get
            Const WS_EX_TOPMOST As Integer = &H8
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or WS_EX_TOPMOST
            Return cp
        End Get
    End Property

End Class

Form2 Code.

Option Strict On

Public Class Form2

    Public ColorToUse As Color = Color.Red

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.ShowInTaskbar = False
        Me.DoubleBuffered = True
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.Opacity = 0.2
    End Sub

    Private Sub Form2_Move(sender As Object, e As EventArgs) Handles Me.Move
        Me.Refresh()
    End Sub

    Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Me.Refresh()
    End Sub

    Private Sub Form2_GotFocus(sender As Object, e As EventArgs) Handles Me.GotFocus
        Form1.BringToFront()
    End Sub

    Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Using b As New SolidBrush(ColorToUse)
            e.Graphics.FillRectangle(b, New Rectangle(0, 0, Me.Width, Me.Height))
        End Using
    End Sub

    Protected Overrides ReadOnly Property CreateParams() As CreateParams ' This code keeps Form at the top of the z order
        Get
            Const WS_EX_TOPMOST As Integer = &H8
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or WS_EX_TOPMOST
            Return cp
        End Get
    End Property

End Class

La vida loca