博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 钩子屏蔽鼠标右键
阅读量:6826 次
发布时间:2019-06-26

本文共 4241 字,大约阅读时间需要 14 分钟。

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace MouseHook

{
public partial class Form1 : Form
{
#region Fields

private int hMouseHook = 0;

//全局钩子常量

private const int WH_MOUSE_LL = 14;

//声明消息的常量,鼠标按下和释放

private const int WM_RBUTTONDOWN = 0x204;
private const int WM_RBUTTONUP = 0x205;
//保存任务栏的矩形区域
private Rectangle taskBarRect;
private Rectangle newTaskBarRect;

//定义委托

public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
private HookProc MouseHookProcedure;

#endregion

#region 声明Api函数,需要引入空间(System.Runtime.InteropServices)

//寻找符合条件的窗口

[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(string lpClassName,string lpWindowName);

//获取窗口的矩形区域

[DllImport("user32.dll", EntryPoint = "GetWindowRect")]
public static extern int GetWindowRect(int hwnd,ref Rectangle lpRect);

//安装钩子

[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook,HookProc lpfn,IntPtr hInstance,int threadId);

//卸载钩子

[DllImport("user32.dll", EntryPoint = "UnhookWindowsHookEx")]
public static extern bool UnhookWindowsHookEx(int hHook);

//调用下一个钩子

[DllImport("user32.dll")]
public static extern int CallNextHookEx(int idHook,int nCode,int wParam,IntPtr lParam);

//获取当前线程的标识符

[DllImport("kernel32.dll")]
public static extern int GetCurrentThreadId();

//获取一个应用程序或动态链接库的模块句柄

[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string name);

//鼠标结构,保存了鼠标的信息

[StructLayout(LayoutKind.Sequential)]
public struct MOUSEHOOKSTRUCT
{
public Point pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}

#endregion

public Form1()

{
InitializeComponent();
}

/// <summary>

/// 安装钩子
/// </summary>
private void StartHook()
{
if (hMouseHook == 0)
{
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);

if (hMouseHook == 0)

{//如果设置钩子失败.

this.StopHook();

MessageBox.Show("Set windows hook failed!");
}
}
}

/// <summary>

/// 卸载钩子
/// </summary>
private void StopHook()
{
bool stop = true;

if (hMouseHook != 0)

{
stop = UnhookWindowsHookEx(hMouseHook);
hMouseHook = 0;

if (!stop)

{//卸载钩子失败

MessageBox.Show("Unhook failed!");

}
}
}

private int MouseHookProc(int nCode, int wParam, IntPtr lParam)

{
if (nCode >= 0)
{
//把参数lParam在内存中指向的数据转换为MOUSEHOOKSTRUCT结构
MOUSEHOOKSTRUCT mouse = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));//鼠标
//这句为了看鼠标的位置
this.Text = "MousePosition:" + mouse.pt.ToString();

if (wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP)

{ //鼠标按下或者释放时候截获

if (newTaskBarRect.Contains(mouse.pt))

{ //当鼠标在任务栏的范围内

//如果返回1,则结束消息,这个消息到此为止,不再传递。

//如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递,也就是传给消息真正的接受者
return 1;
}
}
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}

#region Events

private void Form1_Load(object sender, EventArgs e)

{
MouseHookProcedure = new HookProc(MouseHookProc);

//taskBarHandle为返回的任务栏的句柄

//Shell_TrayWnd为任务栏的类名
int taskBarHandle = FindWindow("Shell_TrayWnd", null);

//获得任务栏的区域

//有一点要注意,函数返回时,taskBarRect包含的是窗口的左上角和右下角的屏幕坐标
//就是说taskBarRect.Width和taskBarRect.Height是相对于屏幕左上角(0,0)的数值
//这与c#的Rectangle结构是不同的
GetWindowRect(taskBarHandle, ref taskBarRect);

this.richTextBox1.Text = "taskBarRect.Location:" + taskBarRect.Location.ToString() + "\n";
this.richTextBox1.Text += "taskBarRect.Size:" + taskBarRect.Size.ToString() + "\n\n";

//构造一个c#中的Rectangle结构
newTaskBarRect = new Rectangle(
taskBarRect.X,
taskBarRect.Y,
taskBarRect.Width - taskBarRect.X,
taskBarRect.Height - taskBarRect.Y
);

this.richTextBox1.Text += "newTaskBarRect.Location:" + newTaskBarRect.Location.ToString() + "\n";

this.richTextBox1.Text += "newTaskBarRect.Size:" + newTaskBarRect.Size.ToString();

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{
this.StopHook();
}

private void button1_Click(object sender, EventArgs e)

{
this.StartHook();
this.button1.Enabled = false;
this.button2.Enabled = true;

}

private void button2_Click(object sender, EventArgs e)

{
this.StopHook();
this.button1.Enabled = true;
this.button2.Enabled = false;
}

#endregion

}
}

本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/08/18/1548712.html,如需转载请自行联系原作者

你可能感兴趣的文章
jquery------使用jQuery的委托方法
查看>>
redis运维的一些知识点
查看>>
ZZZZ
查看>>
Win7或Windows server 2008中IIS7支持ASP+Access解决方法
查看>>
intent 图片调用问题
查看>>
div仿框架布局
查看>>
Windows 服务(附服务开发辅助工具)
查看>>
ScrollView反弹效果 仿小米私密短信效果
查看>>
Redis命令拾遗五(有序集合)
查看>>
防止WordPress利用xmlrpc.php进行暴力破解以及DDoS
查看>>
Rafy 框架 - 使用 SqlTree 查询
查看>>
Python进阶 学习笔记(一)
查看>>
电梯测试点有哪些?
查看>>
如何点击UIWebView上html链接不弹出复制粘贴
查看>>
HDU 2709 Sumsets(递推)
查看>>
spring属性注入DI
查看>>
ofo4.0时代展望移动物联网应用 让智慧生活“随意念而动”
查看>>
阿里巴巴到底是一家什么公司?
查看>>
理想生活没那么难
查看>>
程序员都用什么牌子电脑编程?国产电脑很强大啊!
查看>>