获取(多)屏幕尺寸--C++

来源:互联网 发布:办公室网络安装 编辑:程序博客网 时间:2024/06/05 14:29
前段时间要做多屏程序,为了获取屏幕的真实尺寸,还是花了一番功夫的,目前大部分程序所说的实际上只能获取逻辑尺寸(毫米),不能获取真实的值。下面是C++代码:


C++ 代码:


/**************************************************************************************** ** DisplayDevices.h ** ** CopyRight @ chcucl@qq.com ** ** Created at 2014/03/25 10:50 **  ** ** ** *******************************************************************************************/#pragma once#pragma warning(push)#pragma warning(disable:4995)#include <vector>/* *  DSPDEV *   *  It includes parameters of a display device. */struct DSPDEV{/* posx,posy: the position of this display device locating in virtual-desktop */int posx;int posy;/* rsow, rsoh: the width and height of resolution of this display device, in pixels */int rsow;int rsoh;/* mliw, mlih: the width and height of dispaly device, in millimeters  */int mliw;int mlih;};class CDisplayDevices{public:CDisplayDevices();~CDisplayDevices();/* Enumerate all the display devices which PC is connecting.  */bool EnumDisplays();/* Return the number of devices */int GetCount() const;/* Get the device configuration depending the requesting index. */bool GetDspDev(const int index, DSPDEV * dsp);private:void setDspDev(const DISPLAY_DEVICE& device, const DEVMODE& devmode);private:std::vector<DSPDEV>  m_dspList;};#pragma warning(pop)/**************************************************************************************** ** DisplayDevices.cpp ** ** CopyRight @ chcucl@qq.com ** ** Created at 2014/03/25 10:50 **  ** ** ** *******************************************************************************************/#pragma warning(push)#pragma warning(disable:4995)#include <Windows.h>#include <tchar.h>#include <strsafe.h>#include <sstream>#include <vector>#include "DisplayDevices.h"#ifndef TRACE#ifdef CONSOLE_WINDOWED_MODE#define TRACE(arg, ...) _tprintf(arg, __VA_ARGS__); _tprintf(_T("\r\n"));#endif#endifCDisplayDevices::CDisplayDevices(){}CDisplayDevices::~CDisplayDevices(){m_dspList.clear();}bool CDisplayDevices::EnumDisplays(){struct DISPLAY_SETTING{DISPLAY_DEVICE device;DEVMODE devmode;};bool result = true;std::vector<DISPLAY_SETTING> dspArray;for (int devno = 0; ; devno++){DISPLAY_DEVICE device = {0};device.cb = sizeof(DISPLAY_DEVICE);BOOL succ = EnumDisplayDevices(NULL, devno, &device, 0);if (!succ){TRACE(_T("EnumDisplayDevices failed error=%d"), GetLastError());result = false;break;}DEVMODE devmode = {0};int ret = EnumDisplaySettings(device.DeviceName, ENUM_REGISTRY_SETTINGS, &devmode);if (ret == 0){TRACE(_T("EnumDisplaySettings failed error=%d"), GetLastError());break;}if (device.StateFlags | DISPLAY_DEVICE_ATTACHED_TO_DESKTOP){DISPLAY_SETTING setting = { 0 };setting.device = device;setting.devmode = devmode;dspArray.push_back(setting);}}const size_t sz = dspArray.size();for (size_t dspitr = 0; dspitr < sz; ++dspitr){DISPLAY_DEVICE device = { 0 };device.cb = sizeof(DISPLAY_DEVICE);BOOL succ = EnumDisplayDevices(dspArray[dspitr].device.DeviceName, 0, &device, EDD_GET_DEVICE_INTERFACE_NAME);if (succ){setDspDev(device, dspArray[dspitr].devmode);}}return result;}int CDisplayDevices::GetCount() const{return (int)m_dspList.size();}bool CDisplayDevices::GetDspDev(const int index, DSPDEV * dsp){if (index >=0 && index < (int)m_dspList.size()){if (dsp != NULL){*dsp = m_dspList[index];return true;}}return false;}void CDisplayDevices::setDspDev(const DISPLAY_DEVICE& device, const DEVMODE& devmode){TRACE(_T("device id=%s"), device.DeviceID); // ??#ifdef UNICODEtypedef std::wstring String;#elsetypedef std::string  String;#endifString deviceId = device.DeviceID;String::size_type pos = 0, lastpos = 0;String::size_type len = deviceId.length();std::vector<String> params;for (; ;){pos = deviceId.find_first_of(_T("#"), lastpos);if (pos == String::npos){if (pos >= len)break;}params.push_back(deviceId.substr(lastpos, pos-lastpos));lastpos = pos + 1;}String brand = params[1];String param = params[2];String displayKey = _T("SYSTEM\\CurrentControlSet\\Enum\\DISPLAY\\");displayKey += brand;displayKey += _T("\\");displayKey += param;displayKey += _T("\\Device Parameters");// //  Open Register Key//int mliw = 0;int mlih = 0;HKEY hKey = NULL;long ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, displayKey.c_str(), 0, KEY_READ, &hKey);if (ret == ERROR_SUCCESS){DWORD sz = 256;byte edid[256] = {0};if (RegQueryValueEx(hKey, _T("EDID"), NULL, NULL, edid, &sz) == ERROR_SUCCESS){mliw = (int)edid[21] * 10;mlih = (int)edid[22] * 10;}RegCloseKey(hKey);}DSPDEV dspdev = { 0 };dspdev.posx = devmode.dmPosition.x;dspdev.posy = devmode.dmPosition.y;dspdev.rsow = devmode.dmPelsWidth;dspdev.rsoh = devmode.dmPelsHeight;dspdev.mliw = mliw;dspdev.mlih = mlih;m_dspList.push_back(dspdev);}#pragma warning(pop)

测试程序:

// main.cpp#pragma warning(push)#pragma warning(disable:4995)#include <Windows.h>#include <tchar.h>#include <strsafe.h>#include <list>#include "DisplayDevices.h"int main(){CDisplayDevices d;d.EnumDisplays();const int dspCount = d.GetCount();for (int i = 0; i < dspCount; ++i){DSPDEV dsp = { 0 };d.GetDspDev(i, &dsp);_tprintf(_T("\r\nDSP[%d] \r\n   resolution=>[W:%d, H:%d] \r\n   size:[W:%d, H:%d] \r\n\r\n"),i, dsp.rsow, dsp.rsoh, dsp.mliw, dsp.mlih);}return 0;}#pragma warning(pop)


0 0