Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC

Article on Screen Resolution

Rate me:
Please Sign up or sign in to vote.
2.81/5 (30 votes)
17 Apr 20034 min read 161.6K   4.5K   25   26
Article on changing screen resolution

Sample Image - ScreenResolution.gif

Introduction

Recently, while developing an accounting application for a client who lives about 300 miles away from me, I realized that I had developed the various windows to suit my screen resolution. A few questions came to my mind before I gave him the setup files: Will his screen have the same resolution? If he changes it one-time on my instructions, what happens if he changes it for some reason? I found that the best option would be to include in the code, some lines which change the resolution as per my requirements at the start of the application and revert back to what was original, at the end of the application.

I found some helpful articles in MSDN which I used to develop these projects. This article consists of the following 3 projects:

  • ScreenResolution - Window GUI Application
  • ScreenResolution32 - Console Utility
  • ScrResOCX - ActiveX Control

About the Code

You retrieve the DEVMODE structure with a call to EnumDisplaySettings. Then modify the retrieved DEVMODE structure to your requirements and pass the structure to ChangeDisplaySettings() to change the resolution.

C++
DEVMODE dvmd;
EnumDisplaySettings(NULL, dwModeNum, &dvmd);
ChangeDisplaySettings(&dvmd, 0);
// parameter 2 can be either CDS_TEST , CDS_UPDATEREGISTRY OR 0 (temporary)

Problems Encountered

While attempting to change the resolution to a very high value, for example (1600x1200), the system goes into a loop. The function (ChangeDisplaySettings()) returns success and all attempts to SetTimer() to change back to the original resolution fails.

How to Use

ScreenResolution

This is a normal Windows GUI application which starts as shown in the image above. Select the resolution you want to set and click on "Change Res" button. If the change is possible (read the Known Problems section), you have two options to make the changes: Temporary, Write changes to registry to make change permanent or revert back to the original resolution. Note: Temporary change means that if you re-boot your system, the original screen resolution will be restored not the changed resolution. An application icon is created in the System Tray (not very essential) and you can hide the application by selecting Hide from the menu.

ScreenResoution32

This sub-project is basically meant for use inside other applications developed in Visual Basic, FoxPro, Delphi PowerBuilder, VC++, etc. You invoke the executable by passing parameters: width, height, frequency, like this in Visual Basic.

VB
Shell ("C:\ScreenResolution32\Debug\ScreenResolution32 /w800 /h600 /b16")

You can also test it in Windows by selecting from the Start bar, "Run/Browse". Then select the application and add the parameter as shown in the line of code above: "Shell ..."

You can also open a MS-DOS window and move to the location of the application by typing the following command for example (press Enter after each command).

  • CD\ScreenResolution32\Debug
  • ScreenResolution32 /w1024 /h768 /8

    OR

  • ScreenResolution32 /D (this will display the available resolutions)

ScrResOCX

You can use this control in applications which allow hosting of Active-X controls like Visual Basic, HTML, FoxPro etc. Given below are the instructions for using this control in Visual Basic. Download the demo applications zip file and unzip the files OR build the OCX from the ScrResOCX project source.

  • Start Visual Basic
  • Create a Standard Exe project
  • Select Project/Components from menu
  • On the Controls tab, click Browse
  • Select the ScrResOCX.ocx file which you unzipped from the demo zip file
  • Click Apply
  • Click Close
  • Drag the newly added item (icon of computer screen) on the form
  • Add a command button to the form
  • In the Command1_Click() function, type the following line:
    VB
    ScrResOCX1.ScreenWidth = 1024
    ScrResOCX1.ScreenHeight = 768
    ScrResOCX1.ScreenBits = 8
    ScrResOCX1.ChangeResolution
  • Run the application
  • Click on the command button

Known Problems

When you change resolution from low to high, for example (800x600 to 1024x768), the size of Windows Start bar remains the same, move the Start bar to the bottom of the Desktop and it will resize to the Desktop width.

On my screen, the application shows displayable screen resolutions 1280x1024 and 1600x1200 etc., with frequencies of 4, 8 and 16, but when I try to set any of these resolutions, my monitor stays blank for a very long time due to which I have to re-boot the system. This is not a very serious problem since the call to change resolution (ChangeDisplaySettings()) is temporary and not to the Registry.

In case, if while changing the resolution, the system runs into trouble, start the system in "Safe Mode" and change the resolution to the desired by left clicking on the desktop area, selecting properties and from the displayed window, selecting the settings tab and moving the slider in the "Screen Area" box and clicking Apply.

History

New article.

Use them, abuse them, but don't blame me.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
India India
Nothing to boast about

Comments and Discussions

 
QuestionHas anybody seen my tool bar ? Pin
Member 98686129-Apr-04 3:22
Member 98686129-Apr-04 3:22 
Generalit's not that crazy Pin
philipcunningham29-Feb-04 23:27
philipcunningham29-Feb-04 23:27 
GeneralRe: it's not that crazy Pin
Barretto VN2-Mar-04 0:03
Barretto VN2-Mar-04 0:03 
Thanks

i had made these 3 different project with a very specific purpose, and you have proved me right

THANKS AGAIN Smile | :)
GeneralIts useful to get the screen res too Pin
fjaguirre214-Nov-03 6:04
fjaguirre214-Nov-03 6:04 
GeneralRe: Its useful to get the screen res too Pin
Barretto VN12-Dec-03 0:57
Barretto VN12-Dec-03 0:57 
GeneralThere is actually a better way... Pin
alman22-Apr-03 13:56
alman22-Apr-03 13:56 
GeneralRe: There is actually a better way... Pin
Barretto VN6-Sep-03 1:17
Barretto VN6-Sep-03 1:17 
GeneralComplainers miss the value Pin
Answers200019-Apr-03 6:31
Answers200019-Apr-03 6:31 
GeneralRe: Complainers miss the value Pin
John M. Drescher21-Apr-03 6:07
John M. Drescher21-Apr-03 6:07 
GeneralAdmin remotely change screen resolution ! Pin
Kochise4-Sep-03 5:06
Kochise4-Sep-03 5:06 
GeneralThanks Pin
Barretto VN11-Oct-03 22:45
Barretto VN11-Oct-03 22:45 
QuestionWhy so much anger? Pin
Bartosz Bien18-Apr-03 11:19
Bartosz Bien18-Apr-03 11:19 
AnswerRe: Why so much anger? Pin
dog_spawn18-Apr-03 13:31
dog_spawn18-Apr-03 13:31 
GeneralRe: Why so much anger? Pin
Bartosz Bien18-Apr-03 21:04
Bartosz Bien18-Apr-03 21:04 
GeneralStopping post garbage-level articles, Man! Pin
Member 964475518-Apr-03 8:51
Member 964475518-Apr-03 8:51 
GeneralRe: Stopping post garbage-level articles, Man! Pin
#realJSOP18-Apr-03 9:01
mve#realJSOP18-Apr-03 9:01 
GeneralRe: Stopping post garbage-level articles, Man! Pin
John M. Drescher18-Apr-03 9:10
John M. Drescher18-Apr-03 9:10 
GeneralRe: Stopping post garbage-level articles, Man! Pin
Earl Allen22-Apr-03 20:55
Earl Allen22-Apr-03 20:55 
GeneralRe: Stopping post garbage-level articles, Man! Pin
WREY22-Apr-03 21:32
WREY22-Apr-03 21:32 
GeneralTell us you're joking.... Pin
Jim A. Johnson18-Apr-03 7:39
Jim A. Johnson18-Apr-03 7:39 
GeneralThanks Pin
John M. Drescher18-Apr-03 4:54
John M. Drescher18-Apr-03 4:54 
GeneralThis is crazy! Pin
Paul A. Howes18-Apr-03 3:43
Paul A. Howes18-Apr-03 3:43 
GeneralRe: This is crazy! Pin
#realJSOP18-Apr-03 3:55
mve#realJSOP18-Apr-03 3:55 
GeneralRe: This is crazy! Pin
Rama Krishna Vavilala18-Apr-03 7:47
Rama Krishna Vavilala18-Apr-03 7:47 
GeneralRe: This is crazy! Pin
mwilliamson18-Apr-03 13:54
mwilliamson18-Apr-03 13:54 

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.