Click here to Skip to main content
6,596,602 members and growing! (19,069 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

Dynamic Screen Resolution

By sreejith ss nair

Setting the Screen Resolution in C#
C#, Windows, .NET 1.0, Dev
Posted:21 Apr 2004
Updated:22 Jul 2008
Views:137,117
Bookmarked:64 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
32 votes for this article.
Popularity: 5.75 Rating: 3.82 out of 5
5 votes, 15.6%
1
2 votes, 6.3%
2
3 votes, 9.4%
3
4 votes, 12.5%
4
18 votes, 56.3%
5

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.

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.

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
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.

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#.

[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.

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.

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)

About the Author

sreejith ss nair


Member
Sreejith S S Nair is a promising architect and has been programming since 2001. He is a certified professional in both MCPD and MCTS. He is a mathematics graduate with masters in computer science and international business.

He was born and bred in India and happen to spend some time in Europe. He loves to spend leisure with family & friends.

You can reach him at sreejithssnair@hotmail.com
Occupation: Architect
Company: Freelance
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 67 (Total in Forum: 67) (Refresh)FirstPrevNext
GeneralDesktop doesn't follow new orientation on Windows 7 Pinmemberrapik23:24 12 Apr '09  
GeneralDual Monitor Support PinmemberDaniel N22:55 1 Apr '09  
GeneralThanks for this usefull post PinmemberMedoune5:40 9 Mar '09  
GeneralDynamic Screen Resoultion for Windows Mobile Pinmemberjayanice18:51 24 Feb '09  
Question1280x800 ?? Pinmemberronaldo268913:28 20 Nov '08  
Generalconstant size of letters whatever the screen physical size Pinmemberpierre_lannion23:44 14 Sep '08  
AnswerRe: constant size of letters whatever the screen physical size Pinmemberwebatxcent9:18 15 Jan '09  
Generalget available screen resolutions Pinmemberpravin parmar20:32 15 Oct '07  
GeneralRe: get available screen resolutions Pinmembersreejith ss nair1:15 16 Oct '07  
GeneralRe: get available screen resolutions Pinmemberpravin parmar19:37 16 Oct '07  
GeneralRe: get available screen resolutions PinmemberSKRaj7810:29 22 Jan '08  
GeneralRe: get available screen resolutions PinmemberSKRaj7811:10 22 Jan '08  
GeneralRe: get available screen resolutions PinmemberSKRaj7811:56 22 Jan '08  
GeneralRe: get available screen resolutions Pinmemberizne17:45 31 Mar '08  
QuestionCDS_TEST is 0x2 and CDS_UPDATEREGISTRY = 0x1??? PinmemberAYR Chen22:08 17 Aug '07  
GeneralError Message Pinmembermegaroo7915:47 26 Jun '07  
GeneralRe: Error Message Pinmembershradds5:29 18 Mar '09  
Generalvb.net version Pinmembernandhucbm3:55 5 Jun '07  
Generalplease help me regarding use u,r logic in vb.net Pinmemberram krishna pattnayak22:08 13 Feb '07  
GeneralRe: please help me regarding use u,r logic in vb.net Pinmembersreejith ss nair0:13 12 Mar '07  
GeneralUseful tip PinmemberVyacheslav Trubarov12:25 13 Jan '07  
GeneralRe: Useful tip PinmemberSKRaj7810:19 22 Jan '08  
Generalhiii sreejith Pinmemberkannan m4:52 24 Jul '06  
GeneralRe: hiii sreejith Pinmembersreejith ss nair5:41 7 Aug '06  
AnswerRe: hiii sreejith PinmemberJohn Storer II3:21 14 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Jul 2008
Editor: Sean Ewington
Copyright 2004 by sreejith ss nair
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project