利用定点、句柄,去抓取其他程序的值

时间:2022-03-24 14:38:00

通过“钩子”去到要抓取的定点xy,并转换成句柄,然后通过api获取想要的值。

代码如下

1、首先定义一个MouseHook 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;


namespace Demo
{
    class MouseHook 
    {
        private Point point;
        private Point Point
        {
            get { return point; }
            set
            {
                if (point != value)
                {
                    point = value;
                    if (MouseMoveEvent != null)
                    {
                        var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);
                        MouseMoveEvent(this, e);
                    }
                }
            }
        }
        private int hHook;
        public const int WH_MOUSE_LL = 14;
        public Win32Api.HookProc hProc;
        public MouseHook() { this.Point = new Point(); }
        public int SetHook()
        {
            hProc = new Win32Api.HookProc(MouseHookProc);
            hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
            return hHook;
        }
        public void UnHook()
        {
            Win32Api.UnhookWindowsHookEx(hHook);
        }
        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct));
            if (nCode < 0)
            {
                return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
            }
            else
            {
                this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
                return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
            }
        }
        //委托+事件(把钩到的消息封装为事件,由调用者处理)
        public delegate void MouseMoveHandler(object sender, MouseEventArgs e);
        public event MouseMoveHandler MouseMoveEvent;
    }
}

2、Win32Api类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace Demo
{
    class Win32Api
    {
        [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 delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        //安装钩子
        [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);


        [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
        public static extern int WindowFromPoint(
           int xPoint,
           int yPoint
        );
    }
}

3、通过定点抓取到xy,然后转成句柄,并取值

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace Demo
{
    public partial class Form_detail : Form
    {
        public Form_detail()
        {
            InitializeComponent();
        }
        MouseHook mh;
        private void Form_detail_Load(object sender, EventArgs e)
        {
            mh = new MouseHook();
            mh.SetHook();
            mh.MouseMoveEvent += mh_MouseMoveEvent;
        }
        void mh_MouseMoveEvent(object sender, MouseEventArgs e)
        {
            int x = e.Location.X;
            int y = e.Location.Y;
            int hwnd = Win32Api.WindowFromPoint(x, y);//获取指定坐标处窗口的句柄    
            textBox1.Text = hwnd.ToString();
        }


        private void Form_detail_FormClosed(object sender, FormClosedEventArgs e)
        {
            mh.UnHook();
        }




        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            this.Close();
            getValue.TestValue = textBox1.Text;
        }
    }
}



   [DllImport("user32")]
        public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);      
        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(JB01.Text) || String.IsNullOrEmpty(JB02.Text))
            {
                MessageBox.Show("句柄未抓取到");
                return;
            }
            int a = Convert.ToInt32(JB01.Text.ToString());
            int b = Convert.ToInt32(JB02.Text.ToString());
            IntPtr hWnd = new IntPtr(a);
            IntPtr hWnd2 = new IntPtr(b);
            //IntPtr hWnd = new IntPtr(330194);
            //IntPtr hWnd2 = new IntPtr(526822);
            StringBuilder text2 = new StringBuilder(256);
            StringBuilder text = new StringBuilder(256);          
            int len = GetWindowText(hWnd, text, 256);
            int len2 = GetWindowText(hWnd2, text2, 256);
            if (len > 0)
            {
                textBox1.Text = text.ToString();
            }
            if (len2 > 0)
            {
                textBox2.Text = text2.ToString();
            }
            hWnd = IntPtr.Zero;
            hWnd2 = IntPtr.Zero;
        }