public void UpdateDevices() { List <DisplayAdapter> oldAdapters = Adapters.ToList(); List <DisplayMonitor> oldMonitors = Monitors.ToList(); NativeMethods.DISPLAY_DEVICE dev = new NativeMethods.DISPLAY_DEVICE(true); uint i = 0; while (NativeMethods.EnumDisplayDevices(null, i++, ref dev, 0)) { var adapter = oldAdapters.FirstOrDefault(a => a.DeviceName == dev.DeviceName); if (adapter != null) { oldAdapters.Remove(adapter); adapter.Init(dev, oldMonitors); } else { adapter = new DisplayAdapter(); adapter.Init(dev, oldMonitors); Adapters.Add(adapter); } } var w = new Stopwatch(); w.Start(); NativeMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref NativeMethods.RECT lprcMonitor, IntPtr dwData) { var mi = new NativeMethods.MONITORINFOEX(true); var success = NativeMethods.GetMonitorInfo(hMonitor, ref mi); if (!success) { return(true); } IList monitors = AttachedMonitors.Where(d => d.Adapter.DeviceName == mi.DeviceName).ToList(); foreach (DisplayMonitor monitor in monitors) { monitor.Init(hMonitor, mi); monitor.Timing = w.ElapsedMilliseconds; w.Restart(); } return(true); }, IntPtr.Zero); DevicesUpdated?.Invoke(this, new EventArgs()); foreach (var monitor in Monitors) { Debug.Print(monitor.DeviceName + "\t" + monitor.Edid.Model + "\t" + monitor.Timing); } }
public void Init(DisplayAdapter adapter, NativeMethods.DISPLAY_DEVICE dev) { using (this.Suspend()) { Adapter = adapter; DeviceId = dev.DeviceID; DeviceKey = dev.DeviceKey; DeviceName = dev.DeviceName; DeviceString = dev.DeviceString; State = dev.StateFlags; } }