可用的分辨率对于特定的屏幕

时间:2015-05-12 09:04:00

标签: c# screen resolution

我可能有2个显示器/屏幕连接到我的机器。 我想知道特定屏幕的所有Avaliable分辨率(我有一个System.Windows.Forms.Screen类型的实例)。

我见过以下内容: How to list available video modes using C#?

List of valid resolutions for a given Screen?

但它们都为所有监视器提供结果,而不仅仅是特定的监视器。有什么建议?感谢!!!

编辑1: enter image description here

这是关于我的2个屏幕的信息: enter image description here

1 个答案:

答案 0 :(得分:2)

在第一个链接中,您被告知EnumDisplaySettings。花几秒钟到lookup that function FIRST PARAMETER 就是

  

字符串,指定显示设备的图形模式   功能将获得信息。

这是一个获取有关显示信息的示例类。我故意省略了DEVMODE,因为你已经得到了它。

public class NativeMethods
{
    [DllImport("user32.dll")]
    public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
    [DllImport("user32.dll")]
    public static extern bool EnumDisplayDevices(string deviceName, int modeNum, ref DISPLAY_DEVICE displayDevice, int flags);
}


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

static class Display
{
    public static List<DISPLAY_DEVICE> GetGraphicsAdapters()
    {
        int i = 0;
        DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
        List<DISPLAY_DEVICE> result = new List<DISPLAY_DEVICE>();
        displayDevice.cb = Marshal.SizeOf(displayDevice);
        while (NativeMethods.EnumDisplayDevices(null, i, ref displayDevice, 1))
        {
            result.Add(displayDevice);
            i++;
        }

        return result;
    }

    public static List<DISPLAY_DEVICE> GetMonitors(string graphicsAdapter)
    {

        DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
        List<DISPLAY_DEVICE> result = new List<DISPLAY_DEVICE>();
        int i = 0;
        displayDevice.cb = Marshal.SizeOf(displayDevice);
        while (NativeMethods.EnumDisplayDevices(graphicsAdapter, i, ref displayDevice, 0))
        {
            result.Add(displayDevice);
            i++;
        }

        return result;
    }

    public static List<DEVMODE> GetDeviceModes(string graphicsAdapter)
    {
        int i = 0;
        DEVMODE devMode = new DEVMODE();
        List<DEVMODE> result = new List<DEVMODE>();
        while (NativeMethods.EnumDisplaySettings(graphicsAdapter, i, ref devMode))
        {
            result.Add(devMode);
            i++;
        }
        return result;
    }
}