当鼠标单击可编辑文本框时如何在 C++ 中显示通知

How to show notification in c++ when mouse click an editable textbox is focused

几天来我一直在寻找这个问题的解决方案,但找不到太多信息。

我正在寻找的是一个小程序,当鼠标点击 windows 表单或浏览器页面中的文本框等文本可编辑字段时显示通知。

唯一的起点是这个old question from so,它让我得出以下结论:

// Global variable.
HWINEVENTHOOK g_hook;

// Initializes COM and sets up the event hook.
//
void InitializeMSAA()
{
    CoInitialize(NULL);
    g_hook = SetWinEventHook(
        EVENT_SYSTEM_MENUSTART, EVENT_SYSTEM_MENUEND,  // Range of events (4 to 5).
        NULL,                                          // Handle to DLL.
        HandleWinEvent,                                // The callback.
        0, 0,              // Process and thread IDs of interest (0 = all)
        WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); // Flags.
}

// Unhooks the event and shuts down COM.
//
void ShutdownMSAA()
{
    UnhookWinEvent(g_hook);
    CoUninitialize();
}

// Callback function that handles events.
//
void CALLBACK HandleWinEvent(HWINEVENTHOOK hook, 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))
    {
        BSTR bstrName;
        pAcc->get_accName(varChild, &bstrName);
        if (event == EVENT_SYSTEM_MENUSTART) 
        {
            printf("Begin: ");
        }
        else if (event == EVENT_SYSTEM_MENUEND)
        {
            printf("End:   ");
        }
        printf("%S\n", bstrName);
        SysFreeString(bstrName);
        pAcc->Release();
    }
}

您是否知道如何修改它以便在文本框获得焦点时得到通知?任何解释将不胜感激。

您提供的代码与菜单打开和关闭事件 EVENT_SYSTEM_MENUSTARTEVENT_SYSTEM_MENUEND 挂钩。

您应该尝试使用 EVENT_OBJECT_FOCUS(也许 EVENT_OBJECT_SELECT 而不是 SetWinEventHookHandleWinEvent

void CALLBACK HandleWinEvent(HWINEVENTHOOK hook, 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);  
    BSTR bstrName;
    pAcc->get_accName(varChild, &bstrName);

    if (event == EVENT_OBJECT_FOCUS || event == EVENT_OBJECT_SELECTION) 
    {
        printf("%S\n", bstrName);
    }       
}

void InitializeMSAA()
{
    CoInitialize(NULL);
    g_hook = SetWinEventHook(
        EVENT_OBJECT_FOCUS, EVENT_OBJECT_SELECTION,
        NULL,                                          // Handle to DLL.
        HandleWinEvent,                                // The callback.
        0, 0,              // Process and thread IDs of interest (0 = all)
        WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS); // Flags.
}

查看可用事件列表的文档:

SetWinEventHook function

Event Constants