[.net] Changing the screen resolution

Started by
1 comment, last by cutovoi 17 years, 11 months ago
Hi fellows I've created a class that(tries) changes to screen resolution. In my class I define that I want to change to screen to 1024x768 My problem is when the screen is changed the screen changes to 800x600 and the DisplayFrequency is altered to 4(the most low). I would like that someone helps me. Below my code

public class ChangeResolution
    {
        private int m_iWidth;
        private int m_iHeight;
        //Parâmetros ChangeDisplaySettings
        private const int CDS_UPDATEREGISTRY = 1;
        private const int CDS_TEST           = 2;
        //Retornos de ChangeDisplaySettings
        public const int DISP_CHANGE_SUCCESSFUL = 0;
        public const int DISP_CHANGE_RESTART    = 1;
        public const int DISP_CHANGE_FAILED     = -1;
        public const int DISP_CHANGE_BADMODE    = -2;
        public const int DISP_CHANGE_NOTUPDATED = -3;
        public const int DISP_CHANGE_BADFLAGS   = -4;
        public const int DISP_CHANGE_BADPARAM   = -5;
        public ChangeResolution(int iWidth, int iHeight)
        {
            m_iWidth = iWidth;
            m_iHeight = iHeight;
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int EnumDisplaySettings(string sDeviceName, int iModeNum, ref DEVMODE devMode);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int ChangeDisplaySettings(ref DEVMODE devMode, int iFlags);
        public void AlterResolution()
        {
            DEVMODE devStruct = new DEVMODE();
            devStruct.dmDeviceName = new string(new char[32]);
            devStruct.dmFormName = new string(new char[32]);
            devStruct.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            devStruct.dmPelsWidth = m_iWidth;
            devStruct.dmPelsHeight = m_iHeight;
            devStruct.dmBitsPerPel = 24;
            devStruct.dmDisplayFrequency = 75;
            devStruct.dmFields = (devStruct.dmPelsWidth | devStruct.dmPelsHeight);
            int i = 0;
            StringBuilder sb = new StringBuilder();
            while (true)
            {
                
                if (EnumDisplaySettings(null, i, ref devStruct) != 0)
                {
                    sb.Append(i + "Width: " + devStruct.dmPelsWidth + " Height: " + devStruct.dmPelsHeight);
                    i+=1;
                }
                else
                {
                    sb.Remove(0, sb.Length - 1);
                    sb.Append(i + "Width: " + devStruct.dmPelsWidth + " Height: " + devStruct.dmPelsHeight);
                    //MessageBox.Show(sb.ToString(), "Display2", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                }
            }
            if (EnumDisplaySettings(null, i, ref devStruct) == DISP_CHANGE_FAILED)
            {
                MessageBox.Show("Não foi possivel obter infos do display", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (EnumDisplaySettings(null, i, ref devStruct) == 0)
            {
                int iVal = ChangeDisplaySettings(ref devStruct, CDS_TEST);
                switch(iVal)
                {
                    case DISP_CHANGE_SUCCESSFUL:
                    {
                       MessageBox.Show("Alterações feitas", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       ChangeDisplaySettings(ref devStruct, CDS_UPDATEREGISTRY);
                       break;
                    }
                    case DISP_CHANGE_RESTART:
                    {
                       MessageBox.Show("Reinicie sua máquina para que as novas configurações tenham efeito", "Infomação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       break;
                    }
                    case DISP_CHANGE_FAILED:
                    {
                       MessageBox.Show("Falha ao tentar alterar a tela!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       break;
                    }
                    default:
                    {
                       MessageBox.Show("Nenhuma condição atendida", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       break;
                    }
                }               
            }
        }
    }

Advertisement
What exactly is the problem?

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

I'll explain better.
When the function is called, the screen resolution is altered, but the screen resolution is modified to 800x600 instead of 1024x768 that resolution what I want. And the color quality is changed to 4 bits, instead of 32 bits.
This thing occurs but I don't know why.

This topic is closed to new replies.

Advertisement