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; } } }