Click here to Skip to main content
15,445,560 members
Articles / Programming Languages / C# 4.0
Posted 10 Jan 2012

Tagged as


6 bookmarked

Change Screen Resolution Desktop Gadget

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
13 Jan 2012CPOL3 min read
Program to Switch Screen Setting In One Click[^]


This is DisplaySettingsSample application that switches screen resolution between High and Low. There are only two settings in this demo, but it is fairly simple to modify to include more functions. For the demo, I will just limit it to Low and High. It is from 1280 x 720 to 1920 x 1080. I have read Dynamic Screen Resolution By sreejith and downloaded the code, but I was not able to get it to work on 1920 x 1080 or higher resolution. Changing Screen Orientation Programmatically by Stefan worked very well but it was a long article. I thought a simpler demo might help other programmers who seek the simple solution.


I change my screen resolution frequently; when I am browsing news, I like the font to be bigger but when I am programming, I like font to be smaller, so I can see more on the screen. When I work on Visual Studio or Eclipse, I want to divide screen to several areas to compare code and to debug. In this case, I like to set screen resolution to the highest. So, I frequently right click on the desktop, go into Screen Resolution to change setting, then answer the Yes prompt that prompted if I want to save the changes. Why not just one click to adjust the screen resolution to the liking instead of going through the standard utility to adjust?

Using the code

The code is simple and straight forward. It is compiled under Microsoft Visual C# 2010 Express version. It is a Standard C# Window Form Application. I only added a ListBox control named listBox1 and a Timer control.

Most of the code was generated automatically from Visual Studio, the code I added are in changeDisplaySetting(), and do not forget the reference using Microsoft.Win32; at the top.

To make it work, add NativeMethods.cs to the project. Compile and run that’s all!


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Win32;  // need to add this reference

namespace ChangeDisplaySettings
    public partial class Form1 : Form
        public Form1()
            timer1.Interval = 100;
            timer1.Enabled = true;

        private void timer1_Tick(object sender, EventArgs e)

        private void changeDisplaySetting()
            DEVMODE dm = NativeMethods.CreateDevmode();
        int iRet = 1;

        // get current settings
        iRet = NativeMethods.EnumDisplaySettings(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm);
            listBox1.Items.Add("current display setting is " + dm.dmDeviceName + ", " + dm.dmPelsWidth.ToString() + ", " + dm.dmPelsHeight.ToString());
            if (dm.dmPelsWidth == 1920) //make the switch
                dm.dmPelsWidth = 1280;   // 1280 x 720
                dm.dmPelsHeight = 720;
                dm.dmPelsWidth = 1920;   // 1920 x 1080
                dm.dmPelsHeight =1080;

            iRet = NativeMethods.ChangeDisplaySettings(ref dm, 0);
            listBox1.Items.Add("ChangeDisplaySettings return code iRet = " + iRet.ToString());
            listBox1.Items.Add("display setting changed to " + dm.dmDeviceName + ", " + dm.dmPelsWidth.ToString() + ", " + dm.dmPelsHeight.ToString());
            // System.Environment.Exit(0); //uncomment this line to exit program automatically.


The following Listing is downloaded from Microsoft


//  Copyright (c) Microsoft Corporation.  All rights reserved. 
//  File: NativeMethods.cs 
//            C# file for the DisplaySettingsSample application
using System;
using System.Runtime.InteropServices;

//namespace DisplaySettingsSample
namespace ChangeDisplaySettings
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
    public struct DEVMODE 
        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 int    dmDisplayOrientation;
        public int    dmDisplayFixedOutput;
        public short  dmColor;
        public short  dmDuplex;
        public short  dmYResolution;
        public short  dmTTOption;
        public short  dmCollate;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string dmFormName;

        public short  dmLogPixels;
        public short  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;

    public class NativeMethods
        // PInvoke declaration for EnumDisplaySettings Win32 API
        [DllImport("user32.dll", CharSet=CharSet.Ansi)]
        public static extern int EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);         

        // PInvoke declaration for ChangeDisplaySettings Win32 API
        [DllImport("user32.dll", CharSet=CharSet.Ansi)]
        public static extern int ChangeDisplaySettings(ref DEVMODE lpDevMode, int dwFlags);

        // helper for creating an initialized DEVMODE structure
        public static DEVMODE CreateDevmode()
            DEVMODE dm = new DEVMODE();
            dm.dmDeviceName = new String(new char[32]);
            dm.dmFormName = new String(new char[32]);
            dm.dmSize = (short)Marshal.SizeOf(dm);
            return dm;

        // constants
        public const int ENUM_CURRENT_SETTINGS = -1;
        public const int DISP_CHANGE_SUCCESSFUL = 0;
        public const int DISP_CHANGE_BADDUALVIEW = -6;
        public const int DISP_CHANGE_BADFLAGS = -4;
        public const int DISP_CHANGE_BADMODE = -2;
        public const int DISP_CHANGE_BADPARAM = -5;
        public const int DISP_CHANGE_FAILED = -1;
        public const int DISP_CHANGE_NOTUPDATED = -3;
        public const int DISP_CHANGE_RESTART = 1;
        public const int DMDO_DEFAULT = 0;
        public const int DMDO_90 = 1;
        public const int DMDO_180 = 2;
        public const int DMDO_270 = 3;

Points of Interest

Microsoft has made the function call simple by providing sample of NativeMethods.cs.

I use a ListBox to display information instead of a MessageBox, because MessageBox can be annoying when user had to click OK to put away.

A Timer controls the flow of the program. It sets to 0.1 second delay timer1.Interval = 100;, then calls the changeDisplaySetting() and exits the application by System.Environment.Exit(0);.

If program was added to Taskbar, it became a Gadget; We can switch Screen Resolution from low to high and from high to low in one click.

Example screen shot below is the high to low resolution switch. You can view more coding line after switch to high resolution.[^]
Example Screen shot below is the low to high resolution switch.[^]


Dynamic Screen Resolution By sreejith ss nair

Dynamic Screen Resolution[^]

Changing Screen Orientation Programmatically by Stefan Wick[^]


  • 1/7/2012 Posted
  • 1/8/2012 Fix typo and reduced size of the pictures


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
A program analyst specialize in XML/HTML/CSS, Office Automation and Oracle Database.

A hobbyist of computer science. My favorite languages are C# and C++, but I also like to code in Ruby, Perl, Java, Python, Basic and SQL script.

Comments and Discussions

GeneralMy vote of 5 Pin
Pascal Damman20-Apr-12 5:36
MemberPascal Damman20-Apr-12 5:36 
Nice! It works!
Every time I notice that if you want to do something else as putting a button on a form, .NET doesn't support it and you have to use unmanaged code. Anyway, thanks to articles like these you still can use .NET to a certain level ....

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.