我正在创建一个键盘钩子,其中KeyboardProc是类CWidget的静态成员.
class CWidget { static LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam ); };
我想在CWidget :: KeyboardProc中调用CWidget的非静态成员.
最好的方法是什么?
KeyboardProc没有任何32位DWORD,我可以传递'this'指针.
鉴于您可能只想一次安装一个键盘钩子,只需在您的类中添加一个静态pThis成员:
// Widget.h class CWidget { static HHOOK m_hHook; static CWidget *m_pThis; public: /* NOT static */ bool SetKeyboardHook() { m_pThis = this; m_hHook = ::SetWindowsHookEx(WH_KEYBOARD, StaticKeyboardProc, /* etc */); } // Trampoline static LRESULT CALLBACK StaticKeyboardProc(int code, WPARAM wParam, LPARAM lParam) { ASSERT(m_pThis != NULL); m_pThis->KeyboardProc(code, wParam, lParam); } LRESULT KeyboardProc(int code, WPARAM wParam, LPARAM lParam); /* etc. */ };