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; } }
public static void GetDisplayDevices() { NativeMethods.DISPLAY_DEVICE dd = new NativeMethods.DISPLAY_DEVICE(); dd.cb = Marshal.SizeOf(dd); uint devNum = 0; while (NativeMethods.EnumDisplayDevices(null, devNum, ref dd, 0)) { Console.WriteLine("Found Display Device {0}: {1}; {2}; {3}; {4}; {5};", devNum, dd.DeviceName, dd.DeviceString, dd.StateFlags, dd.DeviceID, dd.DeviceKey); devNum++; dd.cb = Marshal.SizeOf(dd); } }
public void Init(NativeMethods.DISPLAY_DEVICE dev, IList <DisplayMonitor> oldMonitors) { DeviceId = dev.DeviceID; DeviceKey = dev.DeviceKey; DeviceName = dev.DeviceName; DeviceString = dev.DeviceString; State = dev.StateFlags; //if ((dev.StateFlags & NativeMethods.DisplayDeviceStateFlags.AttachedToDesktop) != NativeMethods.DisplayDeviceStateFlags.AttachedToDesktop) return; uint i = 0; var mon = new NativeMethods.DISPLAY_DEVICE(true); var w = new Stopwatch(); w.Start(); while (NativeMethods.EnumDisplayDevices(DeviceName, i++, ref mon, 0)) { var monitor = MonitorsService.D.Monitors.FirstOrDefault(m => m.DeviceName == mon.DeviceName); if (monitor != null) { oldMonitors.Remove(monitor); monitor.Init(this, mon); } else { monitor = new DisplayMonitor(); monitor.Init(this, mon); MonitorsService.D.Monitors.Add(monitor); } monitor.Timing = w.ElapsedMilliseconds; w.Restart(); } }
public static string GetMonitorProfile() { // c++ recommendation: http://stackoverflow.com/questions/13533754/code-example-for-wcsgetdefaultcolorprofile var displayDevice = new NativeMethods.DISPLAY_DEVICE(); displayDevice.cb = Marshal.SizeOf(displayDevice); // First, find the primary adaptor string adaptorName = null; UInt32 deviceIndex = 0; while (NativeMethods.EnumDisplayDevices(null, deviceIndex++, ref displayDevice, NativeMethods.EDD_GET_DEVICE_INTERFACE_NAME) != 0) { if ((displayDevice.StateFlags & NativeMethods.DisplayDeviceStateFlags.AttachedToDesktop) != 0 && (displayDevice.StateFlags & NativeMethods.DisplayDeviceStateFlags.PrimaryDevice) != 0) { adaptorName = displayDevice.DeviceName; break; } } // Second, find the first active (and attached) monitor string deviceName = null; deviceIndex = 0; while (NativeMethods.EnumDisplayDevices(adaptorName, deviceIndex++, ref displayDevice, NativeMethods.EDD_GET_DEVICE_INTERFACE_NAME) != 0) { if ((displayDevice.StateFlags & NativeMethods.DisplayDeviceStateFlags.Active) != 0 && (displayDevice.StateFlags & NativeMethods.DisplayDeviceStateFlags.Attached) != 0) { deviceName = displayDevice.DeviceKey; break; } } // Third, find out whether to use the global or user profile UInt32 usePerUserProfiles = 0; UInt32 res = NativeMethods.WcsGetUsePerUserProfiles(deviceName, NativeMethods.DeviceClassFlags.CLASS_MONITOR, out usePerUserProfiles); if (res == 0) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } // Finally, get the profile name NativeMethods.WCS_PROFILE_MANAGEMENT_SCOPE scope = (usePerUserProfiles != 0) ? NativeMethods.WCS_PROFILE_MANAGEMENT_SCOPE.WCS_PROFILE_MANAGEMENT_SCOPE_CURRENT_USER : NativeMethods.WCS_PROFILE_MANAGEMENT_SCOPE.WCS_PROFILE_MANAGEMENT_SCOPE_SYSTEM_WIDE; UInt32 cbProfileName = 0; // in bytes res = NativeMethods.WcsGetDefaultColorProfileSize(scope, deviceName, NativeMethods.COLORPROFILETYPE.CPT_ICC, NativeMethods.COLORPROFILESUBTYPE.CPST_RGB_WORKING_SPACE, 0, out cbProfileName); if (res == 0) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } int nLengthProfileName = (int)cbProfileName / 2; // WcsGetDefaultColor... is using LPWSTR, i.e. 2 bytes/char StringBuilder profileName = new StringBuilder(nLengthProfileName); res = NativeMethods.WcsGetDefaultColorProfile(scope, deviceName, NativeMethods.COLORPROFILETYPE.CPT_ICC, NativeMethods.COLORPROFILESUBTYPE.CPST_RGB_WORKING_SPACE, 0, cbProfileName, profileName); if (res == 0) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } return(profileName.ToString()); }