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#,鼠标,钩子
0
投稿
猜你喜欢
Java判断所给年份是平年还是闰年
2023-10-21 17:48:59
Springboot引入hibernate配置自动建表并进行增删改查操作
2021-08-30 06:17:02
Spring Boot加密配置文件特殊内容的示例代码详解
2023-09-18 08:47:24
Android编程创建桌面快捷方式的常用方法小结【2种方法】
2023-12-07 00:50:35
Android activity堆栈及管理实例详解
2022-07-28 01:17:14
高斯混合模型与EM算法图文详解
2022-10-02 12:05:02
java8 统计字符串字母个数的几种方法总结(推荐)
2023-09-25 17:37:48
Jmeter 中 CSV 如何参数化测试数据并实现自动断言示例详解
2023-10-17 15:37:42
利用C#9.0新语法如何提升if语句美感
2021-06-15 12:48:54
Mybatis-Plus sum聚合函数及按日期查询并求和的方式详解
2022-07-09 12:58:07
利用ThreadLocal实现一个上下文管理组件
2022-04-16 03:42:23
如何在C#项目中链接一个文件夹下的所有文件详解
2022-09-08 08:48:22
springboot启动脚本start.sh和停止脚本 stop.sh的详细教程
2022-10-11 08:28:26
Mybatis-Plus的SQL语句组拼原理说明
2021-07-03 20:39:22
Unity3D UGUI实现缩放循环拖动卡牌展示效果
2022-02-19 10:37:24
JAVA面试题 从源码角度分析StringBuffer和StringBuilder的区别
2023-07-24 07:29:07
C# 异步多线程入门到精通之ThreadPool篇
2022-03-23 01:51:00
Android仿网易新闻图片详情下滑隐藏效果示例代码
2023-05-31 02:14:03
Java编程调用微信接口实现图文信息推送功能
2023-11-25 07:20:47
C# 7.2中结构体性能问题的解决方案
2022-08-12 23:04:26