加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何使我的回调功能起作用?

发布时间:2020-12-16 09:37:47 所属栏目:百科 来源:网络整理
导读:我正在使用EnumDisplayMonitors获取监视器信息: BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,HDC hdcMonitor,LPRECT lprcMonitor,LPARAM dwData){ Class::callback(hMonitor,hdcMonitor,lprcMonitor,dwData); return true;}bool Class::callback(HMON
我正在使用EnumDisplayMonitors获取监视器信息:

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,HDC hdcMonitor,LPRECT lprcMonitor,LPARAM dwData){
  Class::callback(hMonitor,hdcMonitor,lprcMonitor,dwData);
  return true;
}

bool Class::callback(HMONITOR hMonitor,LPARAM dwData){
  classVar.appendData("callback");
  return true;
}

bool Class::f(){
  ...
  EnumDisplayMonitors(NULL,NULL,MonitorEnumProc,NULL);
  ...
}

Class :: callback是静态的(如果不是我得到错误C2352:非静态调用非静态函数).但是这会导致classVar出现问题:错误C2228:’.appendData的左边必须有class / struct / union’.我应该在这里做些什么来解决这个问题(我希望回调将数据写入classVar)?

解决方法

EnumDisplayMonitors()的最后一个参数是保留供调用者使用的额外指针.它被解释为未回溯到回调函数.传递指向类实例的指针.

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,LPARAM dwData){
  reinterpret_cast<Class*>(dwData)->callback(hMonitor,lprcMonitor);
  return true;
}

bool Class::callback(HMONITOR hMonitor,LPRECT lprcMonitor){
  classVar.appendData("callback");
  return true;
}

bool Class::f(){
  ...
  EnumDisplayMonitors(NULL,reinterpret_cast<LPARAM>(this));
  ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读