【问题标题】:How can I determine programmatically whether the Windows taskbar is hidden or not?如何以编程方式确定 Windows 任务栏是否隐藏?
【发布时间】:2011-01-03 04:45:59
【问题描述】:

我需要知道 Windows 任务栏是否隐藏。我相信没有 .NET 方法可以做到这一点,而且我遇到了很多“如何隐藏和显示任务栏”示例,但我没有看到任何基于我正在寻找的东西。我不熟悉 Windows API,因此很难理解传统的 Windows 代码。有人可以指导我看一篇文章或输入代码,说明任务栏的当前状态是否隐藏?我正在用 C# 编码。

谢谢。

【问题讨论】:

    标签: c# .net winapi visibility taskbar


    【解决方案1】:

    winSharp93 提供了一个辅助类(“Find out Size (and position) of the taskbar”),它似乎可以工作。它使用 Win32 的SHAppBarMessage function

    这是他博客中的代码(有少量补充):

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

    作者声称它可以在他的 Windows 7 机器上运行,它似乎也可以在我的 XP Pro 机器上运行。

    以下是您可以使用的方法:

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

    其中:tb.Size.Width 和 tb.Size.Height 返回任务栏的宽度和高度,tb.AutoHide 如果任务栏被隐藏则返回 true,否则返回 false。

    【讨论】:

      【解决方案2】:

      带有 SPI_GETWORKAREA 的 SystemParametersInfo

      检索主显示器上工作区的大小。工作区是未被系统任务栏或应用程序桌面工具栏遮挡的屏幕部分。 pvParam 参数必须指向一个接收工作区坐标的 RECT 结构,以虚拟屏幕坐标表示。

      要获取主显示器以外的其他显示器的工作区,请调用 GetMonitorInfo 函数。

      【讨论】:

        【解决方案3】:

        你可以使用IsWindowVisibleWin32函数。

            [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);
        

        【讨论】:

          【解决方案4】:

          我发现的所有解决方案都不适用于我,所以我有以下想法,它对我很有用。

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

          SystemParameters.PrimaryScreenHeight 返回实际显示高度。 SystemParameters.WorkArea.Height 返回可用的工作区高度。

          如果它们不同,则显示任务栏。

          【讨论】:

            猜你喜欢
            • 2011-05-06
            • 1970-01-01
            • 2011-01-05
            • 1970-01-01
            • 1970-01-01
            • 2012-02-21
            • 1970-01-01
            • 1970-01-01
            • 2014-09-16
            相关资源
            最近更新 更多