正在做一个鼠标绘图程序,在一个class中需要调用鼠标响应函数setMouseCallback,
由于setMouseCallback中的mousecallback不支持调用non-static function,遇到了很多问题,现总结解决方法。
myclass.h中
class MyClass
{
private:
<span style="white-space:pre"> </span>void on_Mouse(int event, int x, int y);
static void onMouse(int event, int x, int y, int, void* userdata);
}
myclass.cpp中
void MyClass::onMouse(int event, int x, int y, int, void* userdata)
{
// Check for null pointer in userdata and handle the error
MyClass* temp = reinterpret_cast<MyClass*>(userdata);
temp->on_Mouse(event, x, y);
}
void MyClass::on_Mouse(int event, int x, int y)
{
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
//your code here
break;
case CV_EVENT_MOUSEMOVE:
//your code here
break;
case CV_EVENT_LBUTTONUP:
//your code here
break;
}
}