[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------------------------------------------------------------------------------------*/
}
网友回复
python如何调用openai的api实现知识讲解类动画讲解视频的合成?
html如何直接调用openai的api实现海报可视化设计及文本描述生成可编辑海报?
f12前端调试如何找出按钮点击事件触发的那段代码进行调试?
abcjs如何将曲谱播放后导出mid和wav格式音频下载?
python如何将曲子文本生成音乐mp3或wav、mid文件
python中mp3、wav音乐如何转成mid格式?
js在HTML中如何将曲谱生成音乐在线播放并下载本地?
python如何实现在windows上通过键盘来模拟鼠标操作?
python如何给win10电脑增加文件或文件夹右键自定义菜单?
python如何将音乐mp3文件解析获取曲调数据?