January 14th, 2026
like1 reaction

Clipping the focus item when looking for its on-screen location, part 3

Last time, we clipped the focus item to the accessible parent so that we considered only the visible portion of the item. But we found that this failed to clip some items, such as items in File Explorer. What’s going on?

The reason is that the parent of the clipped item is a wrapper and not the Tiles view container. We really need to intersect the item with parent elements all the way up the tree to get it fully clipped.

bool SetCursorPosToLocation(IAccessible* acc, LONG childId)
{
    RECT rcObject;
    if (GetAccessibleBounds(acc, childId, &rcObject)) {
        RECT rcParent;
        if (childId != CHILDID_SELF) {
            if (GetAccessibleBounds(acc, CHILDID_SELF, &rcParent)) {
                IntersectRect(&rcObject, &rcObject, &rcParent);
            }
        } else {
            wil::com_ptr_nothrow<IAccessible> accParent(acc);
            wil::com_ptr_nothrow<IDispatch> dispParent;
            while (accParent->get_accParent(dispParent.put()) == S_OK &&
                dispParent) {
                /* auto */ accParent = dispParent.try_query<IAccessible>();
                if (accParent &&
                    GetAccessibleBounds(accParent.get(), CHILDID_SELF,
                                        &rcParent)) {
                    IntersectRect(&rcObject, &rcObject, &rcParent);
                }
            }
        }
        SetCursorPos(rcObject.right - 1, rcObject.bottom - 1);
        return true;
    }
    return false;
}

I’m going to stop here because this seems to work reasonably well, except of course for the programs I noted earlier that simply refuse to report the caret position at all. I’m not sure what to do about those guys.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments