C#中通过设置钩子监视鼠标移动
2008-05-14 17:16
447 查看
这个问题来自论坛提问,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;
}
}
}
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;
}
}
}
相关文章推荐
- c#中通过设置钩子监视鼠标移动
- c#中通过设置钩子监视鼠标移动
- C#中通过设置钩子监视鼠标移动
- C#中通过设置钩子监视鼠标移动
- C#中通过设置钩子监视鼠标移动
- c#中通过设置钩子监视鼠标移动
- C#中通过设置钩子监视鼠标移动
- c#中通过设置钩子监视鼠标移动
- c#中通过设置钩子监视鼠标移动
- C#中通过设置钩子监视鼠标移动
- c#中通过设置钩子监视鼠标移动
- c#中设置钩子监视鼠标移动
- 在运行时通过鼠标拖动移动控件位置(c#)
- 使用C#模拟键盘输入、鼠标移动和点击、设置光标位置及控制应用程序的显示
- 在运行时通过鼠标拖动移动控件位置(c#)
- 在运行时通过鼠标拖动移动控件位置(c#)
- [Winform]通过钩子监控键盘操作和鼠标移动
- 使用C#钩子监视全局鼠标位置
- 4000 [Winform]通过钩子监控键盘操作和鼠标移动
- C#中,当鼠标移动到控件上,动态显示提示内容 -> 用代码操作ToolTip