Overview
The Windows UI Accessibility API provides developers with a set of interfaces and classes to make applications accessible to users with disabilities. It enables assistive technologies such as screen readers, magnifiers, and speech recognition to interact with UI elements programmatically.
Key Interfaces
- IAccessible – The core COM interface for exposing UI element properties and actions.
- IAccessible2 – An extension of IAccessible offering richer information.
- UIAutomationProvider – Provides UI Automation patterns for custom controls.
- UIAutomationClient – Consumes automation patterns to retrieve UI data.
Sample: Exposing a Custom Button
#include <windows.h>
#include <oleacc.h>
class MyButton : public IAccessible {
public:
// IUnknown
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) override {
if (riid == __uuidof(IAccessible) || riid == IID_IUnknown)
*ppvObject = static_cast<IAccessible*>(this);
else {
*ppvObject = nullptr;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
ULONG STDMETHODCALLTYPE AddRef() override { return ++refCount; }
ULONG STDMETHODCALLTYPE Release() override {
if (--refCount == 0) { delete this; return 0; }
return refCount;
}
// IAccessible
HRESULT STDMETHODCALLTYPE get_accName(VARIANT varChild, BSTR *pszName) override {
if (varChild.lVal == CHILDID_SELF) {
*pszName = SysAllocString(L"Submit");
return S_OK;
}
return E_INVALIDARG;
}
HRESULT STDMETHODCALLTYPE get_accRole(VARIANT varChild, VARIANT *pvarRole) override {
if (varChild.lVal == CHILDID_SELF) {
pvarRole->vt = VT_I4;
pvarRole->lVal = ROLE_SYSTEM_PUSHBUTTON;
return S_OK;
}
return E_INVALIDARG;
}
// Implement other methods as needed...
private:
LONG refCount = 1;
};
Compile and register the COM object to make the button discoverable by assistive technologies.
Accessibility Patterns
| Pattern | Description |
|---|---|
| Invoke | Enables a control to be invoked (e.g., button click). |
| Selection | Allows selection of items within a container. |
| Value | Provides a numeric or textual value of a control. |
| ExpandCollapse | Supports expanding or collapsing a container. |
| Toggle | Represents a binary on/off state. |
Testing Your UI
Use the following tools to verify accessibility compliance: