隐藏任务栏并启动按钮Windows 7 32bit vb.net

时间:2016-02-24 10:11:48

标签: vb.net

我正在尝试隐藏任务栏和我的应用程序打开时的开始按钮,并在我关闭它时显示它们。我设法为64位版本的应用程序执行此操作,但是当我在目标cpu中的visual studio中将其设置为32位时,我得到一个异常'算术运算导致溢出'。

以下是我使用的方法,适用于64位。

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

我尝试将这些设置为long而不是整数,并且它适用于隐藏,但它不适用于取消隐藏。 关于如何为32位做这个的任何想法?

2 个答案:

答案 0 :(得分:2)

按照您尝试的方式执行此操作!你工作太辛苦了,只是讨厌错误!

a simpler solution:如果你想创建一个覆盖任务栏的全屏窗口,那么就这样做,让任务栏自动完成。链接的博客文章解释了所有细节,但代码是用C ++编写的,如果您正在编写VB.NET,可能很难理解。

幸运的是,WinForms框架完全包含了这一切。您需要做的就是:

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

经过全面测试和工作,即使有多台显示器也是如此。全屏时,表单会覆盖整个屏幕,包括任务栏,开始菜单以及恰好在那里的任何其他内容。

要进行操作,请添加一个按钮或其他内容,以便您可以连接其事件处理程序以调用MakeFullScreen和/或MakeNormal方法。

答案 1 :(得分:0)

这是一种语言扩展方法,在Win7上应该可以正常工作。

Working example on OneDrive

用法

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

扩展

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