本文介绍了使用MouseProc回调函数作为类成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好!是否可以使用MouseProc回调函数作为类成员函数? // 类声明 // ... private : LRESULT CALLBACK MouseWndProc( int nCode,WPARAM wParam,LPARAM lParam) ; // ... 当我尝试将MouseWndProc函数指针作为函数SetWindowsHookEx的第二个参数传递时: SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseWndProc, // ... 这里我收到错误消息:必须致电会员功能或其地址 [edit] 我更改了代码如下: // 声明 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); } 当我尝试编译此代码时,就行: 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://declarationclass 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 objectMember MyClass::y cannot be used without an objectHow to fix this?[/edit] 解决方案 The member function must be static to be used in this way. 这篇关于使用MouseProc回调函数作为类成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 08:36