using QqHelper;
using System;
using System.Runtime.InteropServices;
namespace QqRead
{
class Program
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
static void Main(string[] args)
{
String winTitle = "聊天标题";
IntPtr hwnd = FindWindow(null, winTitle);
QqWindowHelper a = new QqWindowHelper(hwnd, winTitle);
System.IO.File.WriteAllText("D:\\1.txt", a.GetContent());
}
}
}
using System;
using Accessibility;
namespace QqHelper
{
///
/// 对Messenger窗口进行操作
///
public class QqWindowHelper
{
IntPtr _QqWindowHandle;
string _winTitle;
IAccessible _inputBox;
public QqWindowHelper(IntPtr windowHandle, String winTitle)
{
_QqWindowHandle = windowHandle;
_winTitle = winTitle;
GetAccessibleObjects(_QqWindowHandle, out _inputBox);
}
///
/// 返回消息框内容
///
///
public string GetContent()
{
string value = (string)_inputBox.get_accValue(Win32.CHILDID_SELF);
return value;
}
private IAccessible[] GetAccessibleChildren(IAccessible paccContainer)
{
IAccessible[] rgvarChildren = new IAccessible[paccContainer.accChildCount];
int pcObtained;
Win32.AccessibleChildren(paccContainer, 0, paccContainer.accChildCount, rgvarChildren, out pcObtained);
return rgvarChildren;
}
//按层级找到对象
public IAccessible GetAccessibleChild(IAccessible paccContainer, int[] array)
{
if (array.Length > 0)
{
IAccessible result = GetAccessibleChildren(paccContainer)[array[0]];
int[] array_1 = new int[array.Length - 1];
for (int i = 0; i < array.Length - 1; i++)
{
array_1[i] = array[i + 1];
}
return GetAccessibleChild(result, array_1);
}
else
{
return paccContainer;
}
}
private void GetAccessibleObjects(System.IntPtr imWindowHwnd, out IAccessible inputBox)
{
Guid guidCOM = new Guid(0x618736E0, 0x3C3D, 0x11CF, 0x81, 0xC, 0x0, 0xAA, 0x0, 0x38, 0x9B, 0x71);
Accessibility.IAccessible IACurrent = null;
Win32.AccessibleObjectFromWindow(imWindowHwnd, (int)Win32.OBJID_CLIENT, ref guidCOM, ref IACurrent);
IACurrent = (IAccessible)IACurrent.accParent;
inputBox = null;
inputBox = GetAccessibleChild(IACurrent, new int[] { 3, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 3, 0 });
//int childCount = IACurrent.accChildCount;
//object[] windowChildren = new object[childCount];
//int pcObtained;
//Win32.AccessibleChildren(IACurrent, 0, childCount, windowChildren, out pcObtained);
//foreach (IAccessible child in windowChildren)
//{
// if (child.get_accName(Win32.CHILDID_SELF) == _winTitle)
// {
// inputBox = GetAccessibleChild(child, new int[] { 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 3, 0 });
// break;
// }
//}
}
}
}
using System;
using System.Runtime.InteropServices;
using Accessibility;
namespace QqHelper
{
///
/// 调用Window API
///
public class Win32
{
public const int WM_SETTEXT = 0x000C;
public const int WM_CLICK = 0x00F5;
public const int CHILDID_SELF = 0;
public const int CHILDID_1 = 1;
public const int OBJID_CLIENT = -4;
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(
IntPtr parentHandle,
IntPtr childAfter,
string lpszClass,
int sWindowTitle /*HWND*/);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessageString(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessageInt(IntPtr hwnd, int wMsg, IntPtr wParam, int lParam);
[DllImport("Oleacc.dll")]
public static extern int AccessibleObjectFromWindow(
IntPtr hwnd,
int dwObjectID,
ref Guid refID,
ref IAccessible ppvObject);
[DllImport("Oleacc.dll")]
public static extern int WindowFromAccessibleObject(
IAccessible pacc,
out IntPtr phwnd);
[DllImport("Oleacc.dll")]
public static extern int AccessibleChildren(
Accessibility.IAccessible paccContainer,
int iChildStart,
int cChildren,
[Out] object[] rgvarChildren,
out int pcObtained);
}
}
参考网址:
http://www.vbgood.com/thread-150379-1-1.html
http://blog.csdn.net/tangyanzhi1111/article/details/8294154