使用 Windows API 创建鼠标钩子时出现的问题

发布于 2024-10-17 08:09:18 字数 2393 浏览 3 评论 0原文

我正在尝试使用 Windows API 创建一个鼠标钩子。目前,我有以下代码,但我正在努力使其工作。也许一些示例代码可以帮助我更好地理解这些概念。

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    HWND hWindow , hEntryWnd;
    HRESULT hResult;
    ztcEvents pEvent;
    ztcField *p_click_field;
    ztcInterfaceContext *p_context_interface;
    TCHAR      className [32];
    HWND hWnd;
    if (nCode < 0) // do not process message 
        return CallNextHookEx(hook, nCode, wParam, lParam); 
    MOUSEHOOKSTRUCT *msg = (MOUSEHOOKSTRUCT *)lParam;

    switch (nCode) 
    { 
    case HC_ACTION: 
        {
            hWnd                =  msg->hwnd;
            p_click_field       = pEvent.GetMatchField(hWnd);
            p_context_interface = pEvent.getMarkInterface();
            if(p_click_field == NULL || p_context_interface == NULL)
            {
                break;
            }
            GetClassName(p_click_field->Widget ()->WindowHandle() , className , sizeof (className ) - 1 );

            if(p_click_field->Widget()->IsKindOf(RUNTIME_CLASS(ztcOleWdgt)))
            {
                switch(wParam)
                {
                    case WM_LBUTTONDOWN:

                        hWindow = p_click_field->Widget()->Parent()->WindowHandle();
                        hEntryWnd = p_click_field->Widget()->WindowHandle();

                        ::PostMessage(hWindow , OLE_NOTIFY , (WPARAM)hEntryWnd , OLE_LCLICK);
                        /*pEvent.SetVariables(p_click_field , evClick ,etFormField , true , p_context_interface);
                        SetEvent(hEventWnd);*/
                        fprintf(trace_event , "buttonClick\t%x\n" , msg->hwnd );
                        fflush(trace_event);
                        break;
                    case WM_RBUTTONDOWN:
                        fprintf(trace_event , "RightClick\n");
                        fflush(trace_event);
                        break;
                    case WM_LBUTTONDBLCLK:
                        fprintf(trace_event , "ButtonDoubleClick\n");
                        fflush(trace_event);
                        break;
                }
            }
        }

        break;
      default:
        break;
    }
    return CallNextHookEx(hook, nCode, wParam, lParam); 
}    

单击鼠标左键时,上面的代码会进入案例 WM_LBUTTONDOWN 超过 30 次。我的代码有问题吗?如果是这样,我该如何修复它?

I am trying to create a mouse hook using the Windows API. Currently, I have the following code, but I am struggling to make it work. Perhaps some sample code would help me to understand the concepts better.

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    HWND hWindow , hEntryWnd;
    HRESULT hResult;
    ztcEvents pEvent;
    ztcField *p_click_field;
    ztcInterfaceContext *p_context_interface;
    TCHAR      className [32];
    HWND hWnd;
    if (nCode < 0) // do not process message 
        return CallNextHookEx(hook, nCode, wParam, lParam); 
    MOUSEHOOKSTRUCT *msg = (MOUSEHOOKSTRUCT *)lParam;

    switch (nCode) 
    { 
    case HC_ACTION: 
        {
            hWnd                =  msg->hwnd;
            p_click_field       = pEvent.GetMatchField(hWnd);
            p_context_interface = pEvent.getMarkInterface();
            if(p_click_field == NULL || p_context_interface == NULL)
            {
                break;
            }
            GetClassName(p_click_field->Widget ()->WindowHandle() , className , sizeof (className ) - 1 );

            if(p_click_field->Widget()->IsKindOf(RUNTIME_CLASS(ztcOleWdgt)))
            {
                switch(wParam)
                {
                    case WM_LBUTTONDOWN:

                        hWindow = p_click_field->Widget()->Parent()->WindowHandle();
                        hEntryWnd = p_click_field->Widget()->WindowHandle();

                        ::PostMessage(hWindow , OLE_NOTIFY , (WPARAM)hEntryWnd , OLE_LCLICK);
                        /*pEvent.SetVariables(p_click_field , evClick ,etFormField , true , p_context_interface);
                        SetEvent(hEventWnd);*/
                        fprintf(trace_event , "buttonClick\t%x\n" , msg->hwnd );
                        fflush(trace_event);
                        break;
                    case WM_RBUTTONDOWN:
                        fprintf(trace_event , "RightClick\n");
                        fflush(trace_event);
                        break;
                    case WM_LBUTTONDBLCLK:
                        fprintf(trace_event , "ButtonDoubleClick\n");
                        fflush(trace_event);
                        break;
                }
            }
        }

        break;
      default:
        break;
    }
    return CallNextHookEx(hook, nCode, wParam, lParam); 
}    

While clicking the left mouse button, the code above went into the case WM_LBUTTONDOWN for more than 30 times. Is there something wrong with my code? If so, how can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2024-10-24 08:09:18

您点击鼠标按钮的次数是否超过 30 次?每次单击鼠标按钮时都会发送一条 WM_LBUTTONDOWN 消息,即使是双击的一部分。这是 Windows 在无法返回过去的情况下识别双击的结果。

该示例看起来值得信赖,并且是由一位相当有信誉的作者编写的:

     挂钩和 DLL

Are you clicking the mouse button more than 30 times? There will be an WM_LBUTTONDOWN message sent each the the mouse button is clicked, even if it is part of a double-click. That's a consequence of how Windows recognizes a double-click in the absence of a way to go back in time.

This sample looks trustworthy, and was written by a fairly reputable author:

     Hooks and DLLs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文