类方法作为winAPI回调 [英] Class method as winAPI callback

查看:104
本文介绍了类方法作为winAPI回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将winAPI消息回调函数设置为类的方法是可行的。如果是,最好如何实施?我不知道是否可能。

Is it feasible to set the winAPI message callback function as a method of a class. If so, how would this be best implemented? I wonder if it is even possible.

对于简短的问题,抱歉,您将能够提供有用的反馈。

Sorry for the short question, hopefully you will be able to provide useful responses.

提前感谢:)

推荐答案

不能使用非 - static C回调的成员函数。

You cannot use non-static member functions for C callbacks.

然而,通常C回调都有一个路由到回调的用户数据指针。这可以通过 static 成员函数的帮助来完成你想要的操作:

However, usually C callbacks have a user data pointer that's routed to the callback. This can be explored to do what you want with the help of a static member functions:

// Beware, brain-compiled code ahead!

typedef void (*callback)(int blah, void* user_data);

void some_func(callback cb, void* user_data);

class my_class {
public:
  // ...
  void call_some_func()
  {
     some_func(&callback_,this);
  }
private:
  void callback(int blah)
  {
    std::cout << blah << '\n';
  }
  static void callback_(int blah, void* user_data)
  {
    my_class* that = static_cast<my_class*>(user_data);
    that->callback(blah);
  }
};

这篇关于类方法作为winAPI回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆