BackgroundBuilder/Services/TaskbarService.cs

54 lines
1.7 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace BackgroundBuilder.Services
{
///
/// Implementation of using P/Invoke (SHAppBarMessage).
///
public class TaskbarService : ITaskbarService
{
#region WinAPI
[DllImport("shell32.dll")]
private static extern IntPtr SHAppBarMessage(uint msg, ref APPBARDATA data);
[StructLayout(LayoutKind.Sequential)]
private struct APPBARDATA
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public uint uEdge;
public RECT rc;
public IntPtr lParam;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT { public int left, top, right, bottom; }
private const uint ABM_GETTASKBARPOS = 5;
#endregion
public int GetTaskbarSize()
{
var data = new APPBARDATA { cbSize = (uint)Marshal.SizeOf<APPBARDATA>() };
if (SHAppBarMessage(ABM_GETTASKBARPOS, ref data) == IntPtr.Zero)
throw new InvalidOperationException("Failed to retrieve taskbar position.");
int thickness = IsHorizontal()
? Math.Abs(data.rc.bottom - data.rc.top)
: Math.Abs(data.rc.right - data.rc.left);
return thickness;
}
public bool IsHorizontal()
{
var data = new APPBARDATA { cbSize = (uint)Marshal.SizeOf<APPBARDATA>() };
SHAppBarMessage(ABM_GETTASKBARPOS, ref data);
int height = Math.Abs(data.rc.bottom - data.rc.top);
int width = Math.Abs(data.rc.right - data.rc.left);
return width > height;
}
}
}