NativeMethods.cs:

using System;
using System.Runtime.InteropServices; namespace GlobalHook
{
internal static class NativeMethods
{
#region window message
internal const int WM_MOUSEMOVE = 0x200; internal const int WM_LBUTTONDOWN = 0x201; internal const int WM_RBUTTONDOWN = 0x204; internal const int WM_MBUTTONDOWN = 0x207; internal const int WM_LBUTTONUP = 0x202; internal const int WM_RBUTTONUP = 0x205; internal const int WM_MBUTTONUP = 0x208; internal const int WM_LBUTTONDBLCLK = 0x203; internal const int WM_RBUTTONDBLCLK = 0x206; internal const int WM_MBUTTONDBLCLK = 0x209; internal const int WM_MOUSEWHEEL = 0x020A;
#endregion #region param idHook
internal const int WH_CALLWNDPROC = ; internal const int WH_CALLWNDPROCRET = ; internal const int WH_CBT = ; internal const int WH_DEBUG = ; internal const int WH_FOREGROUNDIDLE = ; internal const int WH_GETMESSAGE = ; internal const int WH_JOURNALPLAYBACK = ; internal const int WH_JOURNALRECORD = ; internal const int WH_KEYBOARD = ; internal const int WH_KEYBOARD_LL = ; internal const int WH_MOUSE = ; internal const int WH_MOUSE_LL = ; internal const int WH_MSGFILTER = -; internal const int WH_SHELL = ; internal const int WH_SYSMSGFILTER = ;
#endregion [StructLayout(LayoutKind.Sequential)]
internal class POINT
{
internal int x;
internal int y; internal POINT(int x, int y)
{
this.x = x;
this.y = y;
}
} [StructLayout(LayoutKind.Sequential)]
internal class MOUSEHOOKSTRUCT
{
internal POINT pt;
internal IntPtr hWnd;
internal int wHitTestCode;
internal int dwExtraInfo;
} [StructLayout(LayoutKind.Sequential)]
internal class MOUSEHOOKSTRUCTEX
{
internal MOUSEHOOKSTRUCT MOUSEHOOKSTRUCT;
internal uint mouseData;
} internal delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
}
}

UnsafeNativeMethods.cs:

using System;
using System.Runtime.InteropServices;
using static GlobalHook.NativeMethods; namespace GlobalHook
{
internal static class UnsafeNativeMethods
{
/// <summary>
/// https://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx /// </summary>
/// <param name="idHook"></param>
/// <param name="lpfn"></param>
/// <param name="hMod"></param>
/// <param name="dwThreadId"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, HandleRef hMod, int dwThreadId); /// <summary>
/// https://msdn.microsoft.com/en-us/library/ms644993(VS.85).aspx /// </summary>
/// <param name="hhk"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern bool UnhookWindowsHookEx(HandleRef hhk); /// <summary>
/// https://msdn.microsoft.com/en-us/library/ms644974(VS.85).aspx /// </summary>
/// <param name="hhk"></param>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr CallNextHookEx(HandleRef hhk, int nCode, IntPtr wParam, IntPtr lParam); /// <summary>
/// https://msdn.microsoft.com/en-us/library/ms683199(VS.85).aspx /// </summary>
/// <param name="lpModuleName"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr GetModuleHandle(string lpModuleName);
}
}

From1.cs:

using System;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using static GlobalHook.NativeMethods;
using static GlobalHook.SafeNativeMethods;
using static GlobalHook.UnsafeNativeMethods; namespace GlobalHook
{
public partial class Form1 : Form
{
private IntPtr hook; private HookProc mouseHookProcedure; public Form1()
{
InitializeComponent();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnhookWindowsHookEx(new HandleRef(null, hook));
} private void btnHook_Click(object sender, EventArgs e)
{
if (btnHook.Text == "hook")
{
mouseHookProcedure = LowLevelMouseProc;
hook = SetWindowsHookEx(WH_MOUSE_LL, mouseHookProcedure, new HandleRef(null, GetModuleHandle(null)), );
if (hook != IntPtr.Zero)
{
btnHook.Text = "unhook";
}
}
else
{
if (UnhookWindowsHookEx(new HandleRef(null, hook)))
{
btnHook.Text = "hook";
}
}
} private IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam)
{
MOUSEHOOKSTRUCT mouseHookStruct = Marshal.PtrToStructure<MOUSEHOOKSTRUCT>(lParam);
switch ((int)wParam)
{
case WM_MOUSEMOVE:
break;
case WM_LBUTTONDOWN:
break;
case WM_RBUTTONDOWN:
break;
case WM_MBUTTONDOWN:
break;
case WM_LBUTTONUP:
break;
case WM_RBUTTONUP:
break;
case WM_MBUTTONUP:
break;
case WM_LBUTTONDBLCLK:
break;
case WM_RBUTTONDBLCLK:
break;
case WM_MBUTTONDBLCLK:
break;
case WM_MOUSEWHEEL:
break;
default:
break;
}
return CallNextHookEx(new HandleRef(null, hook), nCode, wParam, lParam);
}
}
}

C# [WIN32] [API] Global Hook的更多相关文章

  1. Detours简介 (拦截x86机器上的任意的win32 API函数)

    Detours 当然是用detours,微软明显高腾讯一筹,同上,至今没失败过.写这种HOOK一定要再写个测试程序,不要直接HOOK你的目的程序,例如QQ,因为这样不方面更灵活的测试.说明一下:Det ...

  2. 初次认识 C# win32 api

    第一次接触win32api,刚开始的时候有点迷迷糊糊的. Windows API 就是windows应用程序接口. win api向上就是windows应用程序,向下就是windows操作系统核心. ...

  3. Serial Port Programming using Win32 API(转载)

    In this tutorial we will learn How to communicate with an external device like a microcontroller boa ...

  4. 从.NET平台调用Win32 API

    MSDN文章<Microsoft Win32 to Microsoft .NET Framework API Map> 介绍了.net 类库对win32的封装 从.NET平台调用Win32 ...

  5. 【温故Delphi】GAEA用到Win32 API目录

    Delphi是Windows平台下著名的快速应用程序开发工具,它在VCL中封装并使用了大量的Win32 API. GAEA基于VCL开发的工具类产品,在程序中使用了大量的Win32 API,将经常用到 ...

  6. 【C#】分享基于Win32 API的服务操作类(解决ManagedInstallerClass.InstallHelper不能带参数安装的问题)

    注:这里的服务是指Windows 服务. ------------------201508250915更新------------------ 刚刚得知TransactedInstaller类是支持带 ...

  7. C#中导入Win32 API函数

    C#中导入Win32 API的方法: 1.引用命名空间 using System.Net.Security; using System.Runtime.InteropServices; 2. [Dll ...

  8. MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)

    转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...

  9. C#调用Win32 api学习总结

    从.NET平台调用Win32 API Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微 ...

随机推荐

  1. 最短Hamilton路径【状压DP】

    给定一张 nn 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamilton路径的定义是从 0 到 n-1 不重不漏地经过每个点恰好一次. 输入 ...

  2. 遇到的Cocos2dx问题

    平时工作中,多多少少,总会遇到些棘手的问题,此文章会不断更新,避免下次再入坑  Cocos 官方相关下载 https://github.com/fusijie/Cocos-Resource Easin ...

  3. 蚂蚁风险大脑亮相ATEC城市峰会:为数字经济时代做好“安全守护”

    2019年1月4日,以“数字金融新原力(The New Force of Digital Finance)”为主题的蚂蚁金服ATEC城市峰会在上海隆重举行.大会聚焦金融数字化转型,分享新技术的发展趋势 ...

  4. Windows 环境下进行的jenkins持续集成

    一台服务器作为代码仓库,一条服务器做持续集成代码仓库目前常见的github.gitlab.gitee持续集成常用Jenkins 服务器的配置这边都以Windows为例进行介绍 1. 安装Jenkins ...

  5. 雷林鹏分享:jQuery EasyUI 数据网格 - 格式化列

    jQuery EasyUI 数据网格 - 格式化列 以下实例格式化在 easyui DataGrid 里的列数据,并使用自定义列的 formatter,如果价格小于 20 就将文本变为红色. 为了格式 ...

  6. 干货 | 揭秘如何增加listing多个类目节点

    流量是电商销售的必要因素,可以说,任何成功的电商平台都离不开流量.亚马逊listing优化做得好,不仅能提高产品的曝光率,还能提升转换率,而好的类目可以吸引大的流量.帮你快速爬升. 首先我们来了解一下 ...

  7. @mentions for Users with ActionText; 使用Tribute.js库

    git clone从https://github.com/chentianwei411/at-mentions-with-action-text 先fork下来,然后拷贝https连接,最后在term ...

  8. storybook构建vue组件

    最近在研究业务型组件的使用,因为在单独开发组件的时候需要调试,所以为每一个组件都编写一个webpack开发环境,然后上传上去为了其他人可以直接使用又把webpack上传上去,这样会有两个问题: 1:每 ...

  9. Vue非父子级通信

    <div id="app"> <xz-todo></xz-todo> </div> <!--1. 为每个组件定义HTML 模板 ...

  10. 修改 input / textarea placeholder 属性的颜色和字体大小

    话不多说,直接上代码: input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #666; fon ...