0
我有一個EnumDisplayMonitors回調的問題。我想獲得每個屏幕的數量和屏幕分辨率。它似乎正在使用此代碼。C++:類EnumDisplayMonitors回調
#include <windows.h>
#include <iostream>
#include <vector>
#pragma comment(lib, "user32.lib")
std::vector< std::vector<int> > screenVector;
int screenCounter = 0;
BOOL CALLBACK MonitorEnumProcCallback( _In_ HMONITOR hMonitor,
_In_ HDC hdcMonitor,
_In_ LPRECT lprcMonitor,
_In_ LPARAM dwData) {
screenCounter++;
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
BOOL monitorInfo = GetMonitorInfo(hMonitor,&info);
if(monitorInfo) {
std::vector<int> currentScreenVector;
currentScreenVector.push_back(screenCounter);
currentScreenVector.push_back(abs(info.rcMonitor.left - info.rcMonitor.right));
currentScreenVector.push_back(abs(info.rcMonitor.top - info.rcMonitor.bottom));
std::cout << "Monitor "
<< currentScreenVector.at(0)
<< " -> x: "
<< currentScreenVector.at(1)
<< " y: "
<< currentScreenVector.at(2)
<< "\n";
}
return TRUE;
}
int main() {
BOOL b = EnumDisplayMonitors(NULL,NULL,MonitorEnumProcCallback,0);
system("PAUSE");
return 0;
}
但是,當我將所有內容都傳遞給我的實際代碼庫幷包含在類中時,它將返回一個錯誤。我不知道我是否做得對。 - >這裏的片段:
ScreenManager::ScreenManager() {
BOOL monitorInitialized = EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, reinterpret_cast<LPARAM>(this));
}
BOOL CALLBACK MonitorEnumProc( HMONITOR hMonitor,
HDC hdcMonitor,
LPRECT lprcMonitor,
LPARAM dwData) {
reinterpret_cast<ScreenManager*>(dwData)->callback(hMonitor,hdcMonitor,lprcMonitor);
return true;
}
bool ScreenManager::callback(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor){
screenCounter++;
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
BOOL monitorInfo = GetMonitorInfo(hMonitor,&info);
if(monitorInfo) {
std::vector<int> currentScreenVector;
currentScreenVector.push_back(screenCounter);
currentScreenVector.push_back(abs(info.rcMonitor.left - info.rcMonitor.right));
currentScreenVector.push_back(abs(info.rcMonitor.top - info.rcMonitor.bottom));
}
return true;
}
更好:將靜態枚舉函數集成到類中。比你的課堂回電被允許是私人的! – xMRi