最大化表单但不覆盖任务栏

问题描述

当我单击按钮最大化表单时,它会覆盖整个屏幕,包括任务栏。我设法找到了一个解决方案并且它有效,我在表单加载事件中使用了我的代码,但我无法将表单返回到正常状态。

Private Sub frmDashboard_Load(sender As Object,e As EventArgs) Handles MyBase.Load
    Me.Top = Screen.PrimaryScreen.WorkingArea.Top
    Me.Left = Screen.PrimaryScreen.WorkingArea.Left
    Me.Height = Screen.PrimaryScreen.WorkingArea.Height
    Me.Width = Screen.PrimaryScreen.WorkingArea.Width        
End Sub

Private Sub btnMaximizeMin_Click(sender As Object,e As EventArgs) Handles btnMaxMin.Click
    If Me.WindowState = FormWindowState.normal Then
        'maximize but dont cover taskbar
        Me.Top = Screen.PrimaryScreen.WorkingArea.Top
        Me.Left = Screen.PrimaryScreen.WorkingArea.Left
        Me.Height = Screen.PrimaryScreen.WorkingArea.Height
        Me.Width = Screen.PrimaryScreen.WorkingArea.Width
    Else
        Me.WindowState = FormWindowState.normal
    End If
End Sub

解决方法

问题在于您没有最大化表单。您特别不想最大化表单,因为它覆盖了 Windows 任务栏。您不能将 WindowState "back" 设置为 Normal,因为它已经处于该状态,因为它永远不会离开该状态。由您自己记住状态以及之前的界限,例如

Private isMaximised As Boolean = False
Private normalBounds As Rectangle

Private Sub MaximiseOrRestore()
    isMaximised = Not isMaximised

    If isMaximised Then
        normalBounds = Bounds
        Bounds = Screen.PrimaryScreen.WorkingArea
    Else
        Bounds = normalBounds
    End If
End Sub