using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class LMouse
{
#region # 鼠标事件结构。#
/// <summary>
/// 鼠标事件结构。
/// </summary>
public struct LMouseEvent
{
/// <summary>
/// 鼠标移动。
/// </summary>
public static int Move = 512;
/// <summary>
/// 鼠标右键按下。
/// </summary>
public static int RightButtonDown = 516;
/// <summary>
/// 鼠标右键弹起。
/// </summary>
public static int RightButtonUp = 517;
/// <summary>
/// 鼠标左键按下。
/// </summary>
public static int LeftButtonDown = 513;
/// <summary>
/// 鼠标左键弹起。
/// </summary>
public static int LeftButtonUp = 514;
/// <summary>
/// 鼠标中间按下。
/// </summary>
public static int MiddleButtonDown = 519;
/// <summary>
/// 鼠标中间弹起。
/// </summary>
public static int MiddleButtonUp = 520;
/// <summary>
/// 鼠标滚轮滚动。
/// </summary>
public static int MiddleButonScroll = 522;
}
#endregion
#region # 全局变量 #
/// <summary>
/// SetWindowsHookEx的返回值,若钩子安装成功为该挂钩处理过程的句柄,否则为null或0。
/// </summary>
protected static int hMouseHook = 0;/// <summary>
/// 回调函数委托。
/// </summary>
/// <param name="nCode">钩子代码。</param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns>表示是否拦截。</returns>
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
/// 设置钩子。
/// </summary>
/// <param name="idHook"></param>
/// <param name="lpfn"></param>
/// <param name="hInstance"></param>
/// <param name="threadId"></param>
/// <returns></returns>
[DllImport("user32.dll")]
protected static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
/// <summary>
/// 取消钩子 。
/// </summary>
/// <param name="idHook"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
protected static extern bool UnhookWindowsHookEx(int idHook);
/// <summary>
/// 调用下一个钩子 。
/// </summary>
/// <param name="idHook"></param>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll")]
protected static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
/// <summary>
/// 获取应用程序模块句柄。
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[DllImport("kernel32.dll")]
protected static extern IntPtr GetModuleHandle(string name);
private HookProc hookProc;
#endregion
#region # 鼠标结构 #
/// <summary>
/// 鼠标结构。
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public class MouseStruct
{
/// <summary>
/// 鼠标坐标。
/// </summary>
public Point Position;
/// <summary>
///
/// </summary>
public int Handle;
/// <summary>
///
/// </summary>
public int wHitTestCode;
/// <summary>
///
/// </summary>
public int dwExtraInfo;
}
#endregion
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
#region # 安装钩子 #
/// <summary>
/// 安装钩子。
/// </summary>
public bool InstallHook(Func<int, MouseStruct, bool> method)
{
if (hMouseHook == 0)
{
//将回调函数存放在成员变量中,避免回调函数被GC销毁。
hookProc = GetCallback(method);
Process currProc = Process.GetCurrentProcess();//获取当前进程。
ProcessModule currModule = currProc.MainModule;//获取当前进程主模块。
IntPtr currHandle = GetModuleHandle(currModule.ModuleName);//获取实例句柄。
hMouseHook = SetWindowsHookEx(14, hookProc, currHandle, 0);//安装钩子。
if (hMouseHook == 0)//如果钩子安装失败。
UnInstallHook();
else
return true;
}
return false;
}
#endregion
#region # 卸载钩子 #
/// <summary>
/// 卸载钩子。
/// </summary>
public bool UnInstallHook()
{
if (hMouseHook != 0)
if (UnhookWindowsHookEx(hMouseHook))
{
hMouseHook = 0;
return true;
}
return false;
}
#endregion
#region # 返回一个鼠标钩子的回调函数。+ private HookProc GetCallback(Func<int, MouseStruct, bool> fn) #
/// <summary>
/// 返回一个鼠标钩子的回调函数。
/// </summary>
/// <param name="fn">外部自定义处理方法。</param>
/// <returns></returns>
private HookProc GetCallback(Func<int, MouseStruct, bool> fn)
{
return (nCode, wParam, lParam) =>
{
if (nCode >= 0)
{
//将非托管数据类型转换成托管类型
MouseStruct mouseStruct = (MouseStruct)Marshal.PtrToStructure(lParam, typeof(MouseStruct));
if (fn(wParam, mouseStruct))
return 1;
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
};
}
#endregion
#region 00. 屏蔽鼠标(禁止鼠标移动,禁用鼠标左右键以及鼠标滚轮)。+ public void EnableMouse(bool flag)
/// <summary>
/// 屏蔽鼠标(禁止鼠标移动,禁用鼠标左右键以及鼠标滚轮)。
/// </summary>
/// <param name="flag">true为屏蔽,false不屏蔽。</param>
public void EnabledMouse(bool flag)
{
if (flag)
this.InstallHook((x, y) => true);
else
this.UnInstallHook();
}
#endregion
#region 01. 屏蔽鼠标右键。+ public void EnabledRightButton(bool flag)
/// <summary>
/// 屏蔽鼠标右键。
/// </summary>
/// <param name="flag">true屏蔽,false取消屏蔽。</param>
public void EnabledRightButton(bool flag)
{
if (flag)
this.InstallHook((x, y) => x == LMouseEvent.RightButtonDown || x == LMouseEvent.RightButtonUp);
else
this.UnInstallHook();
}
#endregion
#region 02. 屏蔽鼠标左键。+ public void EnabledLeftButton(bool flag)
/// <summary>
/// 屏蔽鼠标左键。
/// </summary>
/// <param name="flag">true屏蔽,false取消屏蔽。</param>
public void EnabledLeftButton(bool flag)
{
if (flag)
this.InstallHook((x, y) => x == LMouseEvent.LeftButtonDown || x == LMouseEvent.LeftButtonUp);
else
this.UnInstallHook();
}
#endregion
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/34945.html