Click here to Skip to main content
15,792,551 members
Articles / Programming Languages / C++

Programmatically change display resolution

Rate me:
Please Sign up or sign in to vote.
3.69/5 (6 votes)
6 Aug 2006CPOL2 min read 79.8K   3.7K   25   7
Programmatically change display resolution from console using API call.


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. C++
    struct DDList {
     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
         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.)


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

Written By
Team Leader BitDefender
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionCodes works properly Pin
Member 1225105424-Nov-22 22:38
Member 1225105424-Nov-22 22:38 
QuestionResolution Pin
Member 145766883-Sep-19 19:01
Member 145766883-Sep-19 19:01 
GeneralWindows 2000 Pin
Ethan Croteau21-Aug-08 10:22
Ethan Croteau21-Aug-08 10:22 
GeneralRe: Windows 2000 Pin
Cristian Amarie22-Aug-08 23:24
Cristian Amarie22-Aug-08 23:24 
GeneralTool to change (and restore) resolution Pin
skst15-Aug-06 8:26
skst15-Aug-06 8:26 
GeneralRe: Tool to change (and restore) resolution Pin
Cristian Amarie15-Aug-06 19:53
Cristian Amarie15-Aug-06 19:53 
GeneralRe: Tool to change (and restore) resolution Pin
skst16-Aug-06 8:02
skst16-Aug-06 8:02 

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.