国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現(xiàn)的鼠標鉤子

2020-01-24 02:08:07
字體:
供稿:網(wǎng)友

C#實現(xiàn)的鼠標鉤子,可以獲取鼠標在屏幕中的坐標,記得要以管理員權(quán)限運行才行

復(fù)制代碼 代碼如下:

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);
        //調(diào)用下一個鉤子
        [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);
                //這里掛節(jié)鉤子
                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);
            }
        }
    }
}

演示:

以上就是本文所述的全部內(nèi)容了,希望大家能夠喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 资中县| 敦化市| 正定县| 耒阳市| 鄂伦春自治旗| 成都市| 长寿区| 类乌齐县| 永泰县| 镇赉县| 五大连池市| 台湾省| SHOW| 巩留县| 玉环县| 南乐县| 江陵县| 文昌市| 高雄县| 牙克石市| 民乐县| 沙湾县| 库尔勒市| 云阳县| 鄂托克前旗| 韶关市| 怀集县| 新龙县| 武宁县| 鲜城| 高要市| 保亭| 东莞市| 泰和县| 扶沟县| 博湖县| 商都县| 贵定县| 鄄城县| 安庆市| 弥勒县|