我怎样才能让我的回调函数工作?
我使用 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, HDC hdcMonitor, LPRECT lprcMonitor, 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
)?
I'm getting monitor information using 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, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData){
classVar.appendData("callback");
return true;
}
bool Class::f(){
...
EnumDisplayMonitors(NULL,NULL,MonitorEnumProc,NULL);
...
}
Class::callback
is static (if it isn't I get error C2352: illegal call of non-static function). This however causes problems with classVar
: error C2228: left of '.appendData must have class/struct/union'. What should I be doing here to get around this problem (I want the callback to write data to classVar
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
EnumDisplayMonitors()< 的最后一个参数/code>
是保留供调用者使用的额外指针。它未经解释地传递给回调函数。传递一个指向类实例的指针。
The last parameter of
EnumDisplayMonitors()
is an extra pointer reserved for use by the caller. It is passed uninterpreted to the callback function. Pass a pointer to the class instance.使用 LPARAM dwData 提供指向对象的指针。如果有更多数据要提供给回调,则使用辅助结构将所有数据放在一起并将指针传递给该结构。
编辑:使用辅助结构:
Use LPARAM dwData to provide pointer to the object. If there's more data to provide to callback then use auxiliary struct to put all data together and pass pointer to this struct.
EDIT: With auxiliary struct:
您可以使用 dwData 参数将指针传递给您的类实例,即类似这样的内容(注意:回调不再需要是静态的 - 实际上它已经过时了):
You can use the dwData argument to pass in a pointer to your class instance, i.e. something like this (note: callback won't need to be static anymore - actually it becomes obsolete):
我有时会遇到这个问题,通常通过函数对象解决它,它们比静态函数更通用,您可以创建一个可以在传递给 MonitorEnumProc 之前记住任何参数的函数。
I sometimes have this problem, and usually solve it through function objects, they are more versatile that static functions, and you can create one which can memorize any parameter before being passed to MonitorEnumProc.