C ++函数回调

时间:2014-05-07 05:32:58

标签: c++ class callback

这是我在这篇文章中继续关注的问题:How do I get the dimensions (resolution) of each display?

我想在一个类中包含解决方案。但它在编译时保留了这个错误:

  

错误C2065:'MonitorEnumProc':未声明的标识符。

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) );
        screenVector.push_back( currentScreenVector );
    }

    return true;
}

提前致谢!

1 个答案:

答案 0 :(得分:3)

在您调用EnumDisplayMonitors时,编译器不知道MonitorEnumProc存在。您有两种选择:

更改这两个功能的顺序,因此MonitorEnumProc首先出现:

BOOL CALLBACK MonitorEnumProc(  HMONITOR hMonitor,
                                HDC hdcMonitor,
                                LPRECT lprcMonitor,
                                LPARAM dwData ) {

    reinterpret_cast<ScreenManager*>(dwData)->callback(hMonitor,hdcMonitor,lprcMonitor);
    return true;
}

ScreenManager::ScreenManager() {
     BOOL monitorInitialized = EnumDisplayMonitors( NULL, NULL, MonitorEnumProc, reinterpret_cast<LPARAM>(this) );
}

或者,

在尝试引用MonitorEnumProc之前添加转发声明:

BOOL CALLBACK MonitorEnumProc(  HMONITOR hMonitor,
                                HDC hdcMonitor,
                                LPRECT lprcMonitor,
                                LPARAM dwData );

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