C ++:EnumDisplayMonitors在类中回调

时间:2014-05-07 04:54:39

标签: c++ winapi screen monitor

我遇到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;
}

但是当我将所有内容转移到我的实际代码库并包含在一个类中时,它返回了一个错误。我不知道我是否正确行事。 - &GT;这是片段:

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

1 个答案:

答案 0 :(得分:0)

我刚刚通过将回调函数更改为public来解决了我的问题。