using Microsoft.Win32;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
/// Wrapper around the Winapi POINT type.
[StructLayout(LayoutKind.Sequential)]
public struct POINT {
/// <summary>
/// The X Coordinate.
/// </summary>
public int X;
/// <summary>
/// The Y Coordinate.
/// </summary>
public int Y;
/// <summary>
/// Creates a new POINT.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
/// Wrapper around the Winapi SIZE type.
[StructLayout(LayoutKind.Sequential)]
public struct SIZE {
public int WIDTH;
public int HEIGHT;
public SIZE(int w, int h)
{
this.WIDTH = w;
this.HEIGHT = h;
}
}
/// Wrapper around the Winapi RECT type.
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct RECT
{
/// <summary>
/// LEFT
/// </summary>
public int Left;
/// <summary>
/// TOP
/// </summary>
public int Top;
/// <summary>
/// RIGHT
/// </summary>
public int Right;
/// <summary>
/// BOTTOM
/// </summary>
public int Bottom;
/// <summary>
/// Creates a new RECT.
/// </summary>
public RECT(int left_, int top_, int right_, int bottom_)
{
Left = left_;
Top = top_;
Right = right_;
Bottom = bottom_;
}
/// <summary>
/// HEIGHT
/// </summary>
public int Height { get { return Bottom - Top; } }
/// <summary>
/// WIDTH
/// </summary>
public int Width { get { return Right - Left; } }
/// <summary>
/// SIZE
/// </summary>
public SIZE Size { get { return new SIZE(Width, Height); } }
/// <summary>
/// LOCATION
/// </summary>
public POINT Location { get { return new POINT(Left, Top); } }
}
private const int SPI_GETWORKAREA = 0x30;
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(int uAction, int uParam, ref RECT lpvParam, int fuWinIni);
public static Rect GetDeskWorkRect()
{
RECT r = new RECT(0,0,0,0);
try
{
if (SystemParametersInfo(SPI_GETWORKAREA, 0, ref r, 0))
{
return new Rect(r.Left,r.Top,r.Width,r.Height);
}
else
{
Debug.LogError("SystemParametersInfo return error");
}
}
catch (Exception e)
{
Debug.LogError(e);
}
return r;
}