I have a problem with EnumDisplayMonitors callbacks. I want to get the number of screens and the screen resolutions of each one. It seems that it's working using this code.

#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;
}

But when I transferred everything to my actual codebase and contained inside a class, it's returning an error. I don't know if I'm doing this right. -> Here's the snippet:

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;
}
有帮助吗?

解决方案

I just solved my problem by changing the callback function to public.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top