Ejemplo n.º 1
0
        /// <summary>
        /// Changes the display settings
        /// </summary>
        /// <param name="width" />
        /// <param name="height" />
        public bool ChangeDisplaySettings(int width, int height)
        {
            _oldDevmode        = new Devmode();
            _oldDevmode.dmSize = (ushort)Marshal.SizeOf(_oldDevmode);

            // Retrieving current settings
            // to edit them
            EnumDisplaySettings(null,
                                EnumCurrentSettings,
                                ref _oldDevmode);

            // Making a copy of the current settings
            // to allow reseting to the original mode
            var newMode = _oldDevmode;

            // Changing the settings
            newMode.dmPelsWidth  = (uint)width;
            newMode.dmPelsHeight = (uint)height;

            // Capturing the operation result, 1 = update registry
            var result =
                ChangeDisplaySettings(ref newMode, 1);

            switch (result)
            {
            case DispChangeSuccessful:
                return(true);

            case DispChangeBadmode:
                MessageBox.Show("Mode not supported.");
                return(false);

            case DispChangeRestart:
                MessageBox.Show("Restart required.");
                return(false);

            default:
                MessageBox.Show("Failed. Error code = " + result);
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks if the given display mode is supported
        /// </summary>
        /// <param name="width" />
        /// <param name="height" />
        /// <param name="supportedModes" />
        /// <returns>
        public bool IsDisplayModeSupported(int width, int height, out string supportedModes)
        {
            var mode = new Devmode();

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

            var modeIndex = 0; // 0 = The first mode

            supportedModes = string.Empty;
            var previousSupportedMode = string.Empty;

            while (EnumDisplaySettings(null,
                                       modeIndex,
                                       ref mode)) // Mode found
            {
                if (mode.dmPelsWidth == (uint)width && mode.dmPelsHeight == (uint)height)
                {
                    return(true);
                }

                var newSupportedMode = mode.dmPelsWidth + "x" + mode.dmPelsHeight;
                if (newSupportedMode != previousSupportedMode)
                {
                    if (supportedModes == string.Empty)
                    {
                        supportedModes += newSupportedMode;
                    }
                    else
                    {
                        supportedModes += ", " + newSupportedMode;
                    }

                    previousSupportedMode = newSupportedMode;
                }

                modeIndex++; // The next mode
            }

            return(false);
        }
Ejemplo n.º 3
0
        public bool ChangeToMaxResolution(string displayName)
        {
            Devmode CreateDevMove()
            {
                var _mode = new Devmode();

                _mode.dmSize = (ushort)Marshal.SizeOf(_mode);
                return(_mode);
            }

            Console.WriteLine("Trying with monitor " + displayName);

            var     modeLst   = new List <Devmode>();
            var     modeIndex = 0; // 0 = The first mode
            Devmode mode      = CreateDevMove();

            while (EnumDisplaySettings(displayName.ToLPTStr(), modeIndex, ref mode))
            {
                modeLst.Add(mode);
                //Console.WriteLine(String.Format("{2} {0}x{1}", mode.dmPelsWidth, mode.dmPelsHeight, mode.dmDeviceName));
                mode = CreateDevMove();
                modeIndex++;
            }

            for (int i = modeLst.Count - 1; i >= 0; i--)
            {
                mode = modeLst[i];
                Console.WriteLine(String.Format("{2} {0}x{1}", mode.dmPelsWidth, mode.dmPelsHeight, mode.dmDeviceName));
                var ret = ChangeDisplaySettingsEx(displayName, ref mode, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
                switch (ret)
                {
                case DISP_CHANGE.Successful:
                    Console.WriteLine("Successful");
                    break;

                case DISP_CHANGE.Restart:
                    Console.WriteLine("Restart");
                    break;

                case DISP_CHANGE.Failed:
                    Console.WriteLine("Failed");
                    break;

                case DISP_CHANGE.BadMode:
                    Console.WriteLine("BadMode");
                    break;

                case DISP_CHANGE.NotUpdated:
                    break;

                case DISP_CHANGE.BadFlags:
                    break;

                case DISP_CHANGE.BadParam:
                    break;

                case DISP_CHANGE.BadDualView:
                    break;

                default:
                    break;
                }
                if (ret == DISP_CHANGE.Successful)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
 static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, ref Devmode lpDevMode, IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam);
Ejemplo n.º 5
0
 private static extern int ChangeDisplaySettings(
     [In, Out] ref Devmode lpDevMode,
     [param: MarshalAs(UnmanagedType.U4)] uint dwflags);
Ejemplo n.º 6
0
 private static extern Boolean EnumDisplaySettings(
     byte[] lpszDeviceName,
     [param: MarshalAs(UnmanagedType.U4)] int iModeNum,
     [In, Out] ref Devmode lpDevMode);