C#利用钩子控制鼠标
作者:清清月儿
主页:http://blog.csdn.net/21aspnet/ 时间:2007.5.11
工作中有这样的需求,某个控件panel的子控件textbox要实现只留鼠标右键copy,注意同时还不能影响其它panel的子控件textbox,怎么办?
答案是只有用钩子,在codeporject上找到这么一个钩子。
如图所示,第一个文本框只有copy功能。
UserActivityHook.cs
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Reflection;
usingSystem.Threading;
usingSystem.Windows.Forms;
usingSystem.ComponentModel;
namespacegma.System.Windows
...{
/**////<summary>
///Thisclassallowsyoutotapkeyboardandmouseand/ortodetecttheiractivityevenwhenan
///applicationrunesinbackgroundordoesnothaveanyuserinterfaceatall.Thisclassraises
///common.NETeventswithKeyEventArgsandMouseEventArgssoyoucaneasilyretrieveanyinformationyouneed.
///</summary>
publicclassUserActivityHook
...{
Windowsstructuredefinitions#regionWindowsstructuredefinitions
/**////<summary>
///ThePOINTstructuredefinesthex-andy-coordinatesofapoint.
///</summary>
///<remarks>
///http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_0tiq.asp
///</remarks>
[StructLayout(LayoutKind.Sequential)]
privateclassPOINT
...{
/**////<summary>
///Specifiesthex-coordinateofthepoint.
///</summary>
publicintx;
/**////<summary>
///Specifiesthey-coordinateofthepoint.
///</summary>
publicinty;
}
/**////<summary>
///TheMOUSEHOOKSTRUCTstructurecontainsinformationaboutamouseeventpassedtoaWH_MOUSEhookprocedure,MouseProc.
///</summary>
///<remarks>
///http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp
///</remarks>
[StructLayout(LayoutKind.Sequential)]
privateclassMouseHookStruct
...{
/**////<summary>
///SpecifiesaPOINTstructurethatcontainsthex-andy-coordinatesofthecursor,inscreencoordinates.
///</summary>
publicPOINTpt;
/**////<summary>
///Handletothewindowthatwillreceivethemousemessagecorrespondingtothemouseevent.
///</summary>
publicinthwnd;
/**////<summary>
///Specifiesthehit-testvalue.Foralistofhit-testvalues,seethedescriptionoftheWM_NCHITTESTmessage.
///</summary>
publicintwHitTestCode;
/**////<summary>
///Specifiesextrainformationassociatedwiththemessage.
///</summary>
publicintdwExtraInfo;
}
/**////<summary>
///TheMSLLHOOKSTRUCTstructurecontainsinformationaboutalow-levelkeyboardinputevent.
///</summary>
[StructLayout(LayoutKind.Sequential)]
privateclassMouseLLHookStruct
...{
/**////<summary>
///SpecifiesaPOINTstructurethatcontainsthex-andy-coordinatesofthecursor,inscreencoordinates.
///</summary>
publicPOINTpt;
/**////<summary>
///IfthemessageisWM_MOUSEWHEEL,thehigh-orderwordofthismemberisthewheeldelta.
///Thelow-orderwordisreserved.Apositivevalueindicatesthatthewheelwasrotatedforward,
///awayfromtheuser;anegativevalueindicatesthatthewheelwasrotatedbackward,towardtheuser.
///OnewheelclickisdefinedasWHEEL_DELTA,whichis120.
///IfthemessageisWM_XBUTTONDOWN,WM_XBUTTONUP,WM_XBUTTONDBLCLK,WM_NCXBUTTONDOWN,WM_NCXBUTTONUP,
///orWM_NCXBUTTONDBLCLK,thehigh-orderwordspecifieswhichXbuttonwaspressedorreleased,
///andthelow-orderwordisreserved.Thisvaluecanbeoneormoreofthefollowingvalues.Otherwise,mouseDataisnotused.
///XBUTTON1
///ThefirstXbuttonwaspressedorreleased.
///XBUTTON2
///ThesecondXbuttonwaspressedorreleased.
///</summary>
publicintmouseData;
/**////<summary>
///Specifiestheevent-injectedflag.Anapplicationcanusethefollowingvaluetotestthemouseflags.ValuePurpose
///LLMHF_INJECTEDTesttheevent-injectedflag.
///0
///Specifieswhethertheeventwasinjected.Thevalueis1iftheeventwasinjected;otherwise,itis0.
///1-15
///Reserved.
///</summary>
publicintflags;
/**////<summary>
///Specifiesthetimestampforthismessage.
///</summary>
publicinttime;
/**////<summary>
///Specifiesextrainformationassociatedwiththemessage.
///</summary>
publicintdwExtraInfo;
}
/**////<summary>
///TheKBDLLHOOKSTRUCTstructurecontainsinformationaboutalow-levelkeyboardinputevent.
///</summary>
///<remarks>
///http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookstructures/cwpstruct.asp
///</remarks>
[StructLayout(LayoutKind.Sequential)]
privateclassKeyboardHookStruct
...{
/**////<summary>
///Specifiesavirtual-keycode.Thecodemustbeavalueintherange1to254.
///</summary>
publicintvkCode;
/**////<summary>
///Specifiesahardwarescancodeforthekey.
///</summary>
publicintscanCode;
/**////<summary>
///Specifiestheextended-keyflag,event-injectedflag,contextcode,andtransition-stateflag.
///</summary>
publicintflags;
/**////<summary>
///Specifiesthetimestampforthismessage.
///</summary>
publicinttime;
/**////<summary>
///Specifiesextrainformationassociatedwiththemessage.
///</summary>
publicintdwExtraInfo;
}
#endregion
Windowsfunctionimports#regionWindowsfunctionimports
/**////<summary>
///TheSetWindowsHookExfunctioninstallsanapplication-definedhookprocedureintoahookchain.
///Youwouldinstallahookproceduretomonitorthesystemforcertaintypesofevents.Theseevents
///areassociatedeitherwithaspecificthreadorwithallthreadsinthesamedesktopasthecallingthread.
///</summary>
///<paramname="idHook">
///[in]Specifiesthetypeofhookproceduretobeinstalled.Thisparametercanbeoneofthefollowingvalues.
///</param>
///<paramname="lpfn">
///[in]Pointertothehookprocedure.IfthedwThreadIdparameteriszeroorspecifiestheidentifierofa
///threadcreatedbyadifferentprocess,thelpfnparametermustpointtoahookprocedureinadynamic-link
///library(DLL).Otherwise,lpfncanpointtoahookprocedureinthecodeassociatedwiththecurrentprocess.
///</param>
///<paramname="hMod">
///[in]HandletotheDLLcontainingthehookprocedurepointedtobythelpfnparameter.
///ThehModparametermustbesettoNULLifthedwThreadIdparameterspecifiesathreadcreatedby
///thecurrentprocessandifthehookprocedureiswithinthecodeassociatedwiththecurrentprocess.
///</param>
///<paramname="dwThreadId">
///[in]Specifiestheidentifierofthethreadwithwhichthehookprocedureistobeassociated.
///Ifthisparameteriszero,thehookprocedureisassociatedwithallexistingthreadsrunninginthe
///samedesktopasthecallingthread.
///</param>
///<returns>
///Ifthefunctionsucceeds,thereturnvalueisthehandletothehookprocedure.
///Ifthefunctionfails,thereturnvalueisNULL.Togetextendederrorinformation,callGetLastError.
///</returns>
///<remarks>
///http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp
///</remarks>
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall,SetLastError=true)]
privatestaticexternintSetWindowsHookEx(
intidHook,
HookProclpfn,
IntPtrhMod,
intdwThreadId);
/**////<summary>
///TheUnhookWindowsHookExfunctionremovesahookprocedureinstalledinahookchainbytheSetWindowsHookExfunction.
///</summary>
///<paramname="idHook">
///[in]Handletothehooktoberemoved.ThisparameterisahookhandleobtainedbyapreviouscalltoSetWindowsHookEx.
///</param>
///<returns>
///Ifthefunctionsucceeds,thereturnvalueisnonzero.
///Ifthefunctionfails,thereturnvalueiszero.Togetextendederrorinformation,callGetLastError.
///</returns>
///<remarks>
///http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindowshookex.asp
///</remarks>
[DllImport("user32.dll",CharSet=CharSet.Auto,
src
分享到:
相关推荐
c# 钩子记录鼠标运行轨迹c# 钩子记录鼠标运行轨迹 c# 钩子记录鼠标运行轨迹c# 钩子记录鼠标运行轨迹c# 钩子记录鼠标运行轨迹 c# 钩子记录鼠标运行轨迹
在IT领域,尤其是在Windows...总的来说,C#键盘鼠标钩子是一个深入的Windows编程主题,涉及到对底层操作系统的理解以及使用P/Invoke调用API。通过学习和实践,开发者可以构建出能够控制或扩展用户输入行为的应用程序。
在C#中,我们通常利用P/Invoke(Platform Invoke)来调用Windows API函数实现钩子。以下是一些关键知识点: 1. **SetWindowsHookEx函数**:这是Windows API中用于设置钩子的关键函数,接受一个钩子类型、钩子处理...
在这个场景中,我们看到标题提到的是“c#全局钩子”,但实现方式是通过C++来编写鼠标过程。这通常涉及到混合编程,即在C#应用程序中嵌入C++代码来实现特定功能。 首先,让我们了解一下什么是钩子。在Windows操作...
在C#中,我们可以利用`SetWindowsHookEx`函数通过P/Invoke技术调用Win32 API来设置鼠标钩子。通常,我们需要创建一个委托类型以定义钩子回调函数,并在该函数中处理捕获到的鼠标事件,如鼠标移动、点击等。 ### ...
总结,通过使用C#调用Windows API中的钩子功能,我们可以拦截并控制`OpenFileDialog`和`SaveFileDialog`的鼠标右键事件,实现特定的定制行为。这个过程中涉及到了P/Invoke、钩子机制、回调函数以及Windows消息处理等...
使用C#编写的源码,内含4个工具类(键盘监控、键盘输入、鼠标监控、鼠标输入),根据需要自己调用,窗口程序仅做实例效果参考,VS2015,64位win10实测可用,目前尚未实现驱动级,编写外挂可能不会有效果
通过这个"C#键盘鼠标钩子"项目,开发者提供了无DLL依赖的实现方法,简化了部署流程,同时也展示了如何在C#中有效利用WinAPI来扩展.NET Framework的功能。学习和理解这一技术可以帮助开发者更好地控制应用程序的行为...
在C#编程中,"鼠标钩子"是一个高级技术,用于拦截和处理系统级别的鼠标事件。这通常涉及使用Windows API(应用程序接口)函数,因为.NET Framework本身并不直接提供这样的功能。通过设置鼠标钩子,开发者可以捕获并...
在C#中实现全局键盘钩子,开发者通常会利用Windows API函数,如SetWindowsHookEx。这个特定的压缩包文件"AdrHookDemo"可能包含了一个演示如何在C#中实现这种功能的示例项目。 首先,我们要理解键盘钩子的工作原理。...
C#键盘鼠标钩子,可以知道键盘和鼠标的动作, 本人根据这个工具,制作成了不用鼠标键盘五分钟 用友自动退出工具 当然了,还可以做成很多程序! QQ交流:873968102
在IT行业中,C#是一种广泛使用的编程语言,尤其在Windows应用程序和游戏开发中。本案例中,我们讨论的主题是如何...同时,了解如何利用钩子技术与硬件交互,对于进行更复杂的系统集成和设备控制也具有重要的实践价值。
总的来说,这个【C# 键盘钩子实例源码】是学习如何在C#中利用Windows API实现键盘监控的绝佳资源。通过分析和运行这个源码,开发者可以深入理解Windows消息机制、P/Invoke技术以及钩子的工作原理,这对于提升Windows...
### C# 全局钩子 (实例) #### 概述 在C#中,全局钩子(Global Hook)是一种能够捕获系统范围内事件的技术。它主要用于监控和拦截键盘或鼠标等输入设备产生的消息。本篇内容将详细介绍如何使用C#实现全局键盘钩子,...
本项目“C#全局钩子屏蔽键盘按键Demo”就是利用C#编程语言实现的一个实例,能够有效地屏蔽键盘按键以及特定的组合键,如Alt+F4。 1. **C#编程语言**:C#是由微软开发的一种面向对象的编程语言,广泛用于Windows桌面...
本文将深入探讨如何利用C#来实现键盘钩子(Keyboard Hook)技术,从而达到屏蔽特定热键的目的。热键通常指的是用户可以快捷激活某些功能的组合键,如Ctrl+C用于复制,Alt+F4用于关闭窗口。在某些特定应用场景下,...
本项目“C#版本键盘钩子及鼠标控制”聚焦于这一技术,旨在实现对Windows操作系统中键盘和鼠标输入的强大控制。 首先,键盘钩子(Keyboard Hook)是一种技术,它允许程序监视并处理系统的键盘事件,即使这些事件发生...
C#实现键盘鼠标全局钩子 VS2013,可编译运行,带DEMO 可全局拦截鼠标单击/双击/滚动事件 可全局拦截键盘按键事件 可锁定键盘鼠标输入,比如锁屏! 该代码无法屏蔽Ctrl +alt +delete 需要实现所有拦截,请参考相关...
总的来说,这个示例项目是一个很好的起点,帮助开发者了解如何在C#中利用Windows API来实现全局的键盘和鼠标钩子,从而捕获和响应用户的输入事件。通过研究这些代码,你可以学习到C#与底层Windows API交互的方式,...