Skip to main content
Email Password   helpLost your password?

Sample Image - enum_display_modes.jpg

Introduction

This article will briefly describe how to get all possible display modes for a system, including the current mode and also how to change the display mode dynamically.

Enumerating All Modes

To get all display modes available on the system, we use the EnumDisplaySettings API function.

From MSDN:
The EnumDisplaySettings function retrieves information about one of the graphics modes for a display device. To retrieve information for all the graphics modes of a display device, make a series of calls to this function.

So, to get all modes, we need to call this function until it returns FALSE. Here's the code:

BOOL		bRetVal;
CString		sDevMode;

iMode = 0;

do
{
    bRetVal = ::EnumDisplaySettings(NULL, iMode, &devmode);
    iMode++;
    if (bRetVal)
    {
        sDevMode.Format("%d x %d, %d bits %dhtz",
         devmode.dmPelsWidth, devmode.dmPelsHeight,
         devmode.dmBitsPerPel, devmode.dmDisplayFrequency);
        
        // list box for all modes (see demo) 

        if (m_lb1.AddString(sDevMode)==LB_ERR)
            AfxMessageBox("An error occurred!!!");
	}
}
while (bRetVal);

In the above code segment, we increment the iModeNum parameter before each subsequent call to EnumDisplaySettings. According to MSDN, graphics mode indexes start at zero and when you call EnumDisplaySettings with iModeNum set to zero, the OS initializes and caches information about the display device. When you call the function with iModeNum set to a non-zero value, the function returns the information that was cached the last time the function was called with iModeNum set to zero.

The Current Display Mode

To find the display mode currently in use, set the iModeNum parameter of EnumDisplaySettings to ENUM_CURRENT_SETTINGS.

if (::EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devmode))
{
    sDevMode.Format("%i x %i, %i bits %dhtz",
         devmode.dmPelsWidth, devmode.dmPelsHeight,
         devmode.dmBitsPerPel, devmode.dmDisplayFrequency);
	
	m_lb1.SelectString(0, sDevMode);
}

Changing Modes

If you want to change the current display mode, use the ChangeDisplaySettings API function.

BOOL bRetVal;

iMode = m_lb1.GetCurSel();
bRetVal = ::EnumDisplaySettings(NULL, iMode, &devmode);
if (bRetVal)
{
    devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | 
                     DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
                  ::ChangeDisplaySettings(&devmode, 0);
}

Conclusion

This code can be used for DirectX programming to make sure the display adapter supports the correct modes. Other than that, its probably not a good idea to change the user's display mode in your app. But hey, that's up to you. (It is fun to play with however :) ).

Revision History

30 Jun 2002 - Initial Revision

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionHow to get list of modes that monitor cannot display? Pin
voltar23
11:36 11 Oct '07  
GeneralMulti-Devices Pin
Ted Lin
22:50 6 Feb '07  
GeneralRe: Multi-Devices Pin
Jason Henderson
3:42 7 Feb '07  
GeneralRe: Multi-Devices Pin
Ted Lin
17:06 7 Feb '07  
QuestionEnumerate and Change to hidden modes Pin
Robert Lin
1:22 30 Mar '06  
AnswerRe: Enumerate and Change to hidden modes Pin
Member 1268819
23:36 9 Apr '09  
GeneralPersistant Settings Pin
Steve Hart
13:35 1 Feb '04  
GeneralRe: Persistant Settings Pin
Jason Henderson
5:27 2 Feb '04  
GeneralRe: Persistant Settings Pin
Steve Hart
9:22 3 Feb '04  
Generalconvexing affect Pin
wizavi
18:25 23 Mar '03  
GeneralRe: convexing affect Pin
Jason Henderson
3:28 24 Mar '03  
GeneralMulti Monitor Pin
Anonymous
7:37 13 Dec '02  
GeneralRe: Multi Monitor Pin
Jason Henderson
11:04 13 Dec '02  
GeneralHandy bit of Code... Pin
Joel Holdsworth
5:34 30 Jun '02  


Last Updated 29 Jun 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009