如何将Windows任务栏从“显示”切换/切换到“自动隐藏”(反之亦然)?

时间:2009-09-04 22:38:37

标签: c# windows animation taskbar

基本上我想制作简单的切换程序(将映射到某些键盘快捷键),如果处于正常模式,则将任务栏设置为自动隐藏模式(相反,如果处于自动隐藏状态,则设置为正常显示模式)。

你知道如何在C#中实现它吗? (或Win32 C ++,但任何实际上都可以做到的都很好。)

感谢。希望我已经说清楚了。

-

我真的不想要任何会与任务栏重叠的全屏应用程序,只有切换显示模式和退出的无窗口程序。我定期从自动隐藏切换到普通视图,并希望简化它。 (使用Win7。)

-

编辑。例如

#include <windows.h>

int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    SetWindowPos(FindWindow(L"Shell_traywnd", NULL ), 0, 0, 0, 0, 0, 0x40);
}

不会做的伎俩,它只显示任务栏,已经显示= true,但不会将其切换为自动隐藏。 (同样适用于0x80。)

8 个答案:

答案 0 :(得分:15)

以下是我使用的功能:

[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
}

/// <summary>
/// Set the Taskbar State option
/// </summary>
/// <param name="option">AppBarState to activate</param>
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);
}

/// <summary>
/// Gets the current Taskbar state
/// </summary>
/// <returns>current Taskbar state</returns>
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);
}

执行上述代码时,只需将任务栏设置为自动隐藏:SetTaskbarState(AppBarStates.AutoHide);

通过以下方式获取当前状态:

AppBarStates currentState = GetTaskbarState();

答案 1 :(得分:6)

我跟着@Quispie回答但是它在Windows 10中没有起作用,但给了我解决它的基础和来源(所以荣誉)和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
}

/// <summary>
/// Set the Taskbar State option
/// </summary>
/// <param name="option">AppBarState to activate</param>
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);
}

/// <summary>
/// Gets the current Taskbar state
/// </summary>
/// <returns>current Taskbar state</returns>
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);
}

答案 2 :(得分:3)

隐藏任务栏

这是一个比C#更多的WIN32 API相关问题。您可以使用this(当然需要转换为点网)来隐藏任务栏。

您可以使用http://www.pinvoke.net将WIN32 API调用转换为dot net。

将自动隐藏设置为任务栏

您可以使用描述here

的键操作注册表来实现这一点

这应该是一件容易的事,祝你好运。

答案 3 :(得分:1)

任务栏是一个appbar,你可以使用SHAppBarMessage

来控制它

答案 4 :(得分:1)

VB“显示”和“自动隐藏”任务栏-Windows 10

我已经在VB中对此进行了翻译,这可能对其他人很有用(Windows 10;应该在32位和64位上工作):

process_files()

答案 5 :(得分:0)

我从这段代码中创建了一个任务栏类:

public class Taskbar
{
[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
}
/// <summary>
/// Set the Taskbar State option
/// </summary>
/// <param name="option">AppBarState to activate</param>
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);
}
/// <summary>
/// Gets the current Taskbar state
/// </summary>
/// <returns>current Taskbar state</returns>
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);
}
}

问题在于,当我正在表演时

taskbar.SetTaskbarState(Taskbar.AppBarStates.AlwaysOnTop);

之后

taskbar.SetTaskbarState(Taskbar.AppBarStates.AutoHide);

我的开始按钮不再激活(我无法打开startmenu,点击它不会导致一切)。我正在使用Windows 10.有没有人知道解决方案?

答案 6 :(得分:0)

这是我使用的解决方案:7+ Taskbar Tweaker(http://rammichael.com/7-taskbar-tweaker)。感谢作者!

一个简单的免费程序,可以在一个操作中切换任务栏自动隐藏 - 将其指定为双击或中间单击任务栏的空白区域或自行设置(请参阅高级选项+帮助,右键单击尝试图标)。

这对我来说是必不可少的,因为我在垂直位置和笔记本电脑上使用任务栏,当我投影到XVGA时,这是常用的。这是一个真正的痛苦,如此多的点击切换它。

令人遗憾的是,自90年代以来,微软还没有进一步发展这一点。今天,我认为一个选择就是办公室计划中的功能。

干杯!

马里安

答案 7 :(得分:0)

对于像我这样从Google来到这里并使用Windows 10的所有用户,@ Quispie和@nicruo的回答都可以,但需要额外的if

其原因是类名因版本而异(显然,因为我不再有其他Windows,而是10,所以很显然。)

msgData.hWnd = FindWindow("System_TrayWnd", null);
if (msgData.hWnd == IntPtr.Zero)
    msgData.hWnd = FindWindow("Shell_TrayWnd", null);