Question

I've seen this great post regarding the supported resolution : How to list available video modes using C#?

The problem that is gives me the supported resolutions of the screen that I'm running my .exe on.

I have 2 screens, and want to pass the screens "UID" or some other parameter and get the resolution by it.

Is there any way to do it?

Thanks!!

Was it helpful?

Solution

You should enumerate all display devices with EnumDisplayDevices API and then use DeviceName as first parameter for EnumDisplaySettings API, here is the code to get display device names:

  var displayDeviceNames = new List<string>();
  int deviceIndex = 0;
  while (true)
  {
    var deviceData = new DisplayDevice();
    deviceData.cb = Marshal.SizeOf(typeof(DisplayDevice));
    if (EnumDisplayDevices(null, deviceIndex, ref deviceData, 0) != 0)
    {
      displayDeviceNames.Add(deviceData.DeviceName);
      deviceIndex++;
    }
    else
    {
      break;
    }
  }

Needed declarations:

[Flags()]
public enum DisplayDeviceStateFlags : int
{
  /// <summary>The device is part of the desktop.</summary>
  AttachedToDesktop = 0x1,
  MultiDriver = 0x2,
  /// <summary>The device is part of the desktop.</summary>
  PrimaryDevice = 0x4,
  /// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
  MirroringDriver = 0x8,
  /// <summary>The device is VGA compatible.</summary>
  VGACompatible = 0x16,
  /// <summary>The device is removable; it cannot be the primary display.</summary>
  Removable = 0x20,
  /// <summary>The device has more display modes than its output devices support.</summary>
  ModesPruned = 0x8000000,
  Remote = 0x4000000,
  Disconnect = 0x2000000
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DisplayDevice
{
  [MarshalAs(UnmanagedType.U4)]
  public int cb;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  public string DeviceName;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  public string DeviceString;
  [MarshalAs(UnmanagedType.U4)]
  public DisplayDeviceStateFlags StateFlags;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  public string DeviceID;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  public string DeviceKey;
}

[DllImport("User32.dll")]
static extern int EnumDisplayDevices(string lpDevice, int iDevNum, ref DisplayDevice lpDisplayDevice, int dwFlags);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top