[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;
using System.Runtime.InteropServices;
using System.Text;
public partial class WindowApiHelper
{
/// <summary>
/// 尝试查找Error窗口并取出窗口文本
/// </summary>
/// <returns></returns>
public static string TryFindErrorWindowText()
{
string errorText = "";
// 查找标题为Error的窗口
IntPtr mainHandle = FindWindow(null, "Error");
if (mainHandle != IntPtr.Zero)
{
// 枚举子窗体,查找控件句柄
int i = EnumChildWindows(mainHandle, (h, l) =>
{
StringBuilder sbr = new StringBuilder();
GetClassName(h, sbr, 255);
string classname = sbr.ToString();
// 获取Edit子窗口
if ("Edit" == classname)
{
// 是否可见
if (IsWindowVisible(h))
{
// 取出窗口文本
int textLen;
textLen = SendMessage(h, WM_GETTEXTLENGTH, 0, 0);
Byte[] byt = new Byte[textLen];
SendMessage(h, WM_GETTEXT, textLen + 1, byt);
errorText = Encoding.Default.GetString(byt);
// 关闭Error窗口
// SendMessage(h, WM_CLOSE , 0, 0);
}
}
return true;
}, 0);
}
return errorText;
}
/*--Windows API------------------------------------------------------------------------------------*/
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[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);
[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);
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, Byte[] lParam);
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
const int WM_CLOSE = 0x10;
/*--Windows API------------------------------------------------------------------------------------*/
}
网友回复
js如何流式输出ai的回答并折叠代码块,点击代码块右侧可预览代码?
ai大模型如何将文章转换成可视化一目了然的图片流程图图表?
大模型生成html版本的ui原型图和ppt演示文档的系统提示词怎么写?
rtsp视频直播流如何转换成websocket流在h5页面上观看?
为啥coze会开源工作流agent coze studio?
如何检测网页是通过收藏夹打开的?
python如何实现类似php的http动态脚本请求处理响应代码?
js如何实现类似php的http动态脚本请求处理响应代码?
trae与solo有啥区别不同?
vue如何让ai动态生成问卷调查多步骤表单式收集基础信息自动规划执行任务?