欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的鼠标钩子

程序员文章站 2023-12-09 19:55:45
c#实现的鼠标钩子,可以获取鼠标在屏幕中的坐标,记得要以管理员权限运行才行 复制代码 代码如下: using system; using system.collect...

c#实现的鼠标钩子,可以获取鼠标在屏幕中的坐标,记得要以管理员权限运行才行

复制代码 代码如下:

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);
        //定义钩子句柄
        public static int hhook = 0;
        //定义钩子类型
        public const int wh_mouse_ll = 14;
        public hookproc myprocedure;
        //安装钩子
        [dllimport("user32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall)]
        public static extern int setwindowshookex(int idhook, hookproc lpfn, intptr hinstance, int threadid);
        //卸载钩子
        [dllimport("user32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall)]
        public static extern bool unhookwindowshookex(int idhook);
        //调用下一个钩子
        [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);
                //这里挂节钩子
                hhook = setwindowshookex(wh_mouse_ll, myprocedure, marshal.gethinstance(assembly.getexecutingassembly().getmodules()[0]), 0);
                if (hhook == 0)
                {
                    messagebox.show("setwindowshookex failed");
                    return;
                }
                button1.text = "卸载钩子";
            }
            else
            {
                bool ret = unhookwindowshookex(hhook);
                if (ret == false)
                {
                    messagebox.show("unhookwindowshookex failed");
                    return;
                }
                hhook = 0;
                button1.text = "安装钩子";
            }
        }
        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);
            }
        }
    }
}

演示:

C#实现的鼠标钩子

以上就是本文所述的全部内容了,希望大家能够喜欢。