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

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

    I followed @Quispie answer but it didn't worked at first in Windows 10, but gave me the foundation and source to solve it (so kudos) and also http://www.pinvoke.net/.

    [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 int cbSize; // initialize this field using: Marshal.SizeOf(typeof(APPBARDATA));
        public IntPtr hWnd;
        public uint uCallbackMessage;
        public uint uEdge;
        public RECT rc;
        public int lParam;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left, Top, Right, Bottom;
    
        public RECT(int left, int top, int right, int bottom)
        {
            Left = left;
            Top = top;
            Right = right;
            Bottom = bottom;
        }
    
        public RECT(System.Drawing.Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }
    
        public int X
        {
            get { return Left; }
            set { Right -= (Left - value); Left = value; }
        }
    
        public int Y
        {
            get { return Top; }
            set { Bottom -= (Top - value); Top = value; }
        }
    
        public int Height
        {
            get { return Bottom - Top; }
            set { Bottom = value + Top; }
        }
    
        public int Width
        {
            get { return Right - Left; }
            set { Right = value + Left; }
        }
    
        public System.Drawing.Point Location
        {
            get { return new System.Drawing.Point(Left, Top); }
            set { X = value.X; Y = value.Y; }
        }
    
        public System.Drawing.Size Size
        {
            get { return new System.Drawing.Size(Width, Height); }
            set { Width = value.Width; Height = value.Height; }
        }
    
        public static implicit operator System.Drawing.Rectangle(RECT r)
        {
            return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
        }
    
        public static implicit operator RECT(System.Drawing.Rectangle r)
        {
            return new RECT(r);
        }
    
        public static bool operator ==(RECT r1, RECT r2)
        {
            return r1.Equals(r2);
        }
    
        public static bool operator !=(RECT r1, RECT r2)
        {
            return !r1.Equals(r2);
        }
    
        public bool Equals(RECT r)
        {
            return r.Left == Left && r.Top == Top && r.Right == Right && r.Bottom == Bottom;
        }
    
        public override bool Equals(object obj)
        {
            if (obj is RECT)
                return Equals((RECT)obj);
            else if (obj is System.Drawing.Rectangle)
                return Equals(new RECT((System.Drawing.Rectangle)obj));
            return false;
        }
    
        public override int GetHashCode()
        {
            return ((System.Drawing.Rectangle)this).GetHashCode();
        }
    
        public override string ToString()
        {
            return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
        }
    }
    
    
    public enum AppBarStates
    {
        AlwaysOnTop = 0x00,
        AutoHide = 0x01
    }
    
    /// 
    /// Set the Taskbar State option
    /// 
    /// AppBarState to activate
    public void SetTaskbarState(AppBarStates option)
    {
        APPBARDATA msgData = new APPBARDATA();
        msgData.cbSize = Marshal.SizeOf(msgData);
        msgData.hWnd = FindWindow("System_TrayWnd", null);
        msgData.lParam = (int)option;
        SHAppBarMessage((UInt32)AppBarMessages.SetState, ref msgData);
    }
    
    /// 
    /// Gets the current Taskbar state
    /// 
    /// current Taskbar state
    public AppBarStates GetTaskbarState()
    {
        APPBARDATA msgData = new APPBARDATA();
        msgData.cbSize = Marshal.SizeOf(msgData);
        msgData.hWnd = FindWindow("System_TrayWnd", null);
        return (AppBarStates)SHAppBarMessage((UInt32)AppBarMessages.GetState, ref msgData);
    }
    

提交回复
热议问题