麻烦在C#中获得可用的屏幕分辨率

时间:2017-07-27 13:29:11

标签: c#

我需要获取系统上特定显示器的屏幕分辨率。 我找到了一个很好的解决方案,它的工作原理很好: How to list available video modes using C#?

但是,当我在“Main”中用“null”替换显示名称时,我什么也得不回。

    [DllImport("user32.dll")]
    public static extern bool EnumDisplaySettings(
          string deviceName, int modeNum, ref DEVMODE devMode);
    const int ENUM_CURRENT_SETTINGS = -1;

    const int ENUM_REGISTRY_SETTINGS = -2;

    [StructLayout(LayoutKind.Sequential)]
    public struct DEVMODE
    {

        private const int CCHDEVICENAME = 0x20;
        private const int CCHFORMNAME = 0x20;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
        public string dmDeviceName;
        public short dmSpecVersion;
        public short dmDriverVersion;
        public short dmSize;
        public short dmDriverExtra;
        public int dmFields;
        public int dmPositionX;
        public int dmPositionY;
        public ScreenOrientation dmDisplayOrientation;
        public int dmDisplayFixedOutput;
        public short dmColor;
        public short dmDuplex;
        public short dmYResolution;
        public short dmTTOption;
        public short dmCollate;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
        public string dmFormName;
        public short dmLogPixels;
        public int dmBitsPerPel;
        public int dmPelsWidth;
        public int dmPelsHeight;
        public int dmDisplayFlags;
        public int dmDisplayFrequency;
        public int dmICMMethod;
        public int dmICMIntent;
        public int dmMediaType;
        public int dmDitherType;
        public int dmReserved1;
        public int dmReserved2;
        public int dmPanningWidth;
        public int dmPanningHeight;

    }


static void Main(string[] args)
    {
    DISPLAY_DEVICE device = new DISPLAY_DEVICE();
        device.Size = (ushort)Marshal.SizeOf(device);
        DEVMODE vDevMode = new DEVMODE();

        int i = 0;
        while (EnumDisplaySettings(@"\\.\DISPLAY1", i, ref vDevMode))
        {
            Console.WriteLine("Width:{0} Height:{1} Color:{2} Frequency:{3}",
                                    vDevMode.dmPelsWidth,
                                    vDevMode.dmPelsHeight,
                                    1 << vDevMode.dmBitsPerPel, vDevMode.dmDisplayFrequency
                                );
            i++;
        }
    }

1 个答案:

答案 0 :(得分:0)

问题在于系统配置。

我正在使用屏幕关闭的笔记本电脑和两个外接显示器。 当我打开屏幕时,它开始工作。 我认为这也是一个硬件问题,因为在我的家庭(桌面)系统上它按预期工作。