Click here to Skip to main content
6,635,160 members and growing! (17,286 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Win32/64 SDK & OS » General     Beginner License: The Code Project Open License (CPOL)

Programmatically change display resolution

By Cristian Amarie

Programmatically change display resolution from console using API call
C++, Windows, Visual Studio, Dev
Posted:6 Aug 2006
Views:25,777
Bookmarked:21 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 2.56 Rating: 3.67 out of 5
1 vote, 20.0%
1

2

3
2 votes, 40.0%
4
2 votes, 40.0%
5

Introduction

One of the previous days, on a local forum, someone asked how to programmatically change his display resolution from a console application (he wanted to place that in a .bat file for gaming). 20 minutes later, I send him the sources to compile and seems ok until now. So I imagined that this little code snippet can prove useful also 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 are 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 list). The implementation uses a simple linked list and EnumDisplayDevices API to retrieve the display devices, using DISPLAY_DEVICE_ATTACHED_TO_DESKTOP mask. You can use also other masks as well depending on what you need.

struct DDList {
 DISPLAY_DEVICE Device;
 struct DDList *Next;
};

2. After the list is built, then the first device is extracted from 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 one to pass additional data) to get to the desired DISPLAY_DEVICE structure from DDList.

3. Finally, we're changing the resolution for 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 command line, and, if at least one is different, DEVMODE structure is updated with the new width and height and ChangeDisplaySettingsEx is called to update the device.

If the ChangeDisplaySettingsEx returned DISP_CHANGE_SUCCESSFUL, then system is informed about 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


Member

Occupation: Software Developer (Senior)
Company: Gemini Solutions
Location: Romania Romania

Other popular Win32/64 SDK & OS articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralWindows 2000 PinmemberEthan Croteau10:22 21 Aug '08  
GeneralRe: Windows 2000 PinmemberCristian Amarie23:24 22 Aug '08  
GeneralTool to change (and restore) resolution Pinmemberskst8:26 15 Aug '06  
GeneralRe: Tool to change (and restore) resolution PinmemberCristian Amarie19:53 15 Aug '06  
GeneralRe: Tool to change (and restore) resolution Pinmemberskst8:02 16 Aug '06  

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

PermaLink | Privacy | Terms of Use
Last Updated: 6 Aug 2006
Editor:
Copyright 2006 by Cristian Amarie
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project