简体   繁体   English

如何以编程方式确定Windows任务栏是否隐藏?

[英]How can I determine programmatically whether the Windows taskbar is hidden or not?

I need to know whether the Windows taskbar is hidden or not. 我需要知道Windows任务栏是否隐藏。 I believe there is no .NET method to do this, and also I have come across a lot of "how to hide and show the taskbar" samples, but I haven't seen anything based on what I am looking for. 我相信没有.NET方法可以做到这一点,而且我也遇到了很多“如何隐藏和显示任务栏”的样本,但我没有看到任何基于我正在寻找的东西。 I am not familiar with the Windows API, so I find it hard to understand traditional Windows code. 我不熟悉Windows API,因此我发现很难理解传统的Windows代码。 Can someone please direct me to an article or type code telling whether the current state of the taskbar is hidden or not? 有人可以指导我一篇文章或输入代码,告诉我是否隐藏了任务栏的当前状态? I am coding in C#. 我在C#编码。

Thanks. 谢谢。

winSharp93 presents a helper class (" Find out Size (and position) of the taskbar ") that seems to work. winSharp93提供了一个帮助类(“ 查找任务栏的大小(和位置) ”)似乎有效。 It uses Win32's SHAppBarMessage function . 它使用Win32的SHAppBarMessage函数

Here's the code (with minor additions) from his blog: 这是他博客上的代码(有少量补充):

using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace TaskbarTest
{
    public enum TaskbarPosition
    {
        Unknown = -1,
        Left,
        Top,
        Right,
        Bottom,
    }

    public sealed class Taskbar
    {
        private const string ClassName = "Shell_TrayWnd";

        public Rectangle Bounds {
            get;
            private set;
        }
        public TaskbarPosition Position {
            get;
            private set;
        }
        public Point Location {
            get {
                return this.Bounds.Location;
            }
        }
        public Size Size {
            get {
                return this.Bounds.Size;
            }
        }
        //Always returns false under Windows 7
        public bool AlwaysOnTop {
            get;
            private set;
        }
        public bool AutoHide {
            get;
            private set;
        }

        public Taskbar() {
            IntPtr taskbarHandle = User32.FindWindow(Taskbar.ClassName, null);

            APPBARDATA data = new APPBARDATA();
            data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
            data.hWnd = taskbarHandle;
            IntPtr result = Shell32.SHAppBarMessage(ABM.GetTaskbarPos, ref data);
            if (result == IntPtr.Zero)
                throw new InvalidOperationException();

            this.Position = (TaskbarPosition)data.uEdge;
            this.Bounds = Rectangle.FromLTRB(data.rc.left, data.rc.top, data.rc.right, data.rc.bottom);

            data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
            result = Shell32.SHAppBarMessage(ABM.GetState, ref data);
            int state = result.ToInt32();
            this.AlwaysOnTop = (state & ABS.AlwaysOnTop) == ABS.AlwaysOnTop;
            this.AutoHide = (state & ABS.Autohide) == ABS.Autohide;
        }
    }

    public enum ABM : uint
    {
        New = 0x00000000,
        Remove = 0x00000001,
        QueryPos = 0x00000002,
        SetPos = 0x00000003,
        GetState = 0x00000004,
        GetTaskbarPos = 0x00000005,
        Activate = 0x00000006,
        GetAutoHideBar = 0x00000007,
        SetAutoHideBar = 0x00000008,
        WindowPosChanged = 0x00000009,
        SetState = 0x0000000A,
    }

    public enum ABE : uint
    {
        Left = 0,
        Top = 1,
        Right = 2,
        Bottom = 3
    }

    public static class ABS
    {
        public const int Autohide = 0x0000001;
        public const int AlwaysOnTop = 0x0000002;
    }

    public static class Shell32
    {
        [DllImport("shell32.dll", SetLastError = true)]
        public static extern IntPtr SHAppBarMessage(ABM dwMessage, [In] ref APPBARDATA pData);
    }

    public static class User32
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct APPBARDATA
    {
        public uint cbSize;
        public IntPtr hWnd;
        public uint uCallbackMessage;
        public ABE uEdge;
        public RECT rc;
        public int lParam;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }
}

The author claims it works on his Windows 7 machine and it appears to work on my XP Pro machine. 作者声称它适用于他的Windows 7机器,它似乎可以在我的XP Pro机器上运行。

Here's how you might use it: 以下是您可以使用它的方法:

    Taskbar tb = new Taskbar();
    Console.WriteLine("w:{0}, h:{1} - hide:{2}", tb.Size.Width, tb.Size.Height, tb.AutoHide);

Where: tb.Size.Width and tb.Size.Height returns the width and height of the Taskbar, and tb.AutoHide returns true if the Taskbar is hidden and false if it is not. 其中:tb.Size.Width和tb.Size.Height返回任务栏的宽度和高度,如果任务栏被隐藏,则tb.AutoHide返回true,否则返回false。

All solutions I found didn't work for me, so I had follwing idea and it works great for me. 我发现的所有解决方案对我都不起作用,所以我有很好的想法,对我来说很有用。

 public static bool IsTaskbarVisible()
 {
      return Math.Abs(SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Height) > 0;
 }

SystemParameters.PrimaryScreenHeight returns the real display height. SystemParameters.PrimaryScreenHeight返回实际显示高度。 SystemParameters.WorkArea.Height returns the available workarea height. SystemParameters.WorkArea.Height返回可用的工作区高度。

If they are different, the taskbar is showing. 如果它们不同,则显示任务栏。

SystemParametersInfo with SPI_GETWORKAREA 带有SPI_GETWORKAREA的SystemParametersInfo

Retrieves the size of the work area on the primary display monitor. 检索主显示监视器上工作区的大小。 The work area is the portion of the screen not obscured by the system taskbar or by application desktop toolbars. 工作区是屏幕的一部分,不会被系统任务栏或应用程序桌面工具栏遮挡。 The pvParam parameter must point to a RECT structure that receives the coordinates of the work area, expressed in virtual screen coordinates. pvParam参数必须指向接收工作区坐标的RECT结构,以虚拟屏幕坐标表示。

To get the work area of a monitor other than the primary display monitor, call the GetMonitorInfo function. 要获取主显示监视器以外的监视器的工作区,请调用GetMonitorInfo函数。

You can use IsWindowVisible Win32 function. 您可以使用IsWindowVisible Win32函数。

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool IsWindowVisible(IntPtr hWnd);

    IntPtr hWnd = FindWindow("Shell_TrayWnd", null);
    if (hWnd != null) IsTaskBarVisible = IsWindowVisible(hWnd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在任务栏中隐藏Windows 7徽标? - How can I hide the Windows 7 logo in the taskbar? 如何以编程方式确定MP3文件是CBR还是VBR? (最好使用c#) - How can I programmatically determine whether an MP3 file is CBR or VBR? (preferrably using c#) 如何以编程方式确定是在多核,超线程还是在多处理器上? - How can I determine programmatically whether on multi-core, hyperthreading or multi-processor? 我如何以编程方式确定是否使用C#将新工作项添加到TFS中 - How can I programmatically determine whether new work item added to TFS using C# 如何以编程方式移动Windows任务栏(取两个) - How to programmatically move windows taskbar (take two) 如何确定我是否在Windows Phone 8.1(通用应用程序)中连接到WiFi或移动网络? - How can I determine whether I am connected to WiFi or a mobile network in Windows Phone 8.1 (Universal app)? 如何确定我的安装程序是否在 Windows 10 教育版上运行? - How can I determine whether my installer is running on the Education edition of Windows 10? 如何在Windows 7上删除Java程序的标题栏和任务栏图标? - How can I remove titlebar and taskbar icons of Java programs on Windows 7? 如何在Windows 7中找到任务栏项的顺序? - How can i find the order of the taskbar items in Windows 7? 在任务栏中显示隐藏窗口的图标 - Show icon in taskbar for hidden windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM