antonis lambrianides
antonis lambrianides

Reputation: 423

Hide Taskbar and start button windows 7 32bit vb.net

I am trying to hide the taskbar and the start button when my app opens and show them back on when i close it. I manage to do this for an 64bit version of the app, but when i set it to 32bit in visual studio in target cpu i get an exception 'Arithmetic operation resulted in an overflow'.

Here are the methods i use and work for 64bit.

Public Class frmShowHideStartBar
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Const SW_HIDE = 0
    Private Const SW_SHOW = 1


    Public Function HideStartButton() As Boolean
        Dim retval = False
        Try
            HideTaskBar()
            Dim hwndStartButton = FindWindow("Button", "Start")
            If hwndStartButton <> IntPtr.Zero Then
                retval = ShowWindow(hwndStartButton, SW_HIDE)
            End If
        Catch ex As Exception
            MsgBox("HideStartButton " + ex.Message)
        End Try
        Return retval
    End Function
    Public Function HideTaskBar() As Boolean
        Dim retval = False
        Try
            Dim hwndTaskBar = FindWindow("Shell_TrayWnd", "")
            If hwndTaskBar <> IntPtr.Zero Then
                retval = ShowWindow(hwndTaskBar, SW_HIDE)
            End If
        Catch ex As Exception
            MsgBox("HideTaskBar " + ex.Message)
        End Try
        Return retval
    End Function
    Public Function ShowStartButton() As Boolean
        Dim retval1 = False
        Try
            ShowHideTaskBar()
            Dim hwndstartbutton = FindWindow("Button", "Start")
            If hwndstartbutton <> IntPtr.Zero Then
                retval1 = ShowWindow(hwndstartbutton, SW_SHOW)
            End If
        Catch ex As Exception
            MsgBox("ShowStartButton " + ex.Message)
        End Try
        Return retval1
    End Function
    Public Function ShowHideTaskBar() As Boolean
        Dim retval2 = False
        Try
            Dim hwndTaskBar = FindWindow("Shell_TrayWnd", "")
            If hwndTaskBar <> IntPtr.Zero Then
                retval2 = ShowWindow(hwndTaskBar, SW_SHOW)
            End If
        Catch ex As Exception
            MsgBox("ShowHideTaskBar " + ex.Message)
        End Try
        Return retval2
    End Function
End Class

I tried setting these instead of long to integer, and it works for the hide, but its not working for the unhide. Any ideas on how to do it for 32bit?

Upvotes: 1

Views: 4256

Answers (2)

Karen Payne
Karen Payne

Reputation: 5102

Here is a language extension method that should work just fine on Win7.

Working example on OneDrive.

Usage

Private Sub frmMainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    FullScreen(True)
End Sub

Extensions

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></remarks>
    <System.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>
    <System.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

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244722

Do not do this the way you're trying to do it! You are working too hard and just begging for bugs!

There is a simpler solution: if you want to create a full-screen window that covers up the taskbar, then just do it and let the taskbar get out of the way automatically. The linked blog post explains all of the details, but the code is written in C++, which may be difficult to understand if you're writing VB.NET.

Fortunately, this is all quite nicely wrapped by the WinForms framework. All you need to do is the following:

Public Class frmFullScreen

   Public Sub MakeFullScreen()
      ' Hide the window borders.
      Me.FormBorderStyle = FormBorderStyle.None

      ' Change the size and location of the form so that it fills entire screen.
      ' (This works correctly with multiple monitors; the form fills the screen that it is on or closest to.)
      Dim rect As Rectangle = Screen.GetBounds(Me)
      Me.Location = rect.Location
      Me.Size = rect.Size
   End Sub

   Public Sub MakeNormal()
      ' Restore the standard window borders (or any other style you like).
      Me.FormBorderStyle = FormBorderStyle.Sizable

      ' Change the form's size back to its default size, and
      ' set the location automatically by centering it.
      Me.Size = Me.DefaultSize
      Me.CenterToScreen()
   End Sub

End Class

Fully tested and working, even with multiple monitors. When full-screen, the form covers the entire screen, including the taskbar, start menu, and anything else that happens to be there.

To make it go, add a button or something so that you can wire up its event handler to call the MakeFullScreen and/or MakeNormal methods.

Upvotes: 3

Related Questions