有没有办法挂钩鼠标事件在Windows形成特定的按钮 [英] Is there a way to hook mouse events for a specific button in a windows form

查看:134
本文介绍了有没有办法挂钩鼠标事件在Windows形成特定的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要挂钩一个特定的窗口内从一个特定的按钮WM_MOUSEDOWN和WM_MOUSEUP事件。我在想,SetWindowsHookEx函数将钩我想要的消息。和FindWindowEx帮我看看,我想从捕获这些事件的窗口句柄。

I want to hook the WM_MOUSEDOWN and WM_MOUSEUP events from a specific button inside a specific window. I am thinking that SetWindowsHookEx will hook the messages I want. and FindWindowEx will help me find the window handle that I want to trap these events from.

我只是不知道如何让它给我特定窗口中的事件处理。或如何确定什么处理消息是应该去。

I just don't know how to make it give me the Events from specific window handles. Or how to Determine what handle the message is supposed to be going to.

还有其他人用丰富的经验在这一点,我将不胜感激一些好

Anyone else with experience in this I would greatly appreciate some good

修改

另外的代码在C#中的间谍++工具或ManagedSpy中的工作副本或类似的东西。我想学习导航窗口句柄从这些层次结构和挂钩窗口的事件。

Alternatively the code to a Spy++ tool in C# or a working copy of ManagedSpy or something similar. I want to learn to navigate the Window Handle Hierarchy and hook windows events from those.

推荐答案

您不能进行的 SetWindowsHookEx函数只给你thew事件从单一窗口处理,但你可以自己进行过滤。如果您使用的是 WH_CALLWNDPROC WH_CALLWNDPROCRET (你需要使用获得鼠标消息您有兴趣) ,的 lParam的价值http://msdn.microsoft.com/en-us/library/windows/desktop/ms644975%28v=vs.85 %29.aspx相对=nofollow> CallWndProc 和的 CallWndRetProc 是包含控制处理消息的窗口句柄的结构。

You can't make SetWindowsHookEx only give you thew events from a single window handle, but you can filter it yourself. If you are using the WH_CALLWNDPROC or WH_CALLWNDPROCRET (which you need to use to get the mouse messages you are interested in), the lParam value of CallWndProc and CallWndRetProc are a structure that contain the window handle of the control processing the message.

因此,在你的和SetWindowsHookEx 回电话,你只需要检查该消息是为您筛选的窗口

So in your SetWindowsHookEx call back you only need to check that the message is for the window you are filtering.

例如:

static HWND s_hWndButton;
.....
SetWindowsHookEx(WH_CALLWNDPROC, ButtonHookProc, ....);
.....
LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0) {
        CWPSTRUCT* cp = (CWPSTRUCT*)lParam;
        if (cp->hWnd == s_hWndButton) {
            if (cp->Msg == WM_MOUSEUP || cp->Msg == WM_MOUSEDOWN) {
                // Your logic goes here
            }
        }
    }

    Return CallNextHookEx(NULL, nCode, wParam, lParam);
}



几乎是同样的逻辑也适用于 WH_CALLWNDPROCRET

这篇关于有没有办法挂钩鼠标事件在Windows形成特定的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆