Public Class Form1

    Dim CloseButton As New Close
    Dim RestoreButton As New Restore
    Dim MinButton As New Minimise

    Dim Drag As Boolean
    Dim MouseX As Integer
    Dim MouseY As Integer

    Dim WinBorder As New PictureBox

    Private Sub CloseButtonCode() Handles Me.Load
        CloseButton.Anchor = AnchorStyles.Top + AnchorStyles.Right
        CloseButton.Enabled = True
        CloseButton.Location = New Point(1170, 0)
        CloseButton.Size = New Size(30, 30)
        CloseButton.Visible = True
        AddHandler CloseButton.Click, AddressOf CloseButtonClick
    End Sub

    Private Sub CloseButtonClick()
    End Sub

    Private Sub RestoreButtonCode() Handles Me.Load
        RestoreButton.Anchor = AnchorStyles.Top + AnchorStyles.Right
        RestoreButton.Enabled = True
        RestoreButton.Location = New Point(1140, 0)
        RestoreButton.Size = New Size(30, 30)
        RestoreButton.Visible = True
        AddHandler RestoreButton.Click, AddressOf RestoreButtonClick
    End Sub

    Private Sub RestoreButtonClick()
        If Me.WindowState = FormWindowState.Normal Then
            Me.Height = Screen.PrimaryScreen.WorkingArea.Height
            Me.Width = Screen.PrimaryScreen.WorkingArea.Width
            Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
            Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
            Me.WindowState = FormWindowState.Normal
        End If
    End Sub

    Private Sub MinButtonCode() Handles Me.Load
        MinButton.Anchor = AnchorStyles.Top + AnchorStyles.Right
        MinButton.Enabled = True
        MinButton.Location = New Point(1110, 0)
        MinButton.Size = New Size(30, 30)
        MinButton.Visible = True
        AddHandler MinButton.Click, AddressOf MinButtonClick
    End Sub

    Private Sub MinButtonClick()
        Me.WindowState = FormWindowState.Minimized
    End Sub

    Private Sub Border()
        WinBorder.Anchor = AnchorStyles.Top + AnchorStyles.Right + AnchorStyles.Left
        WinBorder.Enabled = True
        WinBorder.Location = New Point(0, 0)
        WinBorder.Size = New Size(1200, 30)
        WinBorder.Visible = True
        AddHandler WinBorder.MouseDown, AddressOf WinBorderMouseDown
        AddHandler WinBorder.MouseMove, AddressOf WinBorderMouseMove
        AddHandler WinBorder.MouseUp, AddressOf WinBorderMouseUp
    End Sub

    Private Sub WinBorderMouseDown()
        Drag = True
        MouseX = Windows.Forms.Cursor.Position.X - Me.Left
        MouseY = Windows.Forms.Cursor.Position.Y - Me.Top
    End Sub

    Private Sub WinBorderMouseMove()
        If Drag Then
            Me.Top = Windows.Forms.Cursor.Position.Y - MouseY
            Me.Left = Windows.Forms.Cursor.Position.X - MouseX
        End If
    End Sub

    Private Sub WinBorderMouseUp()
        Drag = False
    End Sub

End Class

Basically I've got 2 problems.

1) The Maximise / Restore Button Maximises my window but doesn't restore it to the normal window state.
2) The PictureBox (WinBorder) is coded to move the form when mouse is pressed. But it's like the PictureBox isn't there.

PS: Close, Restore & Minimise are all custom buttons created within the program in another class.


It's better to play with ControlBox Property of form than to code for Maximize, Minimize buttons.

This article has been dead for over six months. Start a new discussion instead.