首页 新闻 会员 周边 捐助

鼠标钩子过程,拦截消息WM_LBUTTONUP时进入了两次

0
悬赏园豆:80 [已解决问题] 解决于 2015-11-22 16:06

不知道是什么原因,我也没有多次加入钩子链。但检测到进入了两次,请知道的一定指教一下。

deeeeeed的主页 deeeeeed | 初学一级 | 园豆:6
提问于:2014-05-04 23:33
< >
分享
最佳答案
1

可以先使用spy++观察一下。

收获园豆:60
lucika.zh | 初学一级 |园豆:54 | 2014-05-06 09:30

和spy++确实只看到响应了一次消息,我拦截的是WM_LBUTTONUP,但是不知道这是什么原因

deeeeeed | 园豆:6 (初学一级) | 2014-05-06 20:25

@deeeeeed: 首先应该以spy++为基准。如果spy++截获的WM_LBUTTONUP消息时一次,初步应该是这个钩子本身存在问题,是否将消息多次回调?

lucika.zh | 园豆:54 (初学一级) | 2014-05-07 08:46

@lucika.zh: 首先非常感谢你的跟进回答,我把我的代码贴上来一下,不太复杂,我用的C#,在主窗体Load事件中安装钩子,然后就是钩子过程的事了,代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using System.Runtime.InteropServices;
  9 using System.Threading;
 10 
 11 namespace Show子窗体
 12 {
 13     public partial class Form1 : Form
 14     {
 15         MOUSEHOOK mouseHook = new MOUSEHOOK();
 16         MOUSEHOOK.HookHandle hookhandle = null;
 17         MOUSEHOOK.MouseHookStruct mhs = new MOUSEHOOK.MouseHookStruct();
 18        
 19         public Form1()
 20         {
 21             InitializeComponent();
 22         }
 23         private void Form1_Load(object sender, EventArgs e)
 24         {
 25             hookhandle = new MOUSEHOOK.HookHandle(HookProc);
 26             if (true == mouseHook.InstallHook(hookhandle))
 27             {
 28                 Console.WriteLine("安装钩子成功");
 29             }
 30         }
 31 
 32         
 33         //钩子过程
 34         int HookProc(int nCode, int wParam, IntPtr lParam)
 35         {
 36           Marshal.PtrToStructure(lParam, mhs);
 37             switch (wParam)
 38             {
 39                 case MOUSEHOOK.WM_LBUTTONUP:
 40                     Console.WriteLine("进入几次呢?");
 41                     break;
 42                 default :
 43                     break;
 44             }
 45 
 46             return MOUSEHOOK.CallNextHookEx(0, nCode, wParam, lParam);
 47             
 48         }
 49 
 50 
 51 
 52     }
 53     //钩子类
 54     public class MOUSEHOOK
 55     {
 56         [DllImport("user32.dll")]
 57         private static extern int SetWindowsHookEx(int idHook, HookHandle HookProc, IntPtr hMod, int dwThreadId);
 58         //取消钩子  
 59         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
 60         private static extern bool UnhookWindowsHookEx(int idHook);
 61         //调用下一个钩子  
 62         [DllImport("user32.dll")]
 63         public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
 64         [StructLayout(LayoutKind.Sequential)]
 65         public class POINT
 66         {
 67             public int x;
 68             public int y;
 69         }
 70         [StructLayout(LayoutKind.Sequential)]
 71         public class MouseHookStruct
 72         {
 73             public POINT pt;
 74             public int hwnd;
 75             public int wHitTestCode;
 76             public int dwExtraInfo;
 77         }
 78 
 79         public const int WH_MOUSE = 7;
 80         public const int WM_MOUSEMOVE = 0x200;
 81         public const int WM_LBUTTONDOWN = 0x201;
 82         public const int WM_LBUTTONUP = 0x202;
 83         public const int WM_NCLBUTTONDOWN = 0xA1;
 84         public const int WM_NCLBUTTONUP = 0xA2;
 85         public const int WM_NCMOUSEMOVE = 0xA0;
 86         public MouseHookStruct mhs = new MouseHookStruct();
 87         public int hhook;
 88         public delegate int HookHandle(int nCode, int wParam, IntPtr lParam);
 89 
 90         public bool InstallHook(HookHandle MouseProc)
 91         {
 92             hhook = SetWindowsHookEx(WH_MOUSE, MouseProc, IntPtr.Zero,/*Thread.CurrentThread.ManagedThreadId */AppDomain.GetCurrentThreadId());//AppDomain.GetCurrentThreadId()
 93             if (hhook != 0)
 94                 return true;
 95             return false;
 96         }
 97         public void UnInstallHook()
 98         {
 99             bool ret = false;
100             if (hhook != 0)
101                 ret = UnhookWindowsHookEx(hhook);
102             if (ret)
103                 hhook = 0;
104         }
105     }
106 }
deeeeeed | 园豆:6 (初学一级) | 2014-05-07 09:19
其他回答(1)
0

看一下两个参数有什么不同,之前做过一个键盘钩子的demo.

http://www.cnblogs.com/zhangliming/p/3477109.html

收获园豆:20
平常心队长 | 园豆:1113 (小虾三级) | 2014-05-06 15:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册