C# 实现IAccessible学习(四)读取QQ聊天窗口记录

代码

类:

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());
        }
    }
}

类:QqWindowHelper

using System;
using Accessibility;

namespace QqHelper
{
    /// <summary>
    /// 对Messenger窗口进行操作
    /// </summary>
    public class QqWindowHelper
    {
        IntPtr _QqWindowHandle;
        string _winTitle;
        IAccessible _inputBox;

        public QqWindowHelper(IntPtr windowHandle, String winTitle)
        {
            _QqWindowHandle = windowHandle;
            _winTitle = winTitle;

            GetAccessibleObjects(_QqWindowHandle, out _inputBox);
        }

        /// <summary>
        /// 返回消息框内容
        /// </summary>
        /// <returns></returns>
        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;
            //    }
            //}
        }
    }
}

Win32类

using System;
using System.Runtime.InteropServices;

using Accessibility;

namespace QqHelper
{
    /// <summary>
    /// 调用Window API
    /// </summary>
    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

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程圈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值