SetWindowsHookEx, KeyboardProc et membres non statiques
Question
Je crée un crochet pour le clavier dans lequel KeyboardProc est un membre statique d'une classe CWidget.
class CWidget
{
static LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam );
};
Je souhaite appeler les membres non statiques de CWidget à l'intérieur de CWidget :: KeyboardProc.
Quelle est la meilleure façon de le faire?
KeyboardProc n’a pas de DWORD 32 bits où je puisse passer le pointeur 'this'.
La solution
Etant donné que vous ne voulez probablement installer qu'un seul clavier à la fois, ajoutez simplement un membre pThis à votre classe:
// 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. */
};
Vous devez définir le membre statique:
// Widget.cpp
CWidget *CWidget::m_pThis = NULL;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow