在软件开发中往往需要监听鼠标或者键盘功能(全局/局部监听),.Net本身可能无法完成使命,还好C#可以直接调用WinAPI相关的Function,实现监听功能.
以下做了个Demo,通过鼠标点(单击/右键)击窗体,钩子就会捕捉鼠标事件,Code如下
HOOK.cs 文件下载:WindowsFormsApplication1
![ContractedBlock.gif](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
internal class HOOK
{
private static int hHook = 0 ;
private static Form HookControlForm;
private static HookProc MouseHookProcedure;
[DllImport( " user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int CallNextHookEx( int idHook, int nCode, IntPtr wParam, IntPtr lParam);
/// <summary>
/// GetForegroundWindow
/// 函数功能:该函数返回前台窗口(用户当前工作的窗口)。系统分配给产生前台窗口的线程一个稍高一点的优先级。
/// 函数原型:HWND GetForegroundWindow(VOID)
/// 参数:无。
/// 返回值:函数返回前台窗回的句柄。
/// 速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本:头文件:Winuser.h;库文件:user32.lib。
/// 摘要:
/// 函数功能:该函数返回前台窗口(用户当前工作的窗口)。系统分配给产生前台窗口的线程一个稍高一点的优先级。
/// 函数原型:HWND GetForegroundWindow(VOID)
/// 参数:无。
/// 返回值:函数返回前台窗回的句柄。
/// 速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本:头文件:Winuser.h;库文件:user32.lib。
///
/// </summary>
/// <returns></returns>
[DllImport( " user32.dll " )]
private static extern IntPtr GetForegroundWindow();
/// <summary>
/// GetWindowThreadProcessId这个函数来获得窗口所属进程ID和线程ID
/// </summary>
/// <param name="hwnd"></param>
/// <param name="ID"></param>
/// <returns></returns>
[DllImport( " User32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, int ID);
/// <summary>
/// 消息处理中心
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
private static int HOOKProcReturn( int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 )
{
// 鼠标右键
if (wParam.ToInt32() == 0x205 )
{
IntPtr intpr = GetForegroundWindow();
if ((intpr == HookControlForm.Handle) )
{
MessageBox.Show( " ok- 0x205 " );
}
}
else if (wParam.ToInt32() == 0x203 )
{
if (GetForegroundWindow() == HookControlForm.Handle)
{
MessageBox.Show( " ok- 0x203 " );
}
} // 鼠标左键
else if (wParam.ToInt32() == 0x201 )
{
if (GetForegroundWindow() == HookControlForm.Handle)
MessageBox.Show( " ok- 0x201 " );
}
else if (wParam.ToInt32() == 0xa1 )
{
if ((GetForegroundWindow() == HookControlForm.Handle) )
{
MessageBox.Show( " ok- 0xa1 " );
}
}
else if (wParam.ToInt32() == 0x202 )
{
// MessageBox.Show("ok- 0x202");
}
else if (wParam.ToInt32() == 0x200 )
{
// MessageBox.Show("ok-0x200");
}
}
// 监听下一次事件
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
[DllImport( " user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int SetWindowsHookEx( int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
public static bool StartHook(Form HookControl)
{
HookControlForm = HookControl;
MouseHookProcedure = new HookProc(HOOK.HOOKProcReturn);
// 监听 事件 发送消息
hHook = SetWindowsHookEx(( int )HookType.WH_MOUSE , MouseHookProcedure, IntPtr.Zero, GetWindowThreadProcessId(HookControlForm.Handle, 0 ));
if (hHook == 0 )
{
return false ;
}
return true ;
}
public static bool StopHook()
{
return UnhookWindowsHookEx(hHook);
}
[DllImport( " user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern bool UnhookWindowsHookEx( int idHook);
public delegate int HookProc( int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public HOOK.POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
/// <summary>
/// 钩子类型舰艇鼠标键盘等事件
/// </summary>
private enum HookType : int
{
WH_JOURNALRECORD = 0 ,
WH_JOURNALPLAYBACK = 1 ,
WH_KEYBOARD = 2 ,
WH_GETMESSAGE = 3 ,
WH_CALLWNDPROC = 4 ,
WH_CBT = 5 ,
WH_SYSMSGFILTER = 6 ,
WH_MOUSE = 7 , // 局部 线程级别
WH_HARDWARE = 8 ,
WH_DEBUG = 9 ,
WH_SHELL = 10 ,
WH_FOREGROUNDIDLE = 11 ,
WH_CALLWNDPROCRET = 12 ,
WH_KEYBOARD_LL = 13 ,
WH_MOUSE_LL = 14 // 全局
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
internal class HOOK
{
private static int hHook = 0 ;
private static Form HookControlForm;
private static HookProc MouseHookProcedure;
[DllImport( " user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int CallNextHookEx( int idHook, int nCode, IntPtr wParam, IntPtr lParam);
/// <summary>
/// GetForegroundWindow
/// 函数功能:该函数返回前台窗口(用户当前工作的窗口)。系统分配给产生前台窗口的线程一个稍高一点的优先级。
/// 函数原型:HWND GetForegroundWindow(VOID)
/// 参数:无。
/// 返回值:函数返回前台窗回的句柄。
/// 速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本:头文件:Winuser.h;库文件:user32.lib。
/// 摘要:
/// 函数功能:该函数返回前台窗口(用户当前工作的窗口)。系统分配给产生前台窗口的线程一个稍高一点的优先级。
/// 函数原型:HWND GetForegroundWindow(VOID)
/// 参数:无。
/// 返回值:函数返回前台窗回的句柄。
/// 速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本:头文件:Winuser.h;库文件:user32.lib。
///
/// </summary>
/// <returns></returns>
[DllImport( " user32.dll " )]
private static extern IntPtr GetForegroundWindow();
/// <summary>
/// GetWindowThreadProcessId这个函数来获得窗口所属进程ID和线程ID
/// </summary>
/// <param name="hwnd"></param>
/// <param name="ID"></param>
/// <returns></returns>
[DllImport( " User32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, int ID);
/// <summary>
/// 消息处理中心
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
private static int HOOKProcReturn( int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 )
{
// 鼠标右键
if (wParam.ToInt32() == 0x205 )
{
IntPtr intpr = GetForegroundWindow();
if ((intpr == HookControlForm.Handle) )
{
MessageBox.Show( " ok- 0x205 " );
}
}
else if (wParam.ToInt32() == 0x203 )
{
if (GetForegroundWindow() == HookControlForm.Handle)
{
MessageBox.Show( " ok- 0x203 " );
}
} // 鼠标左键
else if (wParam.ToInt32() == 0x201 )
{
if (GetForegroundWindow() == HookControlForm.Handle)
MessageBox.Show( " ok- 0x201 " );
}
else if (wParam.ToInt32() == 0xa1 )
{
if ((GetForegroundWindow() == HookControlForm.Handle) )
{
MessageBox.Show( " ok- 0xa1 " );
}
}
else if (wParam.ToInt32() == 0x202 )
{
// MessageBox.Show("ok- 0x202");
}
else if (wParam.ToInt32() == 0x200 )
{
// MessageBox.Show("ok-0x200");
}
}
// 监听下一次事件
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
[DllImport( " user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int SetWindowsHookEx( int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
public static bool StartHook(Form HookControl)
{
HookControlForm = HookControl;
MouseHookProcedure = new HookProc(HOOK.HOOKProcReturn);
// 监听 事件 发送消息
hHook = SetWindowsHookEx(( int )HookType.WH_MOUSE , MouseHookProcedure, IntPtr.Zero, GetWindowThreadProcessId(HookControlForm.Handle, 0 ));
if (hHook == 0 )
{
return false ;
}
return true ;
}
public static bool StopHook()
{
return UnhookWindowsHookEx(hHook);
}
[DllImport( " user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern bool UnhookWindowsHookEx( int idHook);
public delegate int HookProc( int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public HOOK.POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
/// <summary>
/// 钩子类型舰艇鼠标键盘等事件
/// </summary>
private enum HookType : int
{
WH_JOURNALRECORD = 0 ,
WH_JOURNALPLAYBACK = 1 ,
WH_KEYBOARD = 2 ,
WH_GETMESSAGE = 3 ,
WH_CALLWNDPROC = 4 ,
WH_CBT = 5 ,
WH_SYSMSGFILTER = 6 ,
WH_MOUSE = 7 , // 局部 线程级别
WH_HARDWARE = 8 ,
WH_DEBUG = 9 ,
WH_SHELL = 10 ,
WH_FOREGROUNDIDLE = 11 ,
WH_CALLWNDPROCRET = 12 ,
WH_KEYBOARD_LL = 13 ,
WH_MOUSE_LL = 14 // 全局
}
}
}
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 开始监听
HOOK.StartHook( this );
}
private void Form1_FormClosing( object sender, FormClosingEventArgs e)
{ // 结束监听
HOOK.StopHook();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 开始监听
HOOK.StartHook( this );
}
private void Form1_FormClosing( object sender, FormClosingEventArgs e)
{ // 结束监听
HOOK.StopHook();
}
}
}