------ #### VSoft V1.0.2107.0501 - *.[新增]新增适配原生64位系统。 - *.[修复]修复将侧键作为热键时,点击两个侧键都会激活软件的BUG。 - *.[修复]修复点击主界面中设置按钮,设置按钮没在父窗体居中的BUG。 - *.[修复]修复在右键菜单中无法编辑和新增软件的BUG。 - *.[修复]修复主界面右键菜单打开的窗体没居中的BUG。
234 lines
8.5 KiB
C#
234 lines
8.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace VSoft.Prams
|
|
{
|
|
/// <summary>
|
|
/// 鼠标全局钩子
|
|
/// </summary>
|
|
public class MouseHook
|
|
{
|
|
private const int WM_MOUSEMOVE = 0x200;
|
|
private const int WM_LBUTTONDOWN = 0x201;
|
|
private const int WM_RBUTTONDOWN = 0x204;
|
|
private const int WM_MBUTTONDOWN = 0x207;
|
|
private const int WM_XBUTTONDOWN = 0x20B;
|
|
private const int WM_LBUTTONUP = 0x202;
|
|
private const int WM_RBUTTONUP = 0x205;
|
|
private const int WM_MBUTTONUP = 0x208;
|
|
private const int WM_XBUTTON1 = 0x20B;
|
|
private const int WM_XBUTTON2 = 0x20C;
|
|
private const int WM_LBUTTONDBLCLK = 0x203;
|
|
private const int WM_RBUTTONDBLCLK = 0x206;
|
|
private const int WM_MBUTTONDBLCLK = 0x209;
|
|
|
|
/// <summary>
|
|
/// 点
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public class POINT
|
|
{
|
|
public int x;
|
|
public int y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 钩子结构体
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public class MouseHookStruct
|
|
{
|
|
/// <summary>
|
|
/// Specifies a Point structure that contains the X- and Y-coordinates of the cursor, in screen coordinates.
|
|
/// </summary>
|
|
[FieldOffset(0x00)] public System.Drawing.Point Point;
|
|
|
|
/// <summary>
|
|
/// Specifies information associated with the message.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The possible values are:
|
|
/// <list type="bullet">
|
|
/// <item>
|
|
/// <description>0 - No Information</description>
|
|
/// </item>
|
|
/// <item>
|
|
/// <description>1 - X-Button1 Click</description>
|
|
/// </item>
|
|
/// <item>
|
|
/// <description>2 - X-Button2 Click</description>
|
|
/// </item>
|
|
/// <item>
|
|
/// <description>120 - Mouse Scroll Away from User</description>
|
|
/// </item>
|
|
/// <item>
|
|
/// <description>-120 - Mouse Scroll Toward User</description>
|
|
/// </item>
|
|
/// </list>
|
|
/// </remarks>
|
|
[FieldOffset(0x0A)] public short MouseData;
|
|
|
|
/// <summary>
|
|
/// Returns a Timestamp associated with the input, in System Ticks.
|
|
/// </summary>
|
|
[FieldOffset(0x10)] public int Timestamp;
|
|
}
|
|
|
|
public const int WH_MOUSE_LL = 14; // mouse hook constant
|
|
|
|
// 装置钩子的函数
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
|
|
|
|
// 卸下钩子的函数
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern bool UnhookWindowsHookEx(int idHook);
|
|
|
|
// 下一个钩挂的函数
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern IntPtr GetModuleHandle(string lpModuleName);
|
|
|
|
// 全局的鼠标事件
|
|
public event MouseEventHandler OnMouseActivity;
|
|
|
|
// 钩子回调函数
|
|
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
|
|
|
|
// 声明鼠标钩子事件类型
|
|
private HookProc _mouseHookProcedure;
|
|
private static int _hMouseHook = 0; // 鼠标钩子句柄
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public MouseHook()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 析构函数
|
|
/// </summary>
|
|
~MouseHook()
|
|
{
|
|
Stop();
|
|
}
|
|
/// <summary>
|
|
/// 启动全局钩子
|
|
/// </summary>
|
|
public void Start()
|
|
{
|
|
// 安装鼠标钩子
|
|
if (_hMouseHook == 0)
|
|
{
|
|
// 生成一个HookProc的实例.
|
|
_mouseHookProcedure = new HookProc(MouseHookProc);
|
|
|
|
_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, _mouseHookProcedure, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
|
|
|
|
//假设装置失败停止钩子
|
|
if (_hMouseHook == 0)
|
|
{
|
|
Stop();
|
|
throw new Exception("SetWindowsHookEx failed.");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止全局钩子
|
|
/// </summary>
|
|
public void Stop()
|
|
{
|
|
bool retMouse = true;
|
|
|
|
if (_hMouseHook != 0)
|
|
{
|
|
retMouse = UnhookWindowsHookEx(_hMouseHook);
|
|
_hMouseHook = 0;
|
|
}
|
|
|
|
// 假设卸下钩子失败
|
|
//if (!(retMouse))
|
|
// throw new Exception("UnhookWindowsHookEx failed.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 鼠标钩子回调函数
|
|
/// </summary>
|
|
private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
|
|
{
|
|
// 假设正常执行而且用户要监听鼠标的消息
|
|
if ((nCode >= 0) && (OnMouseActivity != null))
|
|
{
|
|
MouseButtons button = MouseButtons.None;
|
|
int clickCount = 0;
|
|
// 从回调函数中得到鼠标的信息
|
|
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
|
|
switch (wParam)
|
|
{
|
|
case WM_LBUTTONDOWN:
|
|
button = MouseButtons.Left;
|
|
clickCount = 1;
|
|
break;
|
|
case WM_LBUTTONUP:
|
|
button = MouseButtons.Left;
|
|
clickCount = 1;
|
|
break;
|
|
case WM_LBUTTONDBLCLK:
|
|
button = MouseButtons.Left;
|
|
clickCount = 2;
|
|
break;
|
|
case WM_RBUTTONDOWN:
|
|
button = MouseButtons.Right;
|
|
clickCount = 1;
|
|
break;
|
|
case WM_RBUTTONUP:
|
|
button = MouseButtons.Right;
|
|
clickCount = 1;
|
|
break;
|
|
case WM_RBUTTONDBLCLK:
|
|
button = MouseButtons.Right;
|
|
clickCount = 2;
|
|
break;
|
|
case WM_MBUTTONDOWN:
|
|
button = MouseButtons.Middle;
|
|
clickCount = 1;
|
|
break;
|
|
//case WM_MBUTTONUP:
|
|
// button = MouseButtons.Middle;
|
|
// clickCount = 1;
|
|
// break;
|
|
case WM_XBUTTONDOWN:
|
|
button = MyMouseHookStruct.MouseData == 1
|
|
? MouseButtons.XButton1
|
|
: MouseButtons.XButton2;
|
|
clickCount = 1;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
MouseEventArgs e = new MouseEventArgs(button, clickCount, MyMouseHookStruct.Point.X, MyMouseHookStruct.Point.Y, 0);
|
|
|
|
// 假设想要限制鼠标在屏幕中的移动区域能够在此处设置
|
|
// 后期须要考虑实际的x、y的容差
|
|
//if (!Screen.PrimaryScreen.Bounds.Contains(e.X, e.Y))
|
|
//{
|
|
// //return 1;
|
|
//}
|
|
OnMouseActivity(this, e);
|
|
}
|
|
|
|
// 启动下一次钩子
|
|
return CallNextHookEx(_hMouseHook, nCode, wParam, lParam);
|
|
}
|
|
}
|
|
}
|