Enumerate and Change Display Modes

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

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.

目录
相关文章
|
3月前
|
搜索推荐 算法 JavaScript
基于springboot的健康饮食营养管理系统
本系统基于Spring Boot、Vue与MySQL技术,融合大数据与AI算法,构建个性化健康饮食管理平台。结合用户身体状况、目标需求,智能推荐营养方案,助力科学饮食与健康管理。
|
5月前
|
数据采集 机器学习/深度学习 分布式计算
客户老是流失?可能是你没用好大数据!
客户老是流失?可能是你没用好大数据!
159 2
|
SQL 关系型数据库 MySQL
MySQL必看表设计经验汇总-下(精华版)
MySQL必看表设计经验汇总-下(精华版)
363 1
|
文字识别 Android开发 C++
Tesseract OCR集成Android Studio实现OCR识别
Tesseract OCR集成Android Studio实现OCR识别
920 0
|
机器学习/深度学习 人工智能 自然语言处理
【机器学习】GLM4-9B-Chat大模型/GLM-4V-9B多模态大模型概述、原理及推理实战
【机器学习】GLM4-9B-Chat大模型/GLM-4V-9B多模态大模型概述、原理及推理实战
1863 0
|
XML 敏捷开发 存储
深入理解自动化测试中的数据驱动方法
【5月更文挑战第25天】 在现代软件开发过程中,自动化测试是确保产品质量和加速交付速度的关键手段。本文将探讨数据驱动测试(DDT)方法的核心概念、实施策略以及它如何提升自动化测试的灵活性和效率。不同于常规摘要,本文将通过实际案例分析,揭示数据驱动方法在不同测试场景中的应用,并讨论其对测试覆盖率和可靠性的影响。
|
关系型数据库 数据库 PostgreSQL
【docker-compose】一键安装PostgreSQL数据库
【docker-compose】一键安装PostgreSQL数据库
5848 0
【docker-compose】一键安装PostgreSQL数据库
|
人工智能 前端开发 搜索推荐
使用 Next.js 和 OpenAI 构建旅行助理
使用 Next.js 和 OpenAI 构建旅行助理
384 0
|
数据可视化 数据挖掘
PCA 解释方差骤降图(ElbowPlot)拐点的量化识别
本文分享了一种在R语言里面实现量化识别 PCA方差贡献肘部图(ElbowPlot)中拐点 的简单方法,以供学习参考 定
2100 0