C# 鼠标钩子能否获取到鼠标的移动?
鼠标不在窗体内窗体不是激活的每当鼠标移动时,就执行代码。现在的问题就是鼠标钩子怎么实现捕获到鼠标移动?回答神马MouseMove事件的人才些麻烦你们去回答其他问题吧!...
鼠标不在窗体内
窗体不是激活的
每当鼠标移动时,就执行代码。
现在的问题就是 鼠标钩子怎么实现捕获到鼠标移动?
回答神马MouseMove事件的人才些
麻烦你们去回答其他问题吧! 展开
窗体不是激活的
每当鼠标移动时,就执行代码。
现在的问题就是 鼠标钩子怎么实现捕获到鼠标移动?
回答神马MouseMove事件的人才些
麻烦你们去回答其他问题吧! 展开
展开全部
如果还有其他的解决方案,千万不要用钩子,特别是挂钩鼠标的移动,如果你的代码过多,可能让电脑运行的相当的慢,如果确实需要,可以密我,我有一段C#挂钩全局事件的代码。
private int WinWordHookProc(int nCode,int wParam,int lParam)
{
return Win32Native.CallNextHookEx(hWndProcHook, nCode, wParam, lParam);
}
private int SetWordHook()
{
int ProcessId = System.Diagnostics.Process.GetCurrentProcess().Threads[0].Id;
hookProc = new Win32Native.HookWndProc(WinWordHookProc);
int hHook = Win32Native.SetWindowsHookEx(Win32Native.HookType.WH_GETMESSAGE, hookProc, IntPtr.Zero, ProcessId);
if (hHook == 0)
{
int nErrorCode = Win32Native.GetLastError();
MessageBox.Show(nErrorCode.ToString());
}
return hHook;
}
static class Win32Native
{
public enum HookType
{
WH_CALLWNDPROC = 4,
WH_CALLWNDPROCRET = 12,
WH_CBT = 5,
WH_MOUSE = 7,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_MSGFILTER = -1,
WH_SHELL = 10,
WH_DEBUG = 9,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class CWPSTRUCT
{
public int lParam;
public int wParam;
public int message;
public IntPtr hWnd;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class POINT
{
public int X;
public int Y;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class MSG
{
public IntPtr hWnd;
public int message;
public int wParam;
public int lParam;
public int time;
public POINT pt;
}
#region HookWin32API
public delegate int HookWndProc(int nCode, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(HookType idHook, HookWndProc hookpfn, IntPtr hInstance, int dwThreadId);
[DllImport("user32.dll")]
public static extern int CallNextHookEx(int hHook, int nCode, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(int hHook);
#endregion
}
private int WinWordHookProc(int nCode,int wParam,int lParam)
{
return Win32Native.CallNextHookEx(hWndProcHook, nCode, wParam, lParam);
}
private int SetWordHook()
{
int ProcessId = System.Diagnostics.Process.GetCurrentProcess().Threads[0].Id;
hookProc = new Win32Native.HookWndProc(WinWordHookProc);
int hHook = Win32Native.SetWindowsHookEx(Win32Native.HookType.WH_GETMESSAGE, hookProc, IntPtr.Zero, ProcessId);
if (hHook == 0)
{
int nErrorCode = Win32Native.GetLastError();
MessageBox.Show(nErrorCode.ToString());
}
return hHook;
}
static class Win32Native
{
public enum HookType
{
WH_CALLWNDPROC = 4,
WH_CALLWNDPROCRET = 12,
WH_CBT = 5,
WH_MOUSE = 7,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_MSGFILTER = -1,
WH_SHELL = 10,
WH_DEBUG = 9,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class CWPSTRUCT
{
public int lParam;
public int wParam;
public int message;
public IntPtr hWnd;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class POINT
{
public int X;
public int Y;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class MSG
{
public IntPtr hWnd;
public int message;
public int wParam;
public int lParam;
public int time;
public POINT pt;
}
#region HookWin32API
public delegate int HookWndProc(int nCode, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(HookType idHook, HookWndProc hookpfn, IntPtr hInstance, int dwThreadId);
[DllImport("user32.dll")]
public static extern int CallNextHookEx(int hHook, int nCode, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(int hHook);
#endregion
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询