using System.Collections.Generic; using System.Linq; namespace WindowsDisplayAPI { /// /// Represents a Windows UnAttached Display Device /// public class UnAttachedDisplay : DisplayDevice { /// /// Creates a new UnAttachedDisplay /// /// The DisplayDevice instance to copy information from protected UnAttachedDisplay(DisplayDevice device) : base( device.DevicePath, device.DeviceName, device.DeviceKey, device.Adapter, device.ScreenName, device.DisplayName, device.IsAvailable, false ) { } /// public override bool IsAvailable { get => base.IsAvailable || !IsValid; } /// public override bool IsValid { get { return DisplayAdapter.GetDisplayAdapters() .SelectMany(adapter => adapter.GetDisplayDevices(base.IsAvailable)) .Any( device => device.DevicePath.Equals(DevicePath) && device.DeviceKey.Equals(DeviceKey) ); } } /// /// Returns a list of all unattached displays on this machine /// /// An enumerable list of UnAttachedDisplay public static IEnumerable GetUnAttachedDisplays() { return DisplayAdapter.GetDisplayAdapters() .SelectMany(adapter => adapter.GetDisplayDevices(false)) .Select(device => new UnAttachedDisplay(device)); } /// public override string ToString() { return IsValid ? $"{GetType().Name}: {DisplayName} ({DeviceName})" : $"{GetType().Name}: Invalid"; } /// /// Returns the corresponding Display device for this unattached display. Only functions when this instance is invalidated /// due to display attachment. /// /// public Display ToDisplay() { return IsValid ? null : Display.GetDisplays() .FirstOrDefault( display => display.DevicePath.Equals(DevicePath) && display.DeviceKey.Equals(DeviceKey) ); } } }