How to: Set a Windows Hook in Visual C #. NET-Windo

Source: Internet
Author: User
How to: Set a Windows Hook in Visual C #. NET
The information in this article applies:
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 installan 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.