How to toggle/switch Windows taskbar from “show” to “auto-hide” (and vice-versa)?

后端 未结 7 2231
忘掉有多难
忘掉有多难 2021-02-15 14:58

Basically I want to make simple toggle program (that will be mapped to some keyboard shortcut) that set taskbar to auto-hide mode if in normal mode (and conversely, to normal sh

相关标签:
7条回答
  • 2021-02-15 15:27

    VB "show" and "auto-hide" the Taskbar - Windows 10

    I have translated this in VB, which might be usefull for others (Windows 10; should work in 32bit an 64bit):

        Option Explicit On
        Option Strict On
        Imports System.Runtime.InteropServices
    
        Module WindowsTaskbarSettings
    
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer
    
        'https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shappbarmessage
        'https://docs.microsoft.com/nl-nl/windows/win32/api/shellapi/ns-shellapi-appbardata
    
        'https://docs.microsoft.com/en-us/windows/win32/shell/abm-getstate 'requires csize to be set
        'https://docs.microsoft.com/en-us/windows/win32/shell/abm-setstate 'requires hwnd and csize to be set
    
        Structure APPBARDATA
            Dim cbSize As Integer
            Dim hwnd As Long
            Dim uCallbackMessage As Integer '[Delegate]
            Dim uEdge As Integer
            Dim rc As RECT
            Dim lParam As Integer 'message specific, see 
        End Structure
    
        Structure RECT
            Dim Left As Integer
            Dim Top As Integer
            Dim Right As Integer
            Dim Bottom As Integer
        End Structure
    
        Public Enum AppBarMessages
            Newx = &H0
            Remove = &H1
            QueryPos = &H2
            SetPos = &H3
            GetState = &H4
            GetTaskBarPos = &H5
            Activate = &H6
            GetAutoHideBar = &H7
            SetAutoHideBar = &H8
            WindowPosChanged = &H9
            SetState = &HA
        End Enum
    
        Public Enum AppBarStates
            AutoHide = &H1
            AlwaysOnTop = &H2
        End Enum
    
        Public Sub AutoHide_Toggle()
    
            If GetTaskbarStateAutoHide() Then
                SetTaskbarState(AppBarStates.AlwaysOnTop)
            Else
                SetTaskbarState(AppBarStates.AutoHide)
            End If
        End Sub
    
        Public Sub SetTaskbarState(StateOption As AppBarStates)
            'sets the Taskbar State to StateOption (AllwaysOnTop or AutoHide)
    
            Dim msgData As New APPBARDATA
            msgData.cbSize =  Marshal.SizeOf(msgData)
             'not necessary to use handle of Windows Taskbar, but can be found by
             'msgData.hwnd = CInt(FindWindow("Shell_TrayWnd", ""))
    
            'Set the State which will be requested
            msgData.lParam = StateOption
    
            'Ansd send the message to set this state
            SHAppBarMessage(AppBarMessages.SetState, msgData)
            'Remark on my small (1280x800) screen the desktop area remains the same, but on my larger (1080x1920) screen
            'the desktop icons are displaced when autohide is set on !!! Don't understand why (it then thinks the screen is only 800 high)
        End Sub
    
        Public Function GetTaskbarStateAutoHide() As Boolean
            'true if AutoHide is on, false otherwise
    
            Dim msgData As New APPBARDATA
            Dim ret As Integer
            msgData.cbSize =  Marshal.SizeOf(msgData)
            ' also here not necessay to find handle to Windows Taskbar
    
            ret = SHAppBarMessage(AppBarMessages.GetState, msgData)
    
            GetTaskbarStateAutoHide = CBool(ret And &H1)
        End Function
    
    End Module
    
    
    0 讨论(0)
提交回复
热议问题