Change Screen Resolution Desktop Gadget
Program to Switch Screen Setting In One Click
Introduction
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.
Background
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!
Form1.cs
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()
{
InitializeComponent();
timer1.Interval = 100;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
changeDisplaySetting();
}
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;
}
else
{
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
NativeMethods.cs
//
//
//--------------------------------------------------------------------------
//
// 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
{
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
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 ofNativeMethods.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.
https://docs.google.com/open?id=0BzPU_tIN4sXoNjc0M2VkOTMtODhjNi00ZmFjLTg0YzctNzFhOTUwNGFkNThh[^] Example Screen shot below is the low to high resolution switch.https://docs.google.com/open?id=0BzPU_tIN4sXoYTc4ZDAwOGItOGM1YS00NjkyLTgxYjYtZTg2Zjg2OWQ4YjE0[^]
References
Dynamic Screen Resolution By sreejith ss nair
Changing Screen Orientation Programmatically by Stefan Wick
http://msdn.microsoft.com/en-us/library/ms812499.aspx#tbconchgscrn_usingsample[^]
History
- 1/7/2012 Posted
- 1/8/2012 Fix typo and reduced size of the pictures