Enumerate and Change Display Modes

http://www.codeproject.com/Articles/2518/Enumerate-and-Change-Display-Modes

 

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值