基本信息
源码名称:C# 仿spy++查找窗口句柄
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2018-12-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

仿spy 查找窗口句柄

仿spy 查找窗口句柄 vs2010


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

namespace WinFinder
{
    public partial class FormFindHWnd : Form
    {
        #region 窗体级变量及引用方法定义

        [DllImport("user32.dll")]
        public static extern IntPtr WindowFromPoint(Point pt);
        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int MaxCount);
        [DllImport("user32")]
        public static extern uint RealGetWindowClass(IntPtr hWnd, StringBuilder pszType, int MaxCount);
        [DllImport("User32.dll")]
        public static extern IntPtr GetParent(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam);
        [DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        public delegate bool CallBack(IntPtr hwnd, int lParam);
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
         [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

        public struct Rect
        {
            public int left;
            public int top;
            public int right;
            public int bottom;

            public int Width
            {
                get
                {
                    return right - left;
                }
            }

            public int Height
            {
                get
                {
                    return bottom - top;
                }
            }
        }


        IntPtr hwdFinded = IntPtr.Zero;
        IntPtr hwdApp = IntPtr.Zero;
        IntPtr hwdTemp = IntPtr.Zero;
        Rect rect;
        Image imagePre;

        #endregion

        public FormFindHWnd()
        {
            InitializeComponent();
            pictureBoxFindWnd.Image = Image.FromFile("FindWndHome.bmp");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        

        private void pictureBoxFindWnd_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                textBoxGetText.Text = "";
                textBoxGetClass.Text = "";
                textBoxGetHwnd.Text = "";
                textBoxGetAppName.Text = "";

                //设置查找图标光标
                Cursor.Current = new Cursor("findwnd.cur");
                //变更背景图片
                imagePre = pictureBoxFindWnd.Image;
                pictureBoxFindWnd.Image = Image.FromFile( "FindWndGone.bmp");
                //设置本控件捕获鼠标,处理相应的鼠标事件
                pictureBoxFindWnd.Capture = true;

                hwdFinded = IntPtr.Zero;
            }
        }

        private void pictureBoxFindWnd_MouseUp(object sender, MouseEventArgs e)
        {
           List<IntPtr> sub= GetIntPtr(hwdFinded);
           listBox1.Items.Clear();
           foreach (IntPtr i in sub)
           {
               //输出标题
               StringBuilder strTemp = new StringBuilder(256);
               //GetWindowText(hwdFinded, strTemp, strTemp.Capacity);
               SendMessage(i, 0x000D, 256, strTemp);
               listBox1.Items.Add(i.ToString() "/"  strTemp.ToString());
           }
         
            //恢复初始状态
            Cursor.Current = Cursors.Default;
            pictureBoxFindWnd.Image = imagePre;
            pictureBoxFindWnd.Capture = false;
          
        }


        private void pictureBoxFindWnd_MouseMove(object sender, MouseEventArgs e)
        {
            if (!this.Bounds.Contains(Cursor.Position))
            {
                hwdFinded = WindowFromPoint(Cursor.Position);
                if (hwdFinded != IntPtr.Zero)
                {
                    //输出句柄
                    textBoxGetHwnd.Text = hwdFinded.ToString("D").PadLeft(8,'0');

                    //输出标题
                    StringBuilder strTemp = new StringBuilder(256);
                    //GetWindowText(hwdFinded, strTemp, strTemp.Capacity);
                    SendMessage(hwdFinded, 0x000D, 256, strTemp);                 
                    textBoxGetText.Text = strTemp.ToString();

                    //输出类名
                    RealGetWindowClass(hwdFinded, strTemp, 256);
                    textBoxGetClass.Text = strTemp.ToString();

                    //向上查找Windows窗体,应用程序的主窗体的父窗体句柄为IntPtr.Zero
                    IntPtr hWdParent = GetParent(hwdFinded);
                    
                    while (hWdParent != IntPtr.Zero)
                    {
                        txtParent.Text = hWdParent.ToString();
                        hwdTemp = hWdParent;
                        hWdParent = GetParent(hwdTemp);
                    }
                    
                    StringBuilder title = new StringBuilder(256);
                    GetWindowText(hwdTemp, title, title.Capacity);
                    textBoxGetAppName.Text = title.ToString();
                    if (hwdTemp != hwdFinded)
                    {
                        hwdApp = hwdTemp;
                    }
                    else
                    {
                        hwdApp = hwdFinded;
                    }

                    //输出相对位置
                    rect = new Rect();
                    GetWindowRect(hwdFinded, ref rect);
                    textBoxRevPos.Text = "{X="   (Cursor.Position.X - rect.left).ToString()   ",Y="   (Cursor.Position.Y - rect.top).ToString()   "}";
                    label7.Text =rect.left "/" rect.top ","  rect.Width.ToString() "/"  rect.Height.ToString();
                    textBoxSrcPos.Text= Cursor.Position.ToString();
                  // point=curo
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           
            rect = new Rect();
            GetWindowRect(hwdFinded, ref rect);
            Point pos = Cursor.Position;
            Rectangle r = new Rectangle(rect.left, rect.bottom - 100, rect.Width, 100);
            label7.Text = r.ToString() Cursor.Position.ToString();
            if(!r.Contains(Cursor.Position))
            {
                Cursor.Position = new Point((rect.left r.Width)/2, rect.bottom - 50);
                //mouse_event(0x8000, rect.left   30, rect.bottom - 50, 0, 0);  
                mouse_event(0x0002|0x0004, 0, 0, 1, 0); //模拟鼠标按下操作
            }
            if (WindowFromPoint(Cursor.Position) == hwdFinded)
            {
                //SendMessage(hwdFinded, 0x0C, 256,new StringBuilder("我是中国人"));
                SendKeys.Send("我是中国人");
                SendKeys.Send("{ENTER}");
            }
            System.Threading.Thread.Sleep(1000);
            Cursor.Position = pos;
            //SendMessage(hwdFinded,  0x0C, 256, new StringBuilder("abc\r\n"));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
            //SendMessage(hwdFinded, 0x000C, 256, new StringBuilder("abc\r\n"));
            //mouse_event(0x0002, rect.left   20, rect.bottom - 20, 0, 0); //模拟鼠标按下操作
            //mouse_event(0x0004, rect.left   20, rect.bottom - 20, 0, 0); //模拟鼠标放开操作
            // int h=rect.Height-50-(Cursor.Position.Y - rect.top );
            // label7.Text = h.ToString();
            // if ( h> 0)
           // mouse_event(0x0001, 0, 30, 0, 0);
            //  mouse_event(0x0001, rect.left   20, rect.bottom - 20, 0, 0);
           // SendKeys.Send("我是中国人");   //模拟键盘输入游戏ID
            //SendKeys.Send("{TAB}"); //模拟键盘输入TAB
            //SendKeys.Send(_GamePass); //模拟键盘输入游戏密码
           // SendKeys.Send("{ENTER}"); //模拟键盘输入ENTER
        }

        public static List<IntPtr> GetIntPtr(IntPtr hwd)
        {
            List<IntPtr> listIntPtr = new List<IntPtr>();
            EnumChildWindows(hwd, delegate(IntPtr hWnd, int lParam)
            {
                listIntPtr.Add(hWnd);
                return true;
            }, 0);
            return listIntPtr;
        }
        public static IntPtr FindWindowEx(IntPtr hwnd, string lpszWindow, bool bChild)
        {
            IntPtr iResult = IntPtr.Zero;
            // 首先在父窗体上查找控件
            iResult = FindWindowEx(hwnd, (uint)IntPtr.Zero, null, lpszWindow);
            // 如果找到直接返回控件句柄
            if (iResult != IntPtr.Zero) return iResult;

            // 如果设定了不在子窗体中查找
            if (!bChild) return iResult;

            // 枚举子窗体,查找控件句柄
            int i = EnumChildWindows(
            hwnd,
            (h, l) =>
            {
                IntPtr f1 = FindWindowEx(h, (uint)IntPtr.Zero, null, lpszWindow);
                if (f1 == IntPtr.Zero)
                    return true;
                else
                {
                    iResult = f1;
                    return false;
                }
            },
            0);
            // 返回查找结果
            return iResult;
        }
    }
}