我怎样才能让我的回调函数工作?

发布于 2024-12-10 01:23:48 字数 695 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽清梦 2024-12-17 01:23:48

EnumDisplayMonitors()< 的最后一个参数/code>是保留供调用者使用的额外指针。它未经解释地传递给回调函数。传递一个指向类实例的指针。

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

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

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

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.

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

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

bool Class::f(){
  ...
  EnumDisplayMonitors(NULL,NULL,MonitorEnumProc,reinterpret_cast<LPARAM>(this));
  ...
}
﹂绝世的画 2024-12-17 01:23:48

使用 LPARAM dwData 提供指向对象的指针。如果有更多数据要提供给回调,则使用辅助结构将所有数据放在一起并将指针传递给该结构。

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

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

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

编辑:使用辅助结构:

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
  Class theClass = ((Auxiliary*)dwData)->theClass;
  RestOfData theRest = ((Auxiliary*)dwData)->theRest;

  theClass->callback(hMonitor,hdcMonitor,lprcMonitor, theRest);
  return true;
}

bool Class::callback(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, RestOfData* theRest)
{
  // use theRest
  classVar.appendData("callback");
  return true;
}

bool Class::f()
{
  ...
  Auxiliary theBundle(this, theRest);
  EnumDisplayMonitors(NULL,NULL,MonitorEnumProc, (LPARAM)theBundle);
  ...
}

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.

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

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

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

EDIT: With auxiliary struct:

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
  Class theClass = ((Auxiliary*)dwData)->theClass;
  RestOfData theRest = ((Auxiliary*)dwData)->theRest;

  theClass->callback(hMonitor,hdcMonitor,lprcMonitor, theRest);
  return true;
}

bool Class::callback(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, RestOfData* theRest)
{
  // use theRest
  classVar.appendData("callback");
  return true;
}

bool Class::f()
{
  ...
  Auxiliary theBundle(this, theRest);
  EnumDisplayMonitors(NULL,NULL,MonitorEnumProc, (LPARAM)theBundle);
  ...
}
秋千易 2024-12-17 01:23:48

您可以使用 dwData 参数将指针传递给您的类实例,即类似这样的内容(注意:回调不再需要是静态的 - 实际上它已经过时了):

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

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

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

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):

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

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

bool Class::f(){
  ...
  EnumDisplayMonitors(NULL,NULL,MonitorEnumProc,this);
  ...
}
箹锭⒈辈孓 2024-12-17 01:23:48

我有时会遇到这个问题,通常通过函数对象解决它,它们比静态函数更通用,您可以创建一个可以在传递给 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文