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

前端之家收集整理的这篇文章主要介绍了c# – 如何以编程方式确定Windows任务栏是否被隐藏?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要知道 Windows任务栏是否被隐藏.我相信没有.NET方法可以做到这一点,我也遇到了很多“如何隐藏和显示任务栏”样本,但是我没有看到任何基于我正在寻找的内容.我不熟悉Windows API,所以我觉得很难理解传统的Windows代码.有人可以请我指引一篇文章或者输入代码,告诉任务栏的当前状态是否被隐藏?我在C#编码.

谢谢.

解决方法

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.

原文链接:https://www.f2er.com/csharp/96178.html

猜你在找的C#相关文章