关于获取多个屏幕分辨率以及进行一些设置
生活随笔
收集整理的這篇文章主要介紹了
关于获取多个屏幕分辨率以及进行一些设置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Windows下提示顯示器信息主要通過兩個函數實現。一個是EnumDisplayDevices(), 另一個是EnumDisplayMonitors().
EnumDisplayDevices()枚舉所有顯示設備,而EnumDisplayMonitors枚舉的是所有顯示器。
顯示設備和顯示器不一樣,比如顯卡算顯示設備,但是不是顯示器。
EnumDisplayMonitors()還會枚舉出不可見的偽顯示器,如果只是想得到實際的顯示器數目的話可以用GetSystemMetrics(SM_CMONITORS),該函數不包括虛擬顯示器。
MonitorInfoex 和MonitorInfo 在這兩個結構中保存著相應顯示器的相關信息,如坐標、是否為主顯示器等 GetMonitorInfo ( ) 取得指定顯示器的相關信息,如物理顯示區大小等
MonitorFromPoint ( ) 取得指定點所在的顯示器句柄
MonitorFromRect ( ) 取得指定矩形所在的顯示器句柄
MonitorFromWindow( ) 取得指定窗口所在的顯示器句柄
MonitorEnumProc( ) 當應用程序調用EnumDisplayMonitors ( )查詢顯示器個數時,系統自動為每一個顯示器調用一次該函數。應用程序可以依此判斷顯示器的個數、位置及顯示區域的大小等信息
函數名:MonitorEnumProc()
輸入參數:HMONITOR hMonitor ---顯示器句柄
????????? HDC hdcMonitor? ----顯示器DC句柄
???? ?? LPRECT lprcMonitor-----
???? ?? LPARAM dwData-----EnumDisplayMonitors傳來的數據
返回:bool
功能:若返回為真,EnumDisplayMonitors繼續枚舉,
????? 若返回為假,EnumDisplayMonitors停止枚舉,從而獲得顯示器信息
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);//根據屏幕ID取獲取屏幕的對應分辨率void GetCurrentReselotion(LPCWSTR lpszDeviceName, 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; }void MonitorAdapter::GetCurrentReselotion(LPCWSTR lpszDeviceName, int& nWidth, int& nHeight, int& nFreq, int& nBits) {DEVMODE DeviceMode;EnumDisplaySettings(lpszDeviceName, ENUM_CURRENT_SETTINGS, &DeviceMode);nWidth = DeviceMode.dmPelsWidth;nHeight = DeviceMode.dmPelsHeight;nFreq = DeviceMode.dmDisplayFrequency;nBits = DeviceMode.dmBitsPerPel; }
test #include "stdafx.h" #include "MonitorAdapter.h"int _tmain(int argc, _TCHAR* argv[]) {MonitorAdapter m_monitorAdapter; //顯示器VEC_MONITORMODE_INFO vecMointorListInfo;m_monitorAdapter.GetAllDisplayMode(vecMointorListInfo);int nWidth = 0, nHeight = 0, nFreq = 0, nBits = 0;VEC_MONITORMODE_INFO::iterator itBeg = vecMointorListInfo.begin();for (int i = 0; i < vecMointorListInfo.size();i++){//得到當前顯示器分辨率 刷新率 色位m_monitorAdapter.GetCurrentReselotion(itBeg->szDevice, nWidth, nHeight, nFreq, nBits);itBeg++;nWidth = 0, nHeight = 0, nFreq = 0, nBits = 0;}return 0; }
EnumDisplayDevices()枚舉所有顯示設備,而EnumDisplayMonitors枚舉的是所有顯示器。
顯示設備和顯示器不一樣,比如顯卡算顯示設備,但是不是顯示器。
EnumDisplayMonitors()還會枚舉出不可見的偽顯示器,如果只是想得到實際的顯示器數目的話可以用GetSystemMetrics(SM_CMONITORS),該函數不包括虛擬顯示器。
MonitorInfoex 和MonitorInfo 在這兩個結構中保存著相應顯示器的相關信息,如坐標、是否為主顯示器等 GetMonitorInfo ( ) 取得指定顯示器的相關信息,如物理顯示區大小等
MonitorFromPoint ( ) 取得指定點所在的顯示器句柄
MonitorFromRect ( ) 取得指定矩形所在的顯示器句柄
MonitorFromWindow( ) 取得指定窗口所在的顯示器句柄
MonitorEnumProc( ) 當應用程序調用EnumDisplayMonitors ( )查詢顯示器個數時,系統自動為每一個顯示器調用一次該函數。應用程序可以依此判斷顯示器的個數、位置及顯示區域的大小等信息
函數名:MonitorEnumProc()
輸入參數:HMONITOR hMonitor ---顯示器句柄
????????? HDC hdcMonitor? ----顯示器DC句柄
???? ?? LPRECT lprcMonitor-----
???? ?? LPARAM dwData-----EnumDisplayMonitors傳來的數據
返回:bool
功能:若返回為真,EnumDisplayMonitors繼續枚舉,
????? 若返回為假,EnumDisplayMonitors停止枚舉,從而獲得顯示器信息
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);//根據屏幕ID取獲取屏幕的對應分辨率void GetCurrentReselotion(LPCWSTR lpszDeviceName, 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; }void MonitorAdapter::GetCurrentReselotion(LPCWSTR lpszDeviceName, int& nWidth, int& nHeight, int& nFreq, int& nBits) {DEVMODE DeviceMode;EnumDisplaySettings(lpszDeviceName, ENUM_CURRENT_SETTINGS, &DeviceMode);nWidth = DeviceMode.dmPelsWidth;nHeight = DeviceMode.dmPelsHeight;nFreq = DeviceMode.dmDisplayFrequency;nBits = DeviceMode.dmBitsPerPel; }
test #include "stdafx.h" #include "MonitorAdapter.h"int _tmain(int argc, _TCHAR* argv[]) {MonitorAdapter m_monitorAdapter; //顯示器VEC_MONITORMODE_INFO vecMointorListInfo;m_monitorAdapter.GetAllDisplayMode(vecMointorListInfo);int nWidth = 0, nHeight = 0, nFreq = 0, nBits = 0;VEC_MONITORMODE_INFO::iterator itBeg = vecMointorListInfo.begin();for (int i = 0; i < vecMointorListInfo.size();i++){//得到當前顯示器分辨率 刷新率 色位m_monitorAdapter.GetCurrentReselotion(itBeg->szDevice, nWidth, nHeight, nFreq, nBits);itBeg++;nWidth = 0, nHeight = 0, nFreq = 0, nBits = 0;}return 0; }
總結
以上是生活随笔為你收集整理的关于获取多个屏幕分辨率以及进行一些设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Map接口详解
- 下一篇: 【java项目实践】具体解释Ajax工作