类中的EnumDisplayMonitors回调函数
C++: EnumDisplayMonitors callback inside a class
我有一个问题与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;
}
我只是通过将回调函数更改为public来解决我的问题。
相关文章:
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- 从不同的 cpp 调用回调函数会导致bad_function_call
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- C 回调函数在对象 pascal 上崩溃
- 类 Referention 中C++回调函数引用非静态函数
- CURLOPT_INTERLEAVEFUNCTION回调函数始终接收 nullptr 作为用户数据指针
- 动态链接库中C++回调函数
- 我想制作设置鼠标回调函数,以便在仍然按下鼠标左键时连续传递坐标
- 使用回调函数从构造函数调用虚拟/派生方法的替代方法?
- 我可以使用谷歌 v8 在回调函数中获取 JavaScript 函数C++源文本吗?
- 重写另一个方法 [C++] 使用的超类回调函数
- 是否可以影响 C++ 中回调函数的局部变量?
- C++ Qt5 中带有 pigpio 库的回调函数
- 在定时器回调函数中使用 Sleep() 会导致C++出现问题吗?
- C++在赋值或回调函数时重载模板
- 如何用不同的参数类型和数字回调函数
- 如何为Python Swigged C++对象创建和分配回调函数
- 用回调函数替换C++正则表达式
- C++带有回调函数的模板类