想获取的文本信息如上图所示。
我自己写的获取文本信息的代码如下:
//获取控件句柄
public void getCtlHwd(IntPtr hwd, ref IntPtr[] allHd)
{
IntPtr[] hd = new IntPtr[1];
hd[0] = WinApi.FindWindowEx(hwd, IntPtr.Zero, null, null);
while (true)
{
if (hd[0] != IntPtr.Zero)
{
allHd = allHd.Concat(hd).ToArray();
getCtlHwd(hd[0], ref allHd);
}
else
{
break;
} hd[0] = WinApi.FindWindowEx(hwd, hd[0], null, null);
}
}//获取文本信息的部分代码
IntPtr[] allHd = new IntPtr[0];
IntPtr[] Hd = new IntPtr[0];
getCtlHwd(pt, ref allHd);
StringBuilder sb = new StringBuilder(512);
for (int i = 0; i < allHd.Length; i++)
{
int ii = WinApi.GetWindowText(allHd[i], sb, sb.Capacity);
if (ii != 0 && sb.ToString().Contains("Command"))
{
getCtlHwd(allHd[i], ref Hd);
foreach (var item in Hd)
{
//使用GetWindowText得到的sb.ToString()结果为空
ii = WinApi.GetWindowText(Hd[i], sb, sb.Capacity);
}
}
然后向他发WM_GETTEXT消息
不知道你的GetWindowText是怎么实现的
而且ref []太丑陋了,用List不好吗
[DllImport("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(
IntPtr hWnd,
StringBuilder lpString,
int nMaxCount
);