C # Implementation of mouse hooks

Source: Internet
Author: User

C # Implementation of the mouse hook, you can get the mouse in the screen coordinates, remember to run with administrator rights only line


The code is as follows:


Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Linq;

Using System.Reflection;

Using System.Runtime.InteropServices;

Using System.Text;

Using System.Windows.Forms;

Namespace App01

{

public partial class Form1:form

{

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

Defining a hook handle

public static int hhook = 0;

Defining hook types

public const int WH_MOUSE_LL = 14;

Public HookProc myprocedure;

Mounting hooks

[DllImport ("user32.dll", CharSet = CharSet.Auto, callingconvention = Callingconvention.stdcall)]

public static extern int SetWindowsHookEx (int idhook, HookProc lpfn, IntPtr hinstance, int threadId);

Uninstalling Hooks

[DllImport ("user32.dll", CharSet = CharSet.Auto, callingconvention = Callingconvention.stdcall)]

public static extern bool UnhookWindowsHookEx (int idhook);

Call the next hook

[DllImport ("user32.dll", CharSet = CharSet.Auto, callingconvention = Callingconvention.stdcall)]

public static extern int CallNextHookEx (int idhook, int nCode, IntPtr wParam, IntPtr lParam);

[StructLayout (LayoutKind.Sequential)]

public class Point

{

public int x;

public int y;

}

[StructLayout (LayoutKind.Sequential)]

public class MouseHookStruct

{

Public Point pt;

public int hwnd;

public int whittestcode;

public int dwextrainfo;

}

Public Form1 ()

{

InitializeComponent ();

}

private void Button1_Click (object sender, EventArgs e)

{

if (Hhook = = 0)

{

Myprocedure = new HookProc (this. MOUSEHOOKPROC);

Here's a hook for hanging knots

Hhook = SetWindowsHookEx (Wh_mouse_ll, Myprocedure, Marshal.gethinstance (assembly.getexecutingassembly (). GetModules () [0]), 0);

if (Hhook = = 0)

{

MessageBox.Show ("SetWindowsHookEx Failed");

Return

}

Button1. Text = "Unload hook";

}

Else

{

BOOL ret = UnhookWindowsHookEx (hhook);

if (ret = = false)

{

MessageBox.Show ("UnhookWindowsHookEx Failed");

Return

}

Hhook = 0;

Button1. Text = "Mount Hook";

}

}

public int MouseHookProc (int nCode, IntPtr wParam, IntPtr LParam)

{

MouseHookStruct mymousehookstruct = (mousehookstruct) marshal.ptrtostructure (LParam, typeof (MouseHookStruct));

if (NCode < 0)

{

Return CallNextHookEx (Hhook, NCode, WParam, LParam);

}

Else

{

String strcaption = "x =" + mymousehookstruct.pt.x.tostring ("D") + "y =" + mymousehookstruct.pt.y.tostring ("D");

This. Text = strcaption;

Return CallNextHookEx (Hhook, NCode, WParam, LParam);

}

}

}

}

Demonstrate:

The above is the whole content of this article, I hope you can enjoy.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Implementation of mouse hooks

This address: http://www.paobuke.com/develop/c-develop/pbk23118.html






Related Content C # netremoting implementation of bidirectional communication C # Boxing and unpacking Knowledge Review C # textbox text box word limit problem C # How to add a background image to a PDF
C # Connection to the database methods C # and JavaScript implement interactive methods C # implements the Boss key function code C # method for generating code128 barcodes

C # Implementation of mouse hooks

Related Article

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.