Click here to Skip to main content
Click here to Skip to main content

Programmatically change display resolution

By , 6 Aug 2006
 

Introduction

Recently, on a local forum, someone asked about how to programmatically change the display resolution from a console application (he wanted to place that in a .bat file for gaming). 20 minutes later, I sent him the sources to compile, and it seems to worK OK until now. So I imagined that this little code snippet can prove useful to others.

User: How it works

This is a console application, expecting two arguments, the desired width and height. There are no assumptions or defaults, just a simple input check and the conversions to int.

C:\>chscrres 1024 768
C:\>chscrres 1280 1024

Developer: How it works

  1. The list of devices is enumerated in a structure called DDList. The structure is handled by the functions DDList_Build (constructs the list), DDList_Clean (frees the list), and DDList_Pop (extracts the first item from the list). The implementation uses a simple linked list and the EnumDisplayDevices API to retrieve the display devices, using the DISPLAY_DEVICE_ATTACHED_TO_DESKTOP mask. You can also use other masks as well depending on what you need.
  2. struct DDList {
     DISPLAY_DEVICE Device;
     struct DDList *Next;
    };
  3. After the list is built, the first device is extracted from the list, if any. Depending on the number of devices and/or additional conditions, you may want to impose, rewrite the DDList_Pop call (or implement a DDList_PopEx to pass additional data) to get to the desired DISPLAY_DEVICE structure from DDList.
  4. Finally, we change the resolution for the extracted device. First, the DEVMODE structure is retrieved using a call to EnumDisplaySettingsEx; the existing DEVMODE's dmPelsWidth/dmPelsHeight are compared against the values passed in the command line, and, if at least one is different, the DEVMODE structure is updated with the new width and height and ChangeDisplaySettingsEx is called to update the device.

If ChangeDisplaySettingsEx returned DISP_CHANGE_SUCCESSFUL, then the system is informed about the display change using:

// broadcast change to system
SendMessage(HWND_BROADCAST, WM_DISPLAYCHANGE, 
         (WPARAM)(deviceMode.dmBitsPerPel), 
         MAKELPARAM(newWidth, newHeight));

(Although I tested this on XP, 2000, and NT4, there is absolutely no guarantees that this will work. I don't want to be sued for display damage. Try out and see.)

License

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

About the Author

Cristian Amarie
Team Leader BitDefender
Romania Romania
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralWindows 2000memberEthan Croteau21 Aug '08 - 9:22 
When testing under Windows 2000 it was noticed that the task bar was floating somewhere on the screen if the resolution of the desktop is set higher. Searching for this issue turned up this knowledge base
http://support.microsoft.com/kb/306263[^]
 
To fix, replace:
ChangeDisplaySettingsEx( device.DeviceName, &deviceMode, NULL, 0, NULL)
with:
ChangeDisplaySettingsEx( device.DeviceName, &deviceMode, NULL, CDS_UPDATEREGISTRY, NULL)

GeneralRe: Windows 2000memberCristian Amarie22 Aug '08 - 22:24 
GeneralTool to change (and restore) resolutionmemberskst15 Aug '06 - 7:26 
GeneralRe: Tool to change (and restore) resolutionmemberCristian Amarie15 Aug '06 - 18:53 
GeneralRe: Tool to change (and restore) resolutionmemberskst16 Aug '06 - 7:02 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 6 Aug 2006
Article Copyright 2006 by Cristian Amarie
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid