The following was published on MSDN code sample site where you can download the code for both C# and VB.NET
https://code.msdn.microsoft.com/Wind...da482a#content
This sample shows how to make a form cover the entire screen with an option to cover or not cover the Windows task bar. There are many reasons for this but the main reason is a form has many controls that requires either the user scrolls through the controls or simply depresses the X in the window title bar to maximism the window.
With that we must allow the user to remember this to either startup with maximized or not as the user may be fine with scolling so that when working with one monitor they can share the screen with another application that is used in tangent with your application.
Let's get started, we could place all the code presented in the attached solution directly into a form and be good yet it's a good idea to place the code into another class so that it is reusable and does not clutter up the form with this code. Best practices dicate place very little code into a form so that is what I have done with a slight twist which is place the code into a class project where each method is done as a language extension method.
Figure 1 is contained in a class project
Code:
Imports System.Windows.Forms
Public Module FormExtensions
Private Declare Function SetWindowPos Lib "user32.dll" _
Alias "SetWindowPos" (ByVal hWnd As IntPtr,
ByVal hWndIntertAfter As IntPtr,
ByVal X As Integer,
ByVal Y As Integer,
ByVal cx As Integer,
ByVal cy As Integer,
ByVal uFlags As Integer) As Boolean
Private HWND_TOP As IntPtr = IntPtr.Zero
Private Const SWP_SHOWWINDOW As Integer = 64
''' <summary>
''' Place form into full screen
''' </summary>
''' <param name="sender"></param>
''' <param name="TaskBar">True to hide Windows TaskBar</param>
''' <remarks>
''' Showing this task bar may not work fully but that is not the
''' point here, the point is to cover the task bar with a option
''' to expose it is secondary.
''' </remarks>
<Runtime.CompilerServices.Extension()>
Public Sub FullScreen(ByVal sender As Form, ByVal TaskBar As Boolean)
sender.WindowState = FormWindowState.Maximized
sender.FormBorderStyle = FormBorderStyle.None
sender.TopMost = True
If TaskBar Then
SetWindowPos(sender.Handle, HWND_TOP, 0, 0,
Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
SWP_SHOWWINDOW
)
End If
End Sub
''' <summary>
''' Restore to original size/position
''' </summary>
''' <param name="sender"></param>
''' <remarks></remarks>
<Runtime.CompilerServices.Extension()>
Public Sub NormalMode(ByVal sender As Form)
sender.WindowState = FormWindowState.Normal
sender.FormBorderStyle = FormBorderStyle.FixedSingle
sender.TopMost = True
End Sub
End Module
Figure 2 is come showing usage
Code:
Imports WindowsFormsMaxLibrary
Public Class frmMainForm
Private Sub cmdChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChange.Click
If cmdChange.Text = "Full" Then
cmdChange.Text = "Normal"
FullScreen(chkTaskbar.Checked)
If chkTaskbar.Checked Then
Me.FullScreen(True)
End If
Else
cmdChange.Text = "Full"
NormalMode()
End If
End Sub
Private Sub cmdShowChildForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShowChildForm.Click
Dim f As New frmChildForm
Dim TopMostSetting As Boolean = Me.TopMost
Try
Me.TopMost = False
f.ShowDialog()
Finally
f.Dispose()
Me.TopMost = TopMostSetting
End Try
End Sub
Private Sub cmdDetect_Click(sender As Object, e As EventArgs) Handles cmdDetect.Click
MessageBox.Show(Me.WindowState.ToString)
End Sub
Private Sub frmMainForm_StyleChanged(sender As Object, e As EventArgs) Handles Me.StyleChanged
ListBox1.Items.Add(Me.WindowState.ToString)
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
End Sub
Private Sub cmdCloseForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCloseForm.Click
Close()
End Sub
End Class