/// <summary>Returns a monitor given its device path.</summary> /// <param name="devicePath">The monitor device path.</param> /// <returns>Returns the monitor associated with the specified <paramref name="devicePath"/>, or null.</returns> public static DisplayMonitor GetMonitorByDevicePath(string devicePath) { if (!isInitialized) { Update(); } var adapters = new DisplayAdapter[adaptersByDeviceName.Count]; adaptersByDeviceName.Values.CopyTo(adapters, 0); var aMax = adapters.Length; for (var a = 0; a < aMax; ++a) { var monitor = adapters[a].GetMonitorByDevicePath(devicePath); if (monitor != null) { return(monitor); } } return(null); }
/// <summary>Initializes a new <see cref="DisplayMonitor"/> instance.</summary> /// <param name="displayAdapter">The display adapter this monitor is attached to; must not be null.</param> /// <param name="displayDevice">A valid <see cref="DisplayDevice"/> structure.</param> /// <param name="monitorHandle">A handle (HMONITOR) to the monitor.</param> internal DisplayMonitor(DisplayAdapter displayAdapter, DisplayDevice displayDevice, IntPtr monitorHandle) : base(ref displayDevice) { adapter = displayAdapter ?? throw new ArgumentNullException("displayAdapter"); info = GetInfo(handle = monitorHandle); }
internal DisplayDeviceManagerEventArgs(DisplayAdapter adapter) : base() { this.adapter = adapter; }
/// <summary>Refreshes the device list and their state, and raises events.</summary> public static void Update() { configuration.Refresh(); var adaptersToRefresh = new List <DisplayDevice>(adaptersByDeviceName.Count); var removedAdapters = new List <string>(adaptersByDeviceName.Keys); var newAdapters = new List <DisplayAdapter>(); var primaryAdapterChanged = false; DisplayAdapter adapter; var displayDevices = DisplayAdapter.EnumDisplayDevices(null, false); var aMax = displayDevices.Count; for (var a = 0; a < aMax; ++a) { var displayDevice = displayDevices[a]; if (adaptersByDeviceName.TryGetValue(displayDevice.DeviceName, out adapter)) { adaptersToRefresh.Add(displayDevice); removedAdapters.Remove(adapter.DeviceName); } else { adapter = new DisplayAdapter(displayDevice); GetDisplayConfigInfo(adapter); adaptersByDeviceName.Add(adapter.DeviceName, adapter); newAdapters.Add(adapter); } if (adapter.State.HasFlag(DisplayAdapterStateIndicators.PrimaryDevice) && (primaryAdapterDeviceName != adapter.DeviceName)) { primaryAdapterChanged = (primaryAdapterDeviceName != null); primaryAdapterDeviceName = displayDevice.DeviceName; } } while (removedAdapters.Count > 0) { var s = removedAdapters[0]; removedAdapters.RemoveAt(0); adapter = adaptersByDeviceName[s]; adaptersByDeviceName.Remove(s); adapter.OnRemoved(); AdapterRemoved?.Invoke(null, new DisplayDeviceManagerEventArgs(adapter)); } while (newAdapters.Count > 0) { AdapterAdded?.Invoke(null, new DisplayDeviceManagerEventArgs(newAdapters[0])); newAdapters.RemoveAt(0); } while (adaptersToRefresh.Count > 0) { var device = adaptersToRefresh[0]; adaptersToRefresh.RemoveAt(0); if (adaptersByDeviceName.TryGetValue(device.DeviceName, out adapter)) { adapter.Refresh(ref device); GetDisplayConfigInfo(adapter); } } if (primaryAdapterChanged) { PrimaryAdapterChanged?.Invoke(null, EventArgs.Empty); } isInitialized = true; }
private static void GetDisplayConfigInfo(DisplayAdapter adapter) { var paths = configuration.Paths; var pMax = paths.Count; PathInfo path; PathSourceInfo source; PathTargetInfo target; TargetDeviceDescription desc; DisplayMonitor monitor; int index; for (var p = 0; p < pMax; ++p) { path = paths[p]; source = path.SourceInfo; if (source == PathSourceInfo.Empty) { continue; } if (adapter.DeviceName.Equals(DisplayConfiguration.GetSourceGDIDeviceName(source), StringComparison.OrdinalIgnoreCase)) { adapter.identifier = source.Identifier; if (path.SupportsVirtualMode) { adapter.cloneGroupId = source.CloneGroupId; index = source.ModeInfoIndex2; } else { adapter.cloneGroupId = -1; index = source.ModeInfoIndex; } //if( index == PathSourceInfo.InvalidModeInfoIndex ) // adapter.currentModeFormat = PixelFormat.Undefined; //else // adapter.currentModeFormat = configuration.DisplayModes[ index ].Format; target = path.TargetInfo; if (target == PathTargetInfo.Empty) { continue; } desc = DisplayConfiguration.GetTargetDeviceDescription(target); monitor = adapter.GetMonitorByDevicePath(desc.DevicePath); if (monitor != null) { monitor.identifier = target.Identifier; if (!string.IsNullOrWhiteSpace(desc.FriendlyName)) { monitor.DisplayName = desc.FriendlyName; } monitor.videoOutputTechnology = target.OutputTechnology; monitor.scaling = target.Scaling; monitor.refreshRate = target.RefreshRate; monitor.scanlineOrdering = target.ScanlineOrdering; index = path.SupportsVirtualMode ? target.ModeInfoIndex2 : target.ModeInfoIndex; if (index != PathTargetInfo.InvalidModeInfoIndex) { monitor.videoSignalInfo = configuration.DisplayModes[index].VideoSignalInformation; } else { monitor.videoSignalInfo = VideoSignalInfo.Empty; } monitor.Orientation = target.Rotation; monitor.connectorInstance = desc.ConnectorInstance; } } } }
//private static DisplayConfiguration registryConfiguration; /// <summary>Refreshes the device list and states, and raises events.</summary> public static void Refresh() { var removedAdapters = new List<string>( adaptersByDeviceName.Keys ); var addedAdapters = new List<string>(); var adaptersToRefresh = new List<DisplayDevice>( removedAdapters.Count ); var primaryAdapterChanged = false; DisplayAdapter adapter; var displayDevices = DisplayAdapter.EnumDisplayDevices( null, false ); for( var a = 0; a < displayDevices.Count; a++ ) { var displayDevice = displayDevices[ a ]; if( adaptersByDeviceName.TryGetValue( displayDevice.DeviceName, out adapter ) ) { adaptersToRefresh.Add( displayDevice ); removedAdapters.Remove( adapter.DeviceIdentifier ); } else { adapter = new DisplayAdapter( displayDevice ); adaptersByDeviceName.Add( adapter.DeviceIdentifier, adapter ); addedAdapters.Add( adapter.DeviceIdentifier ); } if( adapter.State.HasFlag( AdapterStateIndicators.PrimaryDevice ) && ( primaryAdapterDeviceName != adapter.DeviceIdentifier ) ) { primaryAdapterChanged = ( primaryAdapterDeviceName != null ); primaryAdapterDeviceName = displayDevice.DeviceName; } } if( DisplayConfiguration.IsSupported ) { if( currentConfiguration == null ) currentConfiguration = DisplayConfiguration.Query( DisplayConfigRequest ); else currentConfiguration.Refresh(); } if( removedAdapters.Count > 0 ) { var adapterRemovedEvent = AdapterRemoved; while( removedAdapters.Count > 0 ) { var s = removedAdapters[ 0 ]; removedAdapters.RemoveAt( 0 ); adapter = adaptersByDeviceName[ s ]; adaptersByDeviceName.Remove( s ); adapter.OnRemoved(); if( adapterRemovedEvent != null ) adapterRemovedEvent.Invoke( null, new DisplayDeviceEventArgs( adapter.DeviceIdentifier ) ); } } if( addedAdapters.Count > 0 ) { var adapterAddedEvent = AdapterAdded; while( addedAdapters.Count > 0 ) { if( adapterAddedEvent != null ) adapterAddedEvent.Invoke( null, new DisplayDeviceEventArgs( addedAdapters[ 0 ] ) ); addedAdapters.RemoveAt( 0 ); } } while( adaptersToRefresh.Count > 0 ) { var device = adaptersToRefresh[ 0 ]; if( adaptersByDeviceName.TryGetValue( device.DeviceName, out adapter ) ) adapter.Refresh( device ); adaptersToRefresh.RemoveAt( 0 ); } if( primaryAdapterChanged && PrimaryAdapterChanged != null ) PrimaryAdapterChanged.Invoke( null, EventArgs.Empty ); isInitialized = true; }
/// <summary>Returns a monitor given its device path.</summary> /// <param name="devicePath">The monitor device path.</param> /// <returns>Returns the monitor associated with the specified <paramref name="devicePath"/>, or null.</returns> public static DisplayMonitor GetMonitorByDevicePath( string devicePath ) { if( !isInitialized ) Refresh(); var adapters = new DisplayAdapter[ adaptersByDeviceName.Count ]; adaptersByDeviceName.Values.CopyTo( adapters, 0 ); for( var a = 0; a < adapters.Length; a++ ) { var monitor = adapters[ a ].Monitors.GetMonitorByDevicePath( devicePath ); if( monitor != null ) return monitor; } return null; }