【问题标题】:Hide Taskbar and start button windows 7 32bit vb.net隐藏任务栏和开始按钮 windows 7 32bit vb.net
【发布时间】:2016-02-24 10:11:48
【问题描述】:

我试图在我的应用程序打开时隐藏任务栏和开始按钮,并在我关闭它时重新显示它们。我设法为 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 位执行此操作的任何想法?

【问题讨论】:

  • 这可能是XY Problem,而不是隐藏任务栏/菜单,而是想要make your form full screen?。如果/当您的应用崩溃时,用户将如何恢复任务栏/开始菜单?
  • 你在哪一行得到错误?

标签: vb.net


【解决方案1】:

不要不要按照你尝试的方式这样做!你太辛苦了,只求bug!

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 方法。

【讨论】:

    【解决方案2】:

    这是一种语言扩展方法,应该可以在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多