使用MouseProc回调函数作为类成员函数 [英] Using MouseProc callback function as a class member function
问题描述
您好!是否可以使用MouseProc回调函数作为类成员函数?
// 类声明
// ...
private :
LRESULT CALLBACK MouseWndProc( int nCode,WPARAM wParam,LPARAM lParam) ;
// ...
当我尝试将MouseWndProc函数指针作为函数SetWindowsHookEx的第二个参数传递时:
SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseWndProc, // ...
这里我收到错误消息:必须致电会员功能或其地址
[edit]
我更改了代码如下:
// < span class =code-comment>声明
class MyClass
{
public :
void MyClass();
void ~MyClass();
静态 LRESULT CALLBACK MouseWndProc( int nCode,WPARAM wParam,LPARAM lParam);
private :
int x;
int y;
};
// 和实现:
LRESULT CALLBACK MyClass :: MouseWndProc( int nCode,WPARAM wParam,LPARAM lParam)
{
switch ( wParam)
{
case WM_MOUSEMOVE:
{
x = pMouseStruct-> pt.x;
y = pMouseStruct-> pt.y;
break ;
}
}
return CallNextHookEx(hhk,nCode,wParam,lParam);
}
当我尝试编译此代码时,就行:
< pre lang =c ++> x = pMouseStruct-> pt.x;
y = pMouseStruct-> pt.y;
我收到错误消息:
成员MyClass :: x不能在没有对象的情况下使用
成员MyClass :: y不能没有对象
如何解决这个问题?
[/ edit]
成员函数必须是静态的才能以这种方式使用。
Hello! Is it possible to use MouseProc callback function as a class member function?
//class declaration
//...
private:
LRESULT CALLBACK MouseWndProc(int nCode, WPARAM wParam, LPARAM lParam);
//...
And when I try to pass a MouseWndProc function pointer as the second parameter of the function SetWindowsHookEx:
SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseWndProc, //...
here I get an error: member function must be called or its address taken
[edit]
I changed the code as follows:
//declaration
class MyClass
{
public:
void MyClass();
void ~MyClass();
static LRESULT CALLBACK MouseWndProc(int nCode, WPARAM wParam, LPARAM lParam);
private:
int x;
int y;
};
//and implementation:
LRESULT CALLBACK MyClass::MouseWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
switch(wParam)
{
case WM_MOUSEMOVE:
{
x = pMouseStruct->pt.x;
y = pMouseStruct->pt.y;
break;
}
}
return CallNextHookEx(hhk, nCode, wParam, lParam);
}
And when I try to compile this code, on this lines:
x = pMouseStruct->pt.x;
y = pMouseStruct->pt.y;
I have got an error:
Member MyClass::x cannot be used without an object
Member MyClass::y cannot be used without an object
How to fix this?
[/edit]
The member function must be static to be used in this way.
这篇关于使用MouseProc回调函数作为类成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!