Click here to Skip to main content
15,887,931 members
Articles / Programming Languages / C#
Article

Dynamic Screen Resolution

Rate me:
Please Sign up or sign in to vote.
4.45/5 (57 votes)
22 Jul 2008CPOL3 min read 516K   17.3K   101   87
Setting the Screen Resolution in C#

Introduction

I had a hard time in setting a dynamic screen resolution when I was doing some thick client application for a test suite. I presume that most of us have had this happen to us, or have faced such times somewhere along our engineering life cycle. Because, as we all know, the resolution of the user screen may not be same as that of the development environment screen.

This article expects to deliver what I found as a solution for the discussed challenge.

The forthcoming part of this literature will communicate about:

  • How to get the end user screen resolution
  • How to change the user screen resolution to product compatible
  • How to safeguard the user screen resolution

How to Get the End User Screen Resolution

Accessing the user screen is being eased by the availing Screen class which is shipped along with the .NET framework. And can access the current user screen resolution through a static Screen.PrimaryScreen property available in the Screen class.

C#
public static Screen PrimaryScreen {get;}

The aforementioned property is read only and returns a Screen type. And the below given logic demonstrates how to make use of the Screen type to access the user screen resolution.

C#
Screen screen = Screen.PrimaryScreen;
int S_width=screen.Bounds.Width;
int S_height=screen.Bounds.Height;

How to Change the User Screen Resolution to Product Compatible

Before heading towards our next goal, let me talk about the unmanaged part of this implementation. Unlike traditional languages, the .NET framework holds a distinct step while leveraging both managed and unmanaged code. Personally, when I wrote this article, I never found any managed code which does this resolution treatment. And this is what made me think to explore some Win32 API’s.

Before continuing, I would request you to have hands on or theoretical knowledge about: COM Interop service, Attribute, DLL import attribute and Platform Invoke.

Since the scope of this article is limited to managed code, I will not be discussing anything about unmanaged code. But, despite that fact, we can use the DllImport attribute to read the definition of unmanaged code to your managed environment. In this case, we will be using the User32.dll API, which facilitates the dynamic resolution and has two functions related to changing the screen resolution.

  • EnumDisplaySettings
  • ChangeDisplaySettings
C#
class User32
{
        [DllImport("user32.dll")]
        public static extern int EnumDisplaySettings (
          string deviceName, int modeNum, ref DEVMODE devMode );         
        [DllImport("user32.dll")]
        public static extern int ChangeDisplaySettings(
              ref DEVMODE devMode, int flags); 
 
        public const int ENUM_CURRENT_SETTINGS = -1;
        public const int CDS_UPDATEREGISTRY = 0x01;
        public const int CDS_TEST = 0x02;
        public const int DISP_CHANGE_SUCCESSFUL = 0;
        public const int DISP_CHANGE_RESTART = 1;
        public const int DISP_CHANGE_FAILED = -1;
}

As we know, the [DllImport("user32.dll")] is an explicit groundwork before injecting an unmanaged implementation in our managed environment.

C#
public static extern int EnumDisplaySettings 
    (string deviceName, int modeNum, ref DEVMODE devMode);

DEVMODE is a structure that is explained in the platform documentation as well as included with Visual Studio .NET. The structure is defined in C#.

C#
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE 
{
        [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)] 
          public string dmDeviceName;
        public short  dmSpecVersion;
        public short  dmDriverVersion;
        ... 
}

You can have a look at the source code attached and apply this logic to any event that you want.

Be aware that, the types have the right size and that fixed length strings are appropriately defined. In this case WORD maps to short, DWORD to int and short stays as short.

C#
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 (0 != User32.EnumDisplaySettings (null, 
       User32.ENUM_CURRENT_SETTINGS, ref dm))
{

At this point the DEVMODE structure will be decorated with the default settings and can modify it at any instance.

C#
dm.dmPelsWidth = iWidth;
dm.dmPelsHeight = iHeight; 
 
int iRet = User32.ChangeDisplaySettings (
  ref dm, User32.CDS_UPDATEREGISTRY);

The enclosed code block does this a little differently to deal with various error conditions. I would encourage you to look at the full source file and see what it does. That's all there is to it.

How to Safeguard the User Screen Resolution

Finally, before we continue, it’s our responsibly to safeguard a user's default screen resolution. In order to do that, you might have to use some static members or classes to hold the user screen resolution and retain it back when you complete the execution.

See the downloads at the top of this article for the source code.

License

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


Written By
Software Developer (Senior) Freelance
India India
He is a certified professional in both MCPD and MCTS. He is a mathematics graduate with masters in computer science.He was born and bred in India and happen to spend some time in Europe.

Comments and Discussions

 
GeneralI really hate to criticize... Pin
Michael Russell22-Apr-04 6:29
Michael Russell22-Apr-04 6:29 
GeneralRe: I really hate to criticize... Pin
dandy7222-Apr-04 7:13
dandy7222-Apr-04 7:13 
GeneralRe: I really hate to criticize... Pin
Rick York22-Apr-04 9:56
mveRick York22-Apr-04 9:56 
GeneralRe: I really hate to criticize... Pin
l a u r e n22-Apr-04 10:06
l a u r e n22-Apr-04 10:06 
GeneralRe: I really hate to criticize... Pin
Michael Russell22-Apr-04 10:16
Michael Russell22-Apr-04 10:16 
GeneralRe: I really hate to criticize... Pin
Wyrm_UK23-Mar-05 4:36
Wyrm_UK23-Mar-05 4:36 
GeneralRe: I really hate to criticize... Pin
Fairy Dave12-Nov-05 18:11
Fairy Dave12-Nov-05 18:11 
GeneralRe: I really hate to criticize... Pin
Michael Russell12-Nov-05 19:40
Michael Russell12-Nov-05 19:40 
GeneralRe: I really hate to criticize... Pin
Fairy Dave12-Nov-05 21:46
Fairy Dave12-Nov-05 21:46 
GeneralRe: I really hate to criticize... Pin
Doug.Chen17-Aug-07 21:03
Doug.Chen17-Aug-07 21:03 
GeneralRe: I really hate to criticize... Pin
DavidFreiberger7-Dec-05 2:52
DavidFreiberger7-Dec-05 2:52 
GeneralRe: I really hate to criticize... Pin
John924129-Jul-16 5:35
John924129-Jul-16 5:35 

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.