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

后端 未结 7 2212
忘掉有多难
忘掉有多难 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

    Here are the functions I use:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow(string strClassName, string strWindowName);
    
    [DllImport("shell32.dll")]
    public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData);
    
    public enum AppBarMessages
    {
        New              = 0x00,
        Remove           = 0x01,
        QueryPos         = 0x02,
        SetPos           = 0x03,
        GetState         = 0x04,
        GetTaskBarPos    = 0x05,
        Activate         = 0x06,
        GetAutoHideBar   = 0x07,
        SetAutoHideBar   = 0x08,
        WindowPosChanged = 0x09,
        SetState         = 0x0a
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public struct APPBARDATA
    {
        public UInt32 cbSize;
        public IntPtr hWnd;
        public UInt32 uCallbackMessage;
        public UInt32 uEdge;
        public Rectangle rc;
        public Int32 lParam;
    }
    
    public enum AppBarStates
    {
        AutoHide    = 0x01,
        AlwaysOnTop = 0x02
    }
    
    /// 
    /// Set the Taskbar State option
    /// 
    /// AppBarState to activate
    public void SetTaskbarState(AppBarStates option)
    {
        APPBARDATA msgData = new APPBARDATA();
        msgData.cbSize = (UInt32)Marshal.SizeOf(msgData);
        msgData.hWnd = FindWindow("System_TrayWnd", null);
        msgData.lParam = (Int32)(option);
        SHAppBarMessage((UInt32)AppBarMessages.SetState, ref msgData);
    }
    
    /// 
    /// Gets the current Taskbar state
    /// 
    /// current Taskbar state
    public AppBarStates GetTaskbarState()
    {
        APPBARDATA msgData = new APPBARDATA();
        msgData.cbSize = (UInt32)Marshal.SizeOf(msgData);
        msgData.hWnd = FindWindow("System_TrayWnd", null);
        return (AppBarStates)SHAppBarMessage((UInt32)AppBarMessages.GetState, ref msgData);
    }
    

    When the code above is implemented just set the Taskbar to autohide by: SetTaskbarState(AppBarStates.AutoHide);

    Get the current state by:

    AppBarStates currentState = GetTaskbarState();
    

提交回复
热议问题