这个问题来自论坛提问
咨询如何通过设置钩子监视鼠标的移动
C#的大致代码如下
using System;
using SystemWindowsForms;
using SystemRuntimeInteropServices;
namespace WindowsApplication
{
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
private void Form_Load(object sender EventArgs e)
{
WinHook hook = new WinHook();
hookonMouseChange += new EventHandler(hook_onMouseChange);
hookSetHook();
}
void hook_onMouseChange(object sender EventArgs e)
{
thisText = CursorPositionToString();
}
}
public class WinHook
{
[DllImport(kernel)]
public static extern int GetCurrentThreadId();
[DllImport(userCharSet = CharSetAuto CallingConvention =
CallingConventionStdCall)]
public static extern int SetWindowsHookEx(
HookType idHook
HOOKPROC lpfn
int hmod
int dwThreadId);
public enum HookType
{
WH_GETMESSAGE =
}
public delegate int HOOKPROC(int nCode int wParam int lParam);
public event SystemEventHandler onMouseChange;
public void SetHook()
{
SetWindowsHookEx(HookTypeWH_GETMESSAGE
new HOOKPROC(thisMyKeyboardProc)
GetCurrentThreadId());
}
public int MyKeyboardProc(int nCode int wParam int lParam)
{
if (onMouseChange != null)
{
onMouseChange(null null);
}
return ;
}
}
}