gpt4 book ai didi

c++ - 带有自定义数据的 SetWinEventHook

转载 作者:太空狗 更新时间:2023-10-29 21:37:36 25 4
gpt4 key购买 nike

我正在使用 SetWinEventHook Hook 来检测编辑控件的选择。在事件钩子(Hook)回调函数中,有没有办法指定自定义数据?在我的例子中,自定义数据将是 VKeyboard 实例。

请查看代码以更好地描述我想要实现的目标。

class VKeyboard
{
public:
static void CALLBACK winEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
// Somehow access VKeyboard instance?
VKeyboard* keyboard = ??;

IAccessible* pAcc = NULL;
VARIANT varChild;
HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild);

if ((hr == S_OK) && (pAcc != NULL))
{
VARIANT varRole;
hr = pAcc->get_accRole(varChild, &varRole);

// if user selects a edit control: show window
if ((hr == S_OK) && (varRole.vt == VT_I4) && (varRole.lVal == ROLE_SYSTEM_TEXT))
ShowWindow(keyboard->mainHwnd, SW_SHOW);
else ShowWindow(keyboard->mainHwnd, SW_HIDE);

pAcc->Release();
}
}

VKeyboard()
{
SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS, NULL, (WINEVENTPROC)&winEventProc, 0, 0, WINEVENT_SKIPOWNPROCESS);


SetWindowLongPtr (mainHwnd, GWLP_USERDATA, (LONG_PTR)this);
}

HWND mainHwnd;
}

最佳答案

由于您的 Hook 回调是作为 Out of Context hook 实现的, 它总是在调用 SetWinEventHook() 的线程的上下文中调用,即使在 Hook 其他进程的事件时也是如此。因此,您可以使用 Thread Local Storage ,甚至只是一个全局变量,来存储您的 VKeyboard 对象指针。前提是您只需要一个 VKeyboard 实例。

class VKeyboard
{
private:
HWINEVENTHOOK hHook;
static VKeyboard *pKeyboard;

static void CALLBACK winEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
IAccessible* pAcc = NULL;
VARIANT varChild;
HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild);

if ((hr == S_OK) && (pAcc != NULL))
{
VARIANT varRole;
hr = pAcc->get_accRole(varChild, &varRole);

// if user selects a edit control: show window
if ((hr == S_OK) && (varRole.vt == VT_I4) && (varRole.lVal == ROLE_SYSTEM_TEXT))
ShowWindow(pKeyboard->mainHwnd, SW_SHOW);
else ShowWindow(pKeyboard->mainHwnd, SW_HIDE);

pAcc->Release();
}
}

VKeyboard()
{
hHook = SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS, NULL, &winEventProc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);

mainHwnd = ...;
SetWindowLongPtr (mainHwnd, GWLP_USERDATA, (LONG_PTR)this);
pKeyboard = this;
}

~VKeyboard()
{
UnhookWinEvent(hHook);
pKeyboard = NULL;
}

HWND mainHwnd;
};

关于c++ - 带有自定义数据的 SetWinEventHook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com