如何获得特定显示器的支持解决方案?

时间:2014-07-29 12:58:17

标签: c# monitor screen-resolution multiple-monitors uid

我看过这篇关于支持的解决方案的精彩帖子: How to list available video modes using C#?

这个问题为我提供了支持我正在运行.exe的屏幕分辨率。

我有2个屏幕,想要通过屏幕“UID”或其他一些参数并通过它获得分辨率。

有什么办法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您应该使用EnumDisplayDevices API枚举所有显示设备,然后使用DeviceName作为EnumDisplaySettings API的第一个参数,以下是获取显示设备名称的代码:

  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;
    }
  }

需要的声明:

[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);