SystemTrayMenu/Helpers/WindowsTaskbar.cs

75 lines
2.1 KiB
C#
Raw Normal View History

2019-07-05 05:04:14 +12:00
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using static SystemTrayMenu.DllImports.NativeMethods;
2019-07-05 05:04:14 +12:00
//Microsoft.WindowsAPICodePack.Taskbar.TaskbarManager do not have the bounds implemented?
namespace SystemTrayMenu.Helper
2019-07-05 05:04:14 +12:00
{
public enum TaskbarPosition
{
Unknown = -1,
Left,
Top,
Right,
Bottom,
}
public sealed class WindowsTaskbar
2019-07-05 05:04:14 +12:00
{
private const string ClassName = "Shell_TrayWnd";
public Rectangle Bounds
{
get;
private set;
}
public TaskbarPosition Position
{
get;
private set;
}
public Point Location => Bounds.Location;
public Size Size => Bounds.Size;
2019-07-05 05:04:14 +12:00
//Always returns false under Windows 7
public bool AlwaysOnTop
{
get;
private set;
}
public bool AutoHide
{
get;
private set;
}
2019-11-19 08:11:55 +13:00
public WindowsTaskbar()
2019-07-05 05:04:14 +12:00
{
IntPtr taskbarHandle = User32FindWindow(ClassName, null);
2019-07-05 05:04:14 +12:00
APPBARDATA data = new APPBARDATA
{
cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA)),
hWnd = taskbarHandle
};
IntPtr result = Shell32SHAppBarMessage(ABM.GetTaskbarPos, ref data);
2019-07-05 05:04:14 +12:00
if (result == IntPtr.Zero)
{
//throw new InvalidOperationException();
Bounds = new Rectangle(20, 20, 20, 20);
}
else
{
Position = (TaskbarPosition)data.uEdge;
Bounds = Rectangle.FromLTRB(data.rc.left, data.rc.top, data.rc.right, data.rc.bottom);
data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
result = Shell32SHAppBarMessage(ABM.GetState, ref data);
2019-07-05 05:04:14 +12:00
int state = result.ToInt32();
AlwaysOnTop = (state & ABS.AlwaysOnTop) == ABS.AlwaysOnTop;
AutoHide = (state & ABS.Autohide) == ABS.Autohide;
2019-07-05 05:04:14 +12:00
}
}
}
}