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



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

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

唯一的起点是这个老问题,这使我得出以下几点:

// 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("%Sn", 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("%Sn", 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 函数

事件常量

相关内容

  • 没有找到相关文章

最新更新