如何从回调方法访问自定义类成员



我想从回调方法访问自定义类的成员。

我正在从回调函数访问m_displayImg->bIsPressAndHold = true;

它给出错误"标识符M_displayImg未定义"。

class CDisplayImage
{
public:
    CDisplayImage(void);
    virtual ~CDisplayImage(void);
    int Initialize(HWND hWnd, CDC* dc, IUnknown* controller);
    void Uninitialize(int context);
    BOOL bIsPressAndHold = false;
    //code omitted
};

VOID CALLBACK DoCircuitHighlighting(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
    m_displayImg->bIsPressAndHold = true;       
   // I have omitted code for simplicity.
}

如何访问该自定义类成员?

以前我遇到了类似的问题。我用命名空间变量解决了它,因为 CALLBACK 函数不能接受更多参数,你甚至不能在 lambda 捕获列表中放置任何东西。

我的代码示例(内容不同,但想法保持不变):

#include <windows.h>
#include <algorithm>
#include <vector>
namespace MonitorInfo {
    // namespace variable
    extern std::vector<MONITORINFOEX> data = std::vector<MONITORINFOEX>();
    // callback function
    BOOL CALLBACK callbackFunction(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
        MONITORINFOEX monitorInfo;
        monitorInfo.cbSize = sizeof(monitorInfo);
        GetMonitorInfo(hMonitor, &monitorInfo);
        data.push_back(monitorInfo);
        return true;
    }
    // get all monitors data
    std::vector<MONITORINFOEX> get(){
        data.clear();
        EnumDisplayMonitors(NULL, NULL, callbackFunction, 0);
        return data;
    }
};
int main(){
    auto info = MonitorInfo::get();
    printf("%d", info.size());
    return 0;
}

最新更新