c#中通过设置钩子监视鼠标移动
[ Thursday, April 17, 2008 ]
这个问题来自论坛提问,C#的大致代码如下
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Win32Hook hook = new Win32Hook();
hook.onMouseChange += new EventHandler(hook_onMouseChange);
hook.SetHook();
}
void hook_onMouseChange(object sender, EventArgs e)
{
this.Text = Cursor.Position.ToString();
}
}
public class Win32Hook
{
[DllImport("kernel32")]
public static extern int GetCurrentThreadId();
[DllImport("user32",CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(
HookType idHook,
HOOKPROC lpfn,
int hmod,
int dwThreadId);
public enum HookType
{
WH_GETMESSAGE = 3
}
public delegate int HOOKPROC(int nCode, int wParam, int lParam);
public event System.EventHandler onMouseChange;
public void SetHook()
{
SetWindowsHookEx(HookType.WH_GETMESSAGE,
new HOOKPROC(this.MyKeyboardProc),
0,
GetCurrentThreadId());
}
public int MyKeyboardProc(int nCode, int wParam, int lParam)
{
if (onMouseChange != null)
{
onMouseChange(null, null);
}
return 0;
}
}
}
发布:babycrazy | 分类:IT新闻 | 评论:0 | 引用:0 | 浏览:


为何C#属性名称的首字母要大写 (2008-4-16 21:18:16)
C# 反射的用法 (2008-4-16 21:16:50)
C# 截取路径中的文件名 (2008-4-11 12:48:21)
C#扫描指定网段的计算机名称及MAC地址 (2008-3-11 12:7:39)
[C#]获取某年指定周的开始日期和结束日期的通用方法 (2008-1-15 12:4:43)