///// <summary></summary> ///// <param name="identifier"></param> ///// <returns></returns> //public static DisplayAdapter GetAdapter( DisplayDeviceId identifier ) //{ // foreach( var adapter in adaptersByDeviceName.Values ) // { // if( adapter.Identifier.Equals( identifier ) ) // return adapter; // } // return null; //} #endregion Display adapters #region Display monitors ///// <summary></summary> ///// <param name="identifier"></param> ///// <returns></returns> //public static DisplayMonitor GetMonitor( DisplayDeviceId identifier ) //{ // foreach( var adapter in adaptersByDeviceName.Values ) // { // foreach( var monitor in adapter.Monitors ) // { // if( monitor.identifier.Equals( identifier ) ) // return monitor; // } // } // return null; //} /// <summary>Returns a monitor given its handle (HMONITOR).</summary> /// <param name="monitorHandle">The monitor handle.</param> /// <returns>Returns the monitor associated with the specified handle, or null.</returns> public static DisplayMonitor GetMonitorByHandle(IntPtr monitorHandle) { if (!isInitialized) { Update(); } var monitorInfo = DisplayMonitor.GetInfo(monitorHandle); if (adaptersByDeviceName.TryGetValue(monitorInfo.AdapterDeviceName, out DisplayAdapter adapter)) { return(adapter.GetMonitorByHandle(monitorHandle)); } return(null); }
/// <summary>Raises the <see cref="MonitorConnected"/> or <see cref="MonitorDisconnected"/> event.</summary> /// <param name="monitor">The device name of the display monitor of interest.</param> /// <param name="connected">True to raise the <see cref="MonitorConnected"/> event, false to raise the <see cref="MonitorDisconnected"/> event.</param> private void OnMonitorConnectedOrDisconnected(DisplayMonitor monitor, bool connected) { (connected ? this.MonitorConnected : this.MonitorDisconnected)?.Invoke(this, new DisplayAdapterEventArgs(monitor)); }
private void RefreshMonitorList() { var deviceName = base.DeviceName; var allHandles = GetMonitorHandles(); var handles = new List <IntPtr>(); var hMax = allHandles.Count; int h; for (h = 0; h < hMax; ++h) { var info = DisplayMonitor.GetInfo(allHandles[h]); if (deviceName.Equals(info.AdapterDeviceName, StringComparison.Ordinal)) { handles.Add(allHandles[h]); } } var removedMonitors = new List <string>(monitorsByDeviceName.Keys); var addedMonitors = new List <DisplayMonitor>(); var primaryMonitorChanged = false; var monitors = EnumDisplayDevices(deviceName, true); DisplayMonitor displayMonitor; h = 0; for (var m = 0; m < monitors.Count; ++m) { var monitor = monitors[m]; var handle = IntPtr.Zero; if ((monitor.State & (int)DisplayMonitorStateIndicators.Active) != 0 && (h < handles.Count)) { handle = handles[h]; // FIXME - this is not the right handle when topology is Clone ! ++h; } if (monitorsByDeviceName.TryGetValue(monitor.DeviceName, out displayMonitor)) { displayMonitor.Refresh(ref monitor); displayMonitor.Handle = handle; removedMonitors.Remove(monitor.DeviceName); } else { displayMonitor = new DisplayMonitor(this, monitor, handle); monitorsByDeviceName.Add(monitor.DeviceName, displayMonitor); addedMonitors.Add(displayMonitor); } if (displayMonitor.IsPrimary && (primaryDisplayMonitor != displayMonitor)) { primaryMonitorChanged = (primaryDisplayMonitor != null); primaryDisplayMonitor = displayMonitor; } } while (addedMonitors.Count > 0) { this.OnMonitorConnectedOrDisconnected(addedMonitors[0], true); addedMonitors.RemoveAt(0); } while (removedMonitors.Count > 0) { deviceName = removedMonitors[0]; removedMonitors.RemoveAt(0); displayMonitor = monitorsByDeviceName[deviceName]; monitorsByDeviceName.Remove(deviceName); displayMonitor.OnDisconnected(); this.OnMonitorConnectedOrDisconnected(displayMonitor, false); } if (primaryMonitorChanged) { PrimaryMonitorChanged?.Invoke(this, EventArgs.Empty); } }
/// <summary>Returns the <see cref="DisplayMonitor"/> which has the largest area of intersection with the bounding rectangle of a window.</summary> /// <param name="windowHandle">A window handle.</param> /// <returns>Returns the <see cref="DisplayMonitor"/> which has the largest area of intersection with the bounding rectangle of a window.</returns> public static DisplayMonitor GetMonitorFromWindowHandle(IntPtr windowHandle) { return(GetMonitorByHandle(DisplayMonitor.GetMonitorHandleFromWindow(windowHandle))); }
private void RefreshMonitors() { var deviceName = base.DeviceIdentifier; var allHandles = GetMonitorHandles(); var handles = new List<IntPtr>(); int h; for( h = 0; h < allHandles.Count; ++h ) { var info = DisplayMonitor.GetMonitorInfo( allHandles[ h ] ); if( deviceName.Equals( info.AdapterDeviceName, StringComparison.Ordinal ) ) handles.Add( allHandles[ h ] ); } var removedMonitors = new List<string>( monitorsByDeviceName.Keys ); var addedMonitors = new List<string>(); var primaryMonitorChanged = false; DisplayMonitor displayMonitor; var monitors = EnumDisplayDevices( deviceName, true ); h = 0; for( var m = 0; m < monitors.Count; ++m ) { var monitor = monitors[ m ]; IntPtr handle; if( ( monitor.State & (int)MonitorStateIndicators.Active ) == (int)MonitorStateIndicators.Active && ( h < handles.Count ) ) { handle = handles[ h ]; // FIXME - this is not the right handle when topology is Clone ! ++h; } else handle = IntPtr.Zero; if( monitorsByDeviceName.TryGetValue( monitor.DeviceName, out displayMonitor ) ) { displayMonitor.Refresh( monitor ); displayMonitor.Handle = handle; removedMonitors.Remove( monitor.DeviceName ); } else { displayMonitor = new DisplayMonitor( monitor, handle ); monitorsByDeviceName.Add( monitor.DeviceName, displayMonitor ); addedMonitors.Add( monitor.DeviceName ); } if( displayMonitor.IsPrimary && ( primaryDisplayMonitor != displayMonitor ) ) { primaryMonitorChanged = ( primaryDisplayMonitor != null ); primaryDisplayMonitor = displayMonitor; } } while( addedMonitors.Count > 0 ) { this.OnMonitorConnectedOrDisconnected( addedMonitors[ 0 ], true ); addedMonitors.RemoveAt( 0 ); } while( removedMonitors.Count > 0 ) { deviceName = removedMonitors[ 0 ]; removedMonitors.RemoveAt( 0 ); displayMonitor = monitorsByDeviceName[ deviceName ]; monitorsByDeviceName.Remove( deviceName ); displayMonitor.OnDisconnected(); this.OnMonitorConnectedOrDisconnected( deviceName, false ); } if( primaryMonitorChanged ) { var primaryMonitorChangedEvent = PrimaryMonitorChanged; if( primaryMonitorChangedEvent != null ) primaryMonitorChangedEvent.Invoke( this, EventArgs.Empty ); } }
internal DisplayAdapterEventArgs(DisplayMonitor monitor) { this.monitor = monitor; }