[英]How can I determine programmatically whether the Windows taskbar is hidden or not?
我需要知道Windows任務欄是否隱藏。 我相信沒有.NET方法可以做到這一點,而且我也遇到了很多“如何隱藏和顯示任務欄”的樣本,但我沒有看到任何基於我正在尋找的東西。 我不熟悉Windows API,因此我發現很難理解傳統的Windows代碼。 有人可以指導我一篇文章或輸入代碼,告訴我是否隱藏了任務欄的當前狀態? 我在C#編碼。
謝謝。
winSharp93提供了一個幫助類(“ 查找任務欄的大小(和位置) ”)似乎有效。 它使用Win32的SHAppBarMessage函數 。
這是他博客上的代碼(有少量補充):
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。
我發現的所有解決方案對我都不起作用,所以我有很好的想法,對我來說很有用。
public static bool IsTaskbarVisible()
{
return Math.Abs(SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Height) > 0;
}
SystemParameters.PrimaryScreenHeight返回實際顯示高度。 SystemParameters.WorkArea.Height返回可用的工作區高度。
如果它們不同,則顯示任務欄。
帶有SPI_GETWORKAREA的SystemParametersInfo
檢索主顯示監視器上工作區的大小。 工作區是屏幕的一部分,不會被系統任務欄或應用程序桌面工具欄遮擋。 pvParam參數必須指向接收工作區坐標的RECT結構,以虛擬屏幕坐標表示。
要獲取主顯示監視器以外的監視器的工作區,請調用GetMonitorInfo函數。
您可以使用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.