一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

HOW TO: Set a Windows Hook in Visual C# .NET-Windo

时间:2008-01-12 编辑:简简单单 来源:一聚教程网

HOW TO: Set a Windows Hook in Visual C# .NET
The information in this article applies to:
Microsoft .NET Framework SDK
Microsoft Visual C# .NET (2002)
IN THIS TASK
SUMMARY
Set a Mouse Hook
Global Hook Is Not Supported in .NET Framework
REFERENCES
SUMMARY
This article describes how to set a hook that is specific to a thread and to a hook procedure by using the mouse hook as an example. You can use hooks to monitor certain types of events. You can associate these events with a specific thread or with all of the threads in the same desktop as a calling thread.
back to the top
Set a Mouse Hook
To set a hook, call the SetWindowsHookEx function from the User32.dll file. This function installs an application-defined hook procedure in the hook chain that is associated with the hook.
To set a mouse hook and to monitor the mouse events, follow these steps:
Start Microsoft Visual Studio .NET.
On the File menu, point to New, and then click Project.
In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates. In the Name box, type ThreadSpecificMouseHook. Form1 is added to the project by default.
Add the following line of code in the Form1.cs file after the other using statements:
using System.Runtime.InteropServices;
Add following code in the Form1 class:
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
//Declare hook handle as int.
static int hHook = 0;
//Declare mouse hook constant.

热门栏目