Microsoft Active Accessibility API (IAccessible)

oleacc.dll
To call these APIs from .NET you need to adopt platform invoke technology ?


The IAccessible interface is a managed wrapper for the COM IAccessible interface
This is a collection of methods that expose the most common attributes and behaviour of a wide range of UI elements.
Nothing in the object model
Cannot use simple Win32 APIs as the ribbon is not recognised as a window
Microsoft Active Accessibility is Microsoft's user interface technology.
This is a COM based technology that provides the IAccessible interface


The MSAA layer provides some abstraction from the Win32 APIs
The IAccessible interface searches the whole tree starting at the root node which is the Desktop.


There is a tool from Microsoft called the Accessibility Explorer.


CommandBars("Ribbon") returns an IAccessible object 

    [ComVisible(true)] 
    public sealed class TabsHandler
    {
        // Retrieves the address of the specified interface for the object associated with the specified window.
        [System.Runtime.InteropServices.DllImport("oleacc.dll", PreserveSig = false, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Interface)]
        private static extern object AccessibleObjectFromWindow(
             IntPtr hwnd, uint id, ref Guid iid);


        // Retrieves the child ID or IDispatch of each child within an accessible container object.
        [System.Runtime.InteropServices.DllImport("oleacc.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        private static extern int AccessibleChildren(
            IAccessible paccContainer,
            int iChildStart,
            int cChildren,
            [System.Runtime.InteropServices.Out()] [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPArray, SizeParamIndex = 4)]
            object[] rgvarChildren,
            ref int pcObtained);


        private IAccessible[] GetAccessibleChildren(IAccessible accContainer)
        {
            // Get the number of child interfaces that belong to this object.
            int childNum = 0;
            try
            {
                childNum = accContainer.accChildCount;
            }
            catch (Exception ex)
            {
                childNum = 0;
                System.Diagnostics.Debug.Print(ex.Message);
            }

            // Get the child accessible objects.
            IAccessible[] accObjects = new IAccessible[childNum];
            int count = 0;
            if (childNum != 0)
            {
                AccessibleChildren(accContainer, 0, childNum,
                    accObjects, ref count);
            }
            return accObjects;
        }


        private IAccessible GetObjectByName(IAccessible Parent, string Name)
        {
            // Return null if Parent is Null or not a COM Object
            if (Parent == null || !Marshal.IsComObject(Parent))
            {
                return null;
            }

            // Return the Parent if the parent matches the name
            if (Parent.accName[0] != null && Parent.accName[0].Equals(Name))
            {
                return Parent;
            }

            // Recursively check the child objects
            IAccessible[] children = GetAccessibleChildren(Parent);
            foreach (IAccessible child in children)
            {
                IAccessible objAcc = GetObjectByName(child, Name);
                if (objAcc != null) return objAcc;
            }

            // If we're still here then return null
            return null;
        }

        public void ActivateTab(string Command)
        {
            try
            {
                IntPtr hwnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
                Guid guidIAccessible = new Guid("{618736E0-3C3D-11CF-810C-00AA00389B71}");
                IAccessible wordObject = (IAccessible)AccessibleObjectFromWindow(hwnd, (uint)0, ref guidIAccessible);
                IAccessible tabObject = GetObjectByName(wordObject, Command);
                if (tabObject != null) tabObject.accDoDefaultAction(0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



© 2018 Better Solutions Limited. All Rights Reserved. © 2018 Better Solutions Limited TopPrevNext