c#如何获取window窗口句柄及里面的内容?
网友回复
[DllImport("User32.dll",EntryPoint="FindWindow")] private static extern IntPtr FindWindow(string lpClassName,string lpWindowName); IntPtr hWnd = FindWindow(null,"微信");
捕获窗体内的文本,先看看相关的api
// 查找窗口 [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // 遍历窗口的所有子窗口,通过CallBack回调 [DllImport("user32.dll")] public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam); public delegate bool CallBack(IntPtr hwnd, int lParam); // 获取窗口的类名 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); // 判断窗口是否可见 [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd); // 获取窗口文本长度 [DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hWnd); // 获取窗口文本,文本会塞入StringBuilder中,需要指明字符串最大长度nMaxCount [DllImport("User32.dll", EntryPoint = "GetWindowText")] private static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount); // 给窗口发送消息 [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); // 给窗口发送消息,事件返回的数据通过Byte[]数组获得 [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, Byte[] lParam);完整代码
using System...
点击查看剩余70%