MSAA遍历查找元素

浏览:1108 发布日期:2023-06-28 16:21:17

遍历查找一个元素:

IAccessible* WalkTreeWithAccessibleChildren(IAccessible* pAcc, const char* nameStr, int roleId)
{
    HRESULT hr;
    long childCount;
    long returnCount;

    hr = pAcc->get_accChildCount(&childCount);
    VARIANT* pArray = new VARIANT[childCount];
    hr = AccessibleChildren(pAcc, 0L, childCount, pArray, &returnCount);

    IAccessible* pChild = NULL;
    // 遍历子元素
    for (int x = 0; x < returnCount; x++)
    {
        VARIANT vtChild = pArray[x];

        VARIANT v;

        BSTR name;

        VARIANT role;
        VariantInit(&role);
        role.vt = VT_I4;
        role.lVal = CHILDID_SELF;

        // If it's an accessible object, get the IAccessible, and recurse.
        if (vtChild.vt == VT_DISPATCH)
        {
            IDispatch* pDisp = vtChild.pdispVal;

            hr = pDisp->QueryInterface(IID_IAccessible, (void**)&pChild);
            if (hr == S_OK)
            {
                VariantInit(&v);
                v.vt = VT_I4;
                v.lVal = CHILDID_SELF;
                hr = pChild->get_accName(v, &name);
                hr = pChild->get_accRole(v, &role);

                if (name != NULL)
                {
                    char* s = _com_util::ConvertBSTRToString(name);
                    std::cout << "当前name: " << s << ", " << role.lVal << std::endl;
                    if (strcmp(s, nameStr) == 0 && roleId == role.lVal)
                    {
                        std::cout << "找到了元素: " << s << ", " << role.lVal << std::endl;
                        delete s;
                        break;
                    }
                    else
                    {
                        delete s;
                        WalkTreeWithAccessibleChildren(pChild, nameStr, roleId);
                        pChild->Release();
                    }
                }
                else
                {
                    WalkTreeWithAccessibleChildren(pChild, nameStr, roleId);
                    pChild->Release();
                }
            }
            pDisp->Release();
        }
        else // 简单元素
        {
            //std::cout << "简单元素之前, pAcc的地址:" << pAcc << std::endl;
            IDispatch* pDisp;
            hr = pAcc->get_accChild(vtChild, &pDisp);
            //std::cout << "简单元素之后, pAcc的地址:" << pAcc << std::endl;
            //hr = pDisp->QueryInterface(IID_IAccessible, (void**)&pChild);
            //pDisp->Release();
            VariantInit(&v);
            v.vt = VT_I4;
            v.lVal = vtChild.lVal;
            pAcc->get_accName(v, &name);
            pAcc->get_accRole(v, &role);

            if (name != NULL)
            {
                char* s = _com_util::ConvertBSTRToString(name);
                std::cout << "当前name-else: " << s << ", " << role.lVal << std::endl;
                if (strcmp(s, nameStr) == 0 && roleId == role.lVal)
                {

                    {
                        VARIANT v2;
                        VariantInit(&v2);
                        v2.vt = VT_I4;
                        v2.lVal = CHILDID_SELF;
                        BSTR b;
                        hr = pAcc->get_accName(v2, &b);
                        long x33;
                        pAcc->get_accChildCount(&x33);
                        int xxx = 333;
                    }

                    std::cout << "name-else找到了元素: " << s << ", " << role.lVal << std::endl;
                    hr = pAcc->accDoDefaultAction(v);
                    delete s;
                    break;
                }
            }
        }
    }
    delete[] pArray;


    return pChild;
}