博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于屏幕分辨率的一些操作
阅读量:4166 次
发布时间:2019-05-26

本文共 5276 字,大约阅读时间需要 17 分钟。

MonitorAdapter.h

#include 
#include
#include
#include
using std::vector;using namespace std;#define MAX_MONITOR_NAME 256static std::vector
g_hMonitorGroup; // 显示器模式信息typedef struct MonitorModeInfo_t{ unsigned int m_nWidth; unsigned int m_nHeight; MonitorModeInfo_t(int nWidth, int nHeight) : m_nWidth(nWidth), m_nHeight(nHeight) {}}MonitorModeInfo;// 显示器信息struct MonitorInfo{ TCHAR szDevice[MAX_MONITOR_NAME]; // 显示器名称 std::vector
m_vecModeInfo; // 当前名称的显示器支持的分辨率模式};typedef std::vector
VEC_MONITORMODE_INFO; // 所有的显示器信息 class MonitorAdapter{public: MonitorAdapter(); ~MonitorAdapter(); // 回调函数 static int CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdc, LPRECT lpRMonitor, LPARAM dwData); // 得到所有显示器的名称 void GetAllMonitorName(VEC_MONITORMODE_INFO& m_vecMonitorListInfo); // 得到所有显示器的模式 void GetAllDisplayMode(VEC_MONITORMODE_INFO& m_vecMonitorListInfo); //得到屏幕当前分辨率 void GetCurrentReselotion(int& nWidth,int& nHeight,int& nFreq,int& nBits); //修改分辨率 int ChangMonitorReselotion(HMONITOR hMonitor,const int nWidth,const int nHight,const int nFre,const int nColorBits);};
MonitorAdapter.cpp
#include "stdafx.h"#include "MonitorAdapter.h"MonitorAdapter::MonitorAdapter(void){}MonitorAdapter::~MonitorAdapter(void){}int CALLBACK MonitorAdapter::MonitorEnumProc(HMONITOR hMonitor, 											 HDC hdc,											 LPRECT lpRMonitor,											 LPARAM dwData){	g_hMonitorGroup.push_back(hMonitor);	return 1;}// 得到所有显示器的名称void MonitorAdapter::GetAllMonitorName(VEC_MONITORMODE_INFO& vecMonitorListInfo){	g_hMonitorGroup.clear();	::EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, 0);	//vector
::iterator ithMoniter = g_hMonitorGroup.begin(); for(int i = 0; i < g_hMonitorGroup.size();i++) { MONITORINFOEX mixTemp; memset(&mixTemp, 0, sizeof(MONITORINFOEX)); mixTemp.cbSize = sizeof(MONITORINFOEX); GetMonitorInfo(g_hMonitorGroup[i], &mixTemp); VEC_MONITORMODE_INFO::iterator itBeg = vecMonitorListInfo.begin(); VEC_MONITORMODE_INFO::iterator itEnd = vecMonitorListInfo.end(); for(; itBeg != itEnd; ++itBeg) { if( 0 == _tcscmp(mixTemp.szDevice, itBeg->szDevice)) { break; } } //没有在列表中找到,则需要添加 if (itBeg == itEnd) { MonitorInfo tmpMonitorInfo; _tcscpy_s(tmpMonitorInfo.szDevice, sizeof(tmpMonitorInfo.szDevice), mixTemp.szDevice); vecMonitorListInfo.push_back(tmpMonitorInfo); } } }// 得到所有显示器的模式void MonitorAdapter::GetAllDisplayMode(VEC_MONITORMODE_INFO& vecMonitorListInfo){ GetAllMonitorName(vecMonitorListInfo); bool bRetVal; DEVMODE devmode; VEC_MONITORMODE_INFO::iterator itBeg = vecMonitorListInfo.begin(); VEC_MONITORMODE_INFO::iterator itEnd = vecMonitorListInfo.end(); for (NULL; itBeg != itEnd; ++itBeg) { int iMode = 0; do { bRetVal = ::EnumDisplaySettings(itBeg->szDevice, iMode, &devmode); iMode++; if (bRetVal) { bool bFind = false; vector
::iterator itBeg_Mode = itBeg->m_vecModeInfo.begin(); vector
::iterator itEnd_Mode = itBeg->m_vecModeInfo.end(); for (NULL; itBeg_Mode != itEnd_Mode; ++itBeg_Mode) { // 如果已经在列表中找到,则结束本次循环 if ((itBeg_Mode->m_nWidth == devmode.dmPelsWidth) && (itBeg_Mode->m_nHeight == devmode.dmPelsHeight)) { bFind = true; break; } // 插入数据时,从 大到小排列 (按windows 分辨率设置,优先比较 宽) if ( (itBeg_Mode->m_nWidth < devmode.dmPelsWidth) || ((itBeg_Mode->m_nWidth == devmode.dmPelsWidth) && (itBeg_Mode->m_nHeight < devmode.dmPelsHeight)) ) { break; } } if(!bFind) { if (itBeg_Mode == itEnd_Mode) { itBeg->m_vecModeInfo.push_back(MonitorModeInfo(devmode.dmPelsWidth, devmode.dmPelsHeight)); } else { itBeg->m_vecModeInfo.insert(itBeg_Mode, MonitorModeInfo(devmode.dmPelsWidth, devmode.dmPelsHeight)); } } } } while (bRetVal); } }int MonitorAdapter::ChangMonitorReselotion(HMONITOR hMonitor,const int nWidth,const int nHight,const int nFre,const int nColorBits){ if ( NULL == hMonitor ) { return -1; } MONITORINFOEX mi; mi.cbSize = sizeof(mi); GetMonitorInfo( hMonitor , &mi); DEVMODE DeviceMode; ZeroMemory(&DeviceMode, sizeof(DEVMODE)); DeviceMode.dmSize = sizeof(DEVMODE); BOOL bFlag = TRUE; bFlag = EnumDisplaySettings(mi.szDevice, ENUM_CURRENT_SETTINGS, &DeviceMode); if ( bFlag != TRUE ) { return -1; } if (DeviceMode.dmPelsWidth == nWidth && DeviceMode.dmPelsHeight == nHight ) { return 0; } DeviceMode.dmDisplayFlags = 0; DeviceMode.dmPelsWidth= nWidth; DeviceMode.dmPelsHeight = nHight; DeviceMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY ; int nRet = ChangeDisplaySettingsEx(mi.szDevice, &DeviceMode, NULL, CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY, NULL); if (DISP_CHANGE_BADMODE == nRet) { ChangeDisplaySettingsEx(mi.szDevice, &DeviceMode, NULL, CDS_GLOBAL | CDS_NORESET | CDS_UPDATEREGISTRY, NULL); } if ( DISP_CHANGE_SUCCESSFUL == nRet ) { return 0; } return -1;}void MonitorAdapter::GetCurrentReselotion(int& nWidth,int& nHeight,int& nFreq,int& nBits){ DEVMODE DeviceMode; EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &DeviceMode); nWidth = DeviceMode.dmPelsWidth; nHeight = DeviceMode.dmPelsHeight; nFreq = DeviceMode.dmDisplayFrequency; nBits = DeviceMode.dmBitsPerPel;}

转载地址:http://nvqxi.baihongyu.com/

你可能感兴趣的文章
java多线程中的join方法详解
查看>>
idea添加gradle模块报错The project is already registered
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 拖放
查看>>
HTML5学习之——HTML 5 Canvas vs. SVG
查看>>
HTML5学习之——HTML 5 应用程序缓存
查看>>
HTML5学习之——HTML 5 Web Workers
查看>>
HTML5学习之——HTML 5 Canvas
查看>>
HTML5学习之——HTML5 内联 SVG
查看>>
HTML5学习之——HTML 5 服务器发送事件
查看>>
SVG学习之——HTML 页面中的 SVG
查看>>
SVG 形状学习之——SVG圆形
查看>>
SVG 滤镜学习之——SVG 滤镜
查看>>
mysql中用命令行复制表结构的方法
查看>>
hbase shell出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException
查看>>
让代码变得更优雅-Lombok
查看>>
解决Rhythmbox乱码
查看>>
豆瓣爱问共享资料插件发布啦
查看>>
Ubuntu10.10 CAJView安装 读取nh\kdh\caj文件 成功
查看>>
kermit的安装和配置
查看>>