c# 设置自动隐藏任务栏、获取状态

from:

http://stackoverflow.com/questions/1381821/how-to-toggle-switch-windows-taskbar-from-show-to-auto-hide-and-vice-versa

[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 =
        0x00000000,
        Remove =
        0x00000001,
        QueryPos =
        0x00000002,
        SetPos =
        0x00000003,
        GetState =
        0x00000004,
        GetTaskBarPos =
        0x00000005,
        Activate =
        0x00000006,
        GetAutoHideBar =
        0x00000007,
        SetAutoHideBar =
        0x00000008,
        WindowPosChanged =
        0x00000009,
        SetState =
        0x0000000a
    }

    [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 =
        0x00000001,
        AlwaysOnTop =
        0x00000002
    }

/// 
    /// 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();


转载于:https://www.cnblogs.com/runliuv/p/3501769.html

你可能感兴趣的:(c# 设置自动隐藏任务栏、获取状态)