关于屏幕分辨率的一些操作
生活随笔
收集整理的這篇文章主要介紹了
关于屏幕分辨率的一些操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MonitorAdapter.h
#include <atlstr.h> #include <vector> #include <WinDef.h> #include <tchar.h> using std::vector;using namespace std;#define MAX_MONITOR_NAME 256static std::vector<HMONITOR> 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<MonitorModeInfo> m_vecModeInfo; // 當前名稱的顯示器支持的分辨率模式 };typedef std::vector<MonitorInfo> 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<HMONITOR>::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<MonitorModeInfo>::iterator itBeg_Mode = itBeg->m_vecModeInfo.begin();vector<MonitorModeInfo>::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; }
總結
以上是生活随笔為你收集整理的关于屏幕分辨率的一些操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis关系映射(1对1,1对多,
- 下一篇: 蜗牛慢慢爬 LeetCode 6. Zi