0

Hello,

I have below code that set customised form border color and title. the form is missing icon though.
kindly asist me on the below on how to add icon to the form as well.

thanks

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

'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

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by codeorder
0

Since you have titleRect to draw the Form's Title, why Not add an "iconRect" just prior to the .Location.X of the titleRect and draw the Form's Icon?

Edited by codeorder

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.