Full-screen Windows app using VB.NET

Share on facebook
Share on twitter
Share on linkedin

Recently I had to write a Windows application that required the main form to run in full-screen mode. This means no title bar and with the window appearance above everything else, i.e. Start Button, taskbar, system tray and all other apps.

This requires an API call to the SetWindowPos function which you need to create an alias to before you can call it. The first part of the code, which should be placed in the declarations part of the form, looks like this :

SetWindowsPos alias:

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

You also need an alias to the API function called GetSystemMetrics, like this :

VB.NET – GetSystemMetrics alias:

Private Declare Function GetSystemMetrics Lib "user32.dll" Alias "GetSystemMetrics" (ByVal Which As Integer) As Integer

Following this you need to declare 4 constants :

Required constants:

Private Const SM_CXSCREEN As Integer = 0
Private Const SM_CYSCREEN As Integer = 1
Private Shared HWND_TOP As IntPtr = IntPtr.Zero
Private Const SWP_SHOWWINDOW As Integer = 64

Then 2 public properties :

Required property #1:

Public ReadOnly Property ScreenX() As Integer
        Return GetSystemMetrics(SM_CXSCREEN)
    End Get
End Property

-- and --

Required property #2:

Public ReadOnly Property ScreenY() As Integer
        Return GetSystemMetrics(SM_CYSCREEN)
    End Get
End Property

After this you need to write a simple sub routine to use the constants you declared above and that calls the SetWindowPos API function. The function's code looks like this :

The FullScreen() method:

Private Sub FullScreen()
    Me.WindowState = FormWindowState.Maximized
    Me.FormBorderStyle = FormBorderStyle.None
    Me.TopMost = True
    SetWindowPos(Me.Handle, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW)
End Sub

Beyond that it's just a case of calling the FullScreen sub routine whenever you want the application to show in full-screen mode. Easy huh? 🙂