获取窗口标题

1、使用工具 microsoft spy++ visual studio 2007  D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\spy++.exe 

2 、使用方法

3、返回计算器的句柄和类名

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

namespace ConsoleApp11
{
    public struct WindowInfo
    {
        public IntPtr hWnd;
        public string szWindowName;
        public string szClassName;
    }
    class Program
    {
        [DllImport("shell32.dll")]
        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
        [DllImport("user32.dll")]
        //枚举所有屏幕上的顶层窗口,并将窗口句柄传送给应用程序定义的回调函数,lpEnumFunc回调函数的指针。
        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        //将指定的消息发送到一个或多个窗口
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lparam);
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//查找窗口
        //检索处理顶级窗口的类名和窗口名称匹配指定的字符串
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        //将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        //获得指定窗口所属的类的类名
        private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
        static IntPtr Game;

        static void Main(string[] args)
        {
            WindowInfo[] a = GetAllDesktopWindows();
            int i = 0;
            int index = 0;
            for (i = 0; i < a.Length; i++)
            {
               
                // MessageBox.Show(a[i].szWindowName.ToString());
                if (a[i].szWindowName.ToString().Contains("计算器"))
                {
                    //返回计算器的类名和句柄

                Console.WriteLine(a[i].szClassName.ToString());
                Console.WriteLine(a[i].hWnd.ToString());
                index = i;
                }
            }

            Game = a[index].hWnd;
            
            Console.ReadKey();
        }

        static WindowInfo[] GetAllDesktopWindows()
        {
            List<WindowInfo> wndList = new List<WindowInfo>();
            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                WindowInfo wnd = new WindowInfo();
                StringBuilder sb = new StringBuilder(256);
                //get hwnd
                wnd.hWnd = hWnd;
                //获取窗口名
                GetWindowTextW(hWnd, sb, sb.Capacity);
                wnd.szWindowName = sb.ToString();
                //获取窗口类名
                GetClassNameW(hWnd, sb, sb.Capacity);
                wnd.szClassName = sb.ToString();
                Console.WriteLine("Window handle=" + wnd.hWnd.ToString().PadRight(20) + " szClassName=" + wnd.szClassName.PadRight(20) + " szWindowName=" + wnd.szWindowName);
                //add it into list
                wndList.Add(wnd);
                return true;
            }, 0);
            return wndList.ToArray();
        }
    }
}

 

===============================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; 
namespace WindowPosDemo
{
    public delegate bool CallBack(int hwnd, int lParam);

    class Program
    {
        [DllImport("user32")]
        public static extern int EnumWindows(CallBack x, int y);

        static void Main(string[] args)
        {
            CallBack myCallBack = new CallBack(Program.Report);
            EnumWindows(myCallBack, 0);

            Console.ReadKey();
        }
        public static bool Report(int hwnd, int lParam)
        {
            Console.Write("Window handle is :"); 
            Console.WriteLine(hwnd); 
            Console.Read();
            return true; 
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值