PersistentWindows/Ninjacrab.PersistentWindows.Solution/Ninjacrab.PersistentWindows.WpfShell/Models/DesktopDisplayMetrics.cs
2014-10-28 21:42:20 -04:00

76 lines
2.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Ninjacrab.PersistentWindows.WpfShell.WinApiBridge;
namespace Ninjacrab.PersistentWindows.WpfShell.Models
{
public class DesktopDisplayMetrics
{
public static DesktopDisplayMetrics AcquireMetrics()
{
DesktopDisplayMetrics metrics = new DesktopDisplayMetrics();
var displays = Display.GetDisplays();
int displayId = 0;
foreach (var display in displays)
{
metrics.SetMonitor(displayId++, display);
}
return metrics;
}
private Dictionary<int, Display> monitorResolutions = new Dictionary<int, Display>();
public int NumberOfDisplays { get { return monitorResolutions.Count; } }
public void SetMonitor(int id, Display display)
{
if (!monitorResolutions.ContainsKey(id) ||
monitorResolutions[id].ScreenWidth != display.ScreenWidth ||
monitorResolutions[id].ScreenHeight != display.ScreenHeight)
{
monitorResolutions.Add(id, display);
BuildKey();
}
}
private void BuildKey()
{
List<string> keySegments = new List<string>();
foreach(var entry in monitorResolutions.OrderBy(row => row.Key))
{
keySegments.Add(string.Format("[{0}-{1}x{2}-{3}x{4}]", entry.Key, entry.Value.Left, entry.Value.Top, entry.Value.ScreenWidth, entry.Value.ScreenHeight));
}
key = string.Join(",", keySegments);
}
private string key;
public string Key
{
get
{
return key;
}
}
public override bool Equals(object obj)
{
var other = obj as DesktopDisplayMetrics;
if (other == null)
{
return false;
}
return this.Key == other.key;
}
public override int GetHashCode()
{
return key.GetHashCode();
}
public int GetHashCode(DesktopDisplayMetrics obj)
{
return obj.key.GetHashCode();
}
}
}