public override IEnumerable<Display> GetActiveDisplays() { var displayDevices = GetDisplayDevices(); // find out resolution parameters for each display device. foreach (var displayDevice in displayDevices.Where( x => x.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop))) { var mode = new DevMode {size = (short) Marshal.SizeOf(typeof (DevMode))}; // -1 means current RESOLUTION for specific display. // you can enumerate through 0..N to find supported resolutions. var success = XPWrapper.EnumDisplaySettings(displayDevice.DeviceName, -1, ref mode); if (!success) continue; var origin = mode.position; var resolution = mode.resolution; var refreshRate = mode.displayFrequency; var rotation = mode.displayOrientation; yield return new Display { Resolution = resolution, Origin = origin, Rotation = rotation.ToScreenRotation(), RefreshRate = refreshRate, Name = displayDevice.DeviceName }; } }
/// <summary> /// Reads the device information using XP native functions /// and fills our specific structure. /// </summary> /// <param name="deviceName">Name of the device to get screen information from.</param> /// <returns>Fills it with screen resolution</returns> public void ReadDeviceInformation(string deviceName) { var vDevMode = new DevMode(); int rt = 0;// EnumDisplaySettings(deviceName, -1, ref vDevMode); if(rt != 0) { /* // This is not necessiarly true // TODO: In future, replace that. // Primary is not always DISPLAY1 AFAIK scr.Primary = deviceName == "\\\\.\\DISPLAY1"; scr.BitsPerPixel = 1 << vDevMode.dmBitsPerPel; scr.Bounds = new Rectangle(vDevMode.dmPositionX, vDevMode.dmPositionY, vDevMode.dmPelsWidth, vDevMode.dmPelsHeight); scr.WorkingArea = new Rectangle(vDevMode.dmPositionX, vDevMode.dmPositionY, vDevMode.dmPelsWidth, vDevMode.dmPelsHeight); scr.DeviceName = vDevMode.dmDeviceName;*/ } }
public static extern bool EnumDisplaySettingsEx(string lpszDeviceName, int iModeNum, ref DevMode lpDevMode, uint dwFlags);
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DevMode devMode);
private static extern DispChange ChangeDisplaySettingsEx(string deviceName, ref DevMode lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);
public static extern int ChangeDisplaySettings(ref DevMode devMode, int flags);