简体   繁体   English

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

[英]How to access the custom class members from a call back method

I want to access the members of a custom class from callback method. 我想从回调方法访问自定义类的成员。

I am accessing m_displayImg->bIsPressAndHold = true; 我正在访问m_displayImg->bIsPressAndHold = true; from call back functions. 从回调函数。

ie

It gives error "Identifier M_displayImg is undefined". 它给出错误“标识符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.
}

How can I access that custom class member? 如何访问该自定义类成员?

Previously i encountered a similar problem. 以前我遇到过类似的问题。 I solved it with namespaced variable, because CALLBACK function cannot accept more parameters, you cannot even put anything in lambda capture list. 我使用命名空间变量解决了该问题,因为CALLBACK函数无法接受更多参数,因此您甚至无法在lambda捕获列表中放入任何内容。

My code sample (different content, but the idea remains the same): 我的代码示例(不同的内容,但是想法仍然相同):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM