Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:38:44+00:00 2026-05-31T12:38:44+00:00

So what I am trying to do is set a screen resolution on the

  • 0

So what I am trying to do is set a screen resolution on the primary display in C# using ChangeDisplaySettings. I have tested this across multiple windows 7 computers that I own, and the result is always the same: any* valid resolution (those that are listed in the “screen resolution” menu upon right-clicking the desktop) will work just fine, except for the largest resolution that can be selected in that menu, which will cause User32.ChangeDisplaySettings to return -2, which is DISP_CHANGE_BADMODE, meaning that the requested display mode was invalid.

*On some of the computers with larger primary displays, I didn’t bother to test every resolution, and just picked some arbitrary smaller ones & the maximum, as there were too many to test on every single one. I feel confident enough in my testing to say that the maximum resolution ALWAYS fails, while smaller resolutions usually/always succeed (I never had any of them fail during my tests anyways).

Documentation on ChangeDisplaySettings: http://msdn.microsoft.com/en-us/library/dd183411%28VS.85%29.aspx

Documentation on the DEVMODE struct it uses: http://msdn.microsoft.com/en-us/library/dd183565%28v=vs.85%29.aspx

So for an example, lets say I’m running on a 1920×1080 display.

I set my resolution manually (or programmatically) to something else, doesn’t matter what it is or how it was changed, and then run the following code:

DEVMODE dm = new DEVMODE();
dm.dmDeviceName = new String(new char[32]);
dm.dmFormName = new String(new char[32]);
dm.dmSize = (short)Marshal.SizeOf(dm);
if (User32.EnumDisplaySettings(null, User32.ENUM_CURRENT_SETTINGS, ref dm) != 0)
{
     dm.dmPelsWidth = 1920;
     dm.dmPelsHeight = 1080;
     Console.WriteLine("" + User32.ChangeDisplaySettings(ref dm, User32.CDS_UPDATEREGISTRY) + "\n");
}

*Note that this is not actually the code from the program. I just made this simplified version to cut it down to the bare necessities to illustrate the point.

The program would print out:

-2

Which, as mentioned previously, is the value of DISP_CHANGE_BADMODE, and the resolution would fail to change.

Now, if I changed the values of 1080 and 1920 to 900 and 1600 respectively, another supported resolution on this monitor, and then set the resolution to something other than 1600×900, and then ran this program, it would in fact change the resolution to 1600×900, and return DISP_CHANGE_SUCCESSFUL.

Note that using other flags, such as CDS_RESET (0x40000000 or 1073741824) in place of CDS_UPDATEREGISTRY also result in the same error.

This is a tutorial I found that helped me get started:

http://www.codeproject.com/Articles/6810/Dynamic-Screen-Resolution

[I removed the hyperlink because of the apparent spam prevention system. Sort of silly given that the first to were msdn.microsoft links, and this is a codeproject one, but, w/e]

Note that in the comments section, it seems there is someone who used the provided source files directly, and is experiencing a similar problem. To quote them:

hello ,
i’m using Resolution.cs on my c# application
and it doesn’t work with high resolutions like ” 1366*768 ” & ” 1280*720 ”
can any one help ???

However, despite how commonly ChangeDisplaySettings seems to be recommended by tutorials, I can’t find any information on resolving this issue (which could very well be operating system specific, but I currently lack any non-Windows 7 computers to test on, and even if I did, it would not solve the issue that it doesn’t work on Windows 7 computers)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:38:45+00:00Added an answer on May 31, 2026 at 12:38 pm

    As it turns out, the tutorial I was using was making the assumption that nothing would change any of the display mode parameters to something invalid during the time that it was at a lower resolution (Such as increasing the refresh rate, which is capped on the monitor’s side. And since I was using the same two monitors for the testing, and the max resolution had a lower max refresh rate than any of the other resolutions, I would encounter this issue.)

    A safer method than what is illustrated in the tutorial is to work with the indexes of the display modes or work only with EnumDisplayModes and never touch the data inside the DEVMODE struct.

    The following is an excerpt of an example program I whipped up that changes the resolution to the specified parameters, and then back again.

                int selB, selG, selF, selH, selW;
                ... //these values get defined, etc.
                DEVMODE OSpecs = new DEVMODE();
                getCurrentRes(ref OSpecs);
                int Ondx = getDMbySpecs(OSpecs.dmPelsHeight, OSpecs.dmPelsWidth, OSpecs.dmDisplayFrequency, OSpecs.dmDisplayFlags, OSpecs.dmBitsPerPel, ref OSpecs);
                Screen Srn = Screen.PrimaryScreen;
                Console.WriteLine("Current res is " + OSpecs.dmPelsHeight + " by " + OSpecs.dmPelsWidth + "\n");
                DEVMODE NSpecs = new DEVMODE();
                int Nndx = getDMbySpecs(selH, selW, selF, selG, selB, ref NSpecs);
                    //Note that this function sets both the DEVMODE to the selected display mode and returns the index value of this display mode. It returns -1 if it fails (-1 is the value of ENUM_CURRENT_SETTINGS), and sets the DEVMODE to the current display mode.
                if (Nndx == -1)
                {
                     Console.WriteLine("Could not find specified mode");
                }
                else if (setDisplayMode(ref NSpecs) || setDisplayMode(Nndx)) //This is just to illustrate both ways of doing it. One or the other may be more convenient (ie, the latter if you are getting this from a file, the former if you already have the DEVMODE in your program, etc.)
                {
                    //reset display mode to original after waiting long enough to see it changed
                    Console.WriteLine("Successful change. Waiting 4 seconds.");
                    Thread.Sleep(4000);
                    if (setDisplayMode(ref OSpecs) || setDisplayMode(Ondx))
                    {
                        //success!
                        Console.WriteLine("Mode reversion succeeded.");
                    }
                    else
                    {
                        Console.WriteLine("Mode reversion failed. Manual reset required.");
                    }
                }
                else
                {
                    //return
                    Console.WriteLine("Resolution change failed. Aborting");
                }
    

    where the functions used here are as follows:

        static bool setDisplayMode(int i)
        {
            DEVMODE DM = new DEVMODE();
            DM.dmSize = (short)Marshal.SizeOf(DM);
            User32.EnumDisplaySettings(null, i, ref DM);
            if (User32.ChangeDisplaySettings(ref DM, User32.CDS_TEST) == 0 && User32.ChangeDisplaySettings(ref DM, User32.CDS_UPDATEREGISTRY) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        static bool setDisplayMode(ref DEVMODE DM)
        {
            if (User32.ChangeDisplaySettings(ref DM, User32.CDS_TEST) == 0 && User32.ChangeDisplaySettings(ref DM, User32.CDS_UPDATEREGISTRY) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        static int getDMbySpecs(int H, int W, int F, int G, int B, ref DEVMODE DM)
        {
            DM.dmSize = (short)Marshal.SizeOf(DM);
            DEVMODE SelDM = new DEVMODE();
            SelDM.dmSize = (short)Marshal.SizeOf(SelDM);
            int iOMI = 0;
            for (iOMI = 0; User32.EnumDisplaySettings(null, iOMI, ref SelDM) != 0; iOMI++)
            {
                if (( B == -1 || B == SelDM.dmBitsPerPel) && ( H == -1 || H == SelDM.dmPelsHeight) && ( W == -1 || W == SelDM.dmPelsWidth) && ( G == -1 || G == SelDM.dmDisplayFlags) && ( F == -1 || F == SelDM.dmDisplayFrequency))
    
                    break;
            }
            if (User32.EnumDisplaySettings(null, iOMI, ref DM) == 0)
            {
                iOMI = -1;
                getCurrentRes(ref DM);
            }
            return iOMI;
        }
        static void getCurrentRes(ref DEVMODE dm)
        {
            dm = new DEVMODE();
            dm.dmSize = (short)Marshal.SizeOf(dm);
            User32.EnumDisplaySettings(null, User32.ENUM_CURRENT_SETTINGS, ref dm);
            return;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set activity screen orientation using a styles: Here is the styles.xml:
I am trying to set the screen brightness from a widget. We know this
I'm trying to set up a display screen for the floor info which is
Trying to set up caching on our datasets - we're using clr stored procedures
Scripts, registries, screen-savers, oh my! I'm trying to use a screen-saver on a Windows
I am trying to set a different wallpaper for every home screen, but I
I'm trying to set up a shell script that will start a screen session
I am trying to set my machine up so I can follow this tutorial:
I'm trying to work with an emulator with a particular screen resolution, but when
I'm currently trying to set up a web page for a touch screen computer,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.