Beispiel #1
0
        public List <SystemDisplayModel> GetAll()
        {
            var modeIndex = 0;
            var mode      = new NativeMethods.Display.DEVMODE();
            var displays  = new List <SystemDisplayModel>();

            mode.dmSize = (ushort)Marshal.SizeOf(mode);

            while (NativeMethods.Display.EnumDisplaySettings(null, modeIndex, ref mode))
            {
                var display = new SystemDisplayModel((int)mode.dmPelsWidth, (int)mode.dmPelsHeight, (int)mode.dmBitsPerPel);
                if (!displays.Any(__ => __.Width == display.Width && __.Height == display.Height && __.BitCount == display.BitCount))
                {
                    displays.Add(display);
                }

                modeIndex++;
            }

            if (!displays.Any())
            {
                throw new InvalidOperationException("EnumDisplaySettings()");
            }

            return(displays);
        }
Beispiel #2
0
        public SystemDisplayModel GetCurrent()
        {
            var mode = new NativeMethods.Display.DEVMODE();

            mode.dmSize = (ushort)Marshal.SizeOf(mode);

            if (NativeMethods.Display.EnumDisplaySettings(null, NativeMethods.Display.ENUM_CURRENT_SETTINGS, ref mode))
            {
                return(new SystemDisplayModel((int)mode.dmPelsWidth, (int)mode.dmPelsHeight, (int)mode.dmBitsPerPel));
            }

            throw new InvalidOperationException("EnumDisplaySettings()");
        }
Beispiel #3
0
        public void SetCurrent(SystemDisplayModel display)
        {
            var originalMode = new NativeMethods.Display.DEVMODE();

            originalMode.dmSize = (ushort)Marshal.SizeOf(originalMode);

            NativeMethods.Display.EnumDisplaySettings(null, NativeMethods.Display.ENUM_CURRENT_SETTINGS, ref originalMode);

            var newMode = originalMode;

            newMode.dmPelsWidth  = (uint)display.Width;
            newMode.dmPelsHeight = (uint)display.Height;
            newMode.dmBitsPerPel = (uint)display.BitCount;

            var result = NativeMethods.Display.ChangeDisplaySettings(ref newMode, 0);

            if (result == NativeMethods.Display.DISP_CHANGE_SUCCESSFUL)
            {
                return;
            }

            throw new InvalidOperationException("ChangeDisplaySettings()");
        }