使用hook 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;
}
}
}
在MouseDown事件中判断e.X >= rectangle[i].X && e.Y >= rectangle[i].Y && e.Y <= rectangle[i].Bottom && e.X <= rectangle[i].Right
就知道鼠标在那个区域了。
能具体说下“Rectangle[] 定义所有区域”吗
我的区域是用GDI动态绘制的,又该定义区域和确定鼠标所指向的位置呢
2 在各个区域,添加对应控件的鼠标事件(Mounsedown,up,over)
int y = Cursor.Y;
...
然后在Lable中添加事件!