当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现的鼠标钩子

C#实现的鼠标钩子

2019年07月18日  | 移动技术网IT编程  | 我要评论
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);
            }
        }
    }
}

演示:

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网