C#实现的鼠标钩子
作者:hebedich 时间:2021-10-29 22:48:29
C#实现的鼠标钩子,可以获取鼠标在屏幕中的坐标,记得要以管理员权限运行才行
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace app01
{
public partial class Form1 : Form
{
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
//定义钩子句柄
public static int hHook = 0;
//定义钩子类型
public const int WH_MOUSE_LL = 14;
public HookProc MyProcedure;
//安装钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
//卸载钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//调用下一个钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (hHook == 0)
{
MyProcedure = new HookProc(this.MouseHookProc);
//这里挂节钩子
hHook = SetWindowsHookEx(WH_MOUSE_LL, MyProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
if (hHook == 0)
{
MessageBox.Show("SetWindowsHookEx Failed");
return;
}
button1.Text = "卸载钩子";
}
else
{
bool ret = UnhookWindowsHookEx(hHook);
if (ret == false)
{
MessageBox.Show("UnhookWindowsHookEx Failed");
return;
}
hHook = 0;
button1.Text = "安装钩子";
}
}
public int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
String strCaption = "x = " + MyMouseHookStruct.pt.x.ToString("d") + " y = " + MyMouseHookStruct.pt.y.ToString("d");
this.Text = strCaption;
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
}
}
演示:
标签:C#,鼠标,钩子
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java设计模式之备忘录模式实现对象状态的保存和恢复
2023-08-28 15:22:27
![](https://img.aspxhome.com/file/2023/9/58439_0s.png)
使用SpringBoot发送邮件的方法详解
2023-01-30 02:21:37
![](https://img.aspxhome.com/file/2023/7/73107_0s.webp)
IDEA中sout快捷键无效问题的解决方法
2023-11-28 21:27:02
![](https://img.aspxhome.com/file/2023/7/60817_0s.png)
Android Studio实现简易登录界面制作
2023-08-25 08:49:42
![](https://img.aspxhome.com/file/2023/8/99778_0s.jpg)
C# char类型字符转换大小写的实现代码
2021-07-27 19:21:09
Spring AOP在web应用中的使用方法实例
2023-12-24 12:20:00
![](https://img.aspxhome.com/file/2023/3/81353_0s.png)
详解Java中对象池的介绍与使用
2023-07-25 13:13:48
Java多线程开发工具之CompletableFuture的应用详解
2023-08-30 00:43:00
![](https://img.aspxhome.com/file/2023/9/57709_0s.png)
java实现简易飞机大战
2022-08-27 12:36:07
![](https://img.aspxhome.com/file/2023/7/81227_0s.jpg)
深入Android 五大布局对象的应用
2022-03-23 15:25:15
![](https://img.aspxhome.com/file/2023/7/93337_0s.jpg)
Springboot集成Elasticsearch的步骤与相关功能
2022-07-23 06:47:30
![](https://img.aspxhome.com/file/2023/8/70488_0s.png)
15个顶级Java多线程面试题(附答案)
2023-11-24 01:53:44
聊一聊Java的JVM类加载机制
2023-11-08 03:58:37
![](https://img.aspxhome.com/file/2023/5/58755_0s.png)
Java常量池知识点总结
2023-01-09 10:23:09
![](https://img.aspxhome.com/file/2023/2/66712_0s.png)
java二维数组实现推箱子小游戏
2022-08-31 10:18:40
java面向对象设计原则之里氏替换原则示例详解
2023-02-02 05:04:41
C#实现保存文件时重名自动生成新文件的方法
2022-07-02 15:01:57
Java语法基础之循环结构语句详解
2023-09-06 21:37:28
![](https://img.aspxhome.com/file/2023/1/79811_0s.jpg)
Hibernate环境搭建与配置方法(Hello world配置文件版)
2022-07-31 06:26:04
java日期时间操作工具类
2022-09-03 13:04:58