Fortunately, starting with Windows 7 there is an easier alternative, the Direct Annotation feature of the Dynamic Annotation API. The Dynamic Annotation API is extension to Microsoft Active Accessibility that makes it possible to customize existing IAccessible support without using subclassing or wrapping techniques. Direct Annotation is one of several features provided by the Dynamic Annotation API; it is the simplest of the features provided.
Direct Annotation makes it possible to customize the following properties.
Since Name, Role, Value, and State are included in the list of properties supported by the Direct Annotation feature, this feature is sufficient to meet WCAG Success Criterion 4.1.2: Name, Role, Value.
Implementation Details
The first step to using the Direct Annotation feature of the Dynamic Annotation API is to create an [IAccPropServices][] object using either the CoCreateInstance function or CoCreateInstanceEx function. Then you can use either the IAccPropServices::SetHwndProp method or the IAccPropServices::SetHwndPropStr method.
The following sample from Using Direct Annotation demonstrates the technique.
HRESULT CMyTextControl::SetAccessibleProperties()
{
// COM is assumed to be initialized...
IAccPropServices* pAccPropServices = NULL;
HRESULT hr = CoCreateInstance(
CLSID_AccPropServices, nullptr,
CLSCTX_SERVER, IID_IAccPropServices,
reinterpret_cast<void**>(&pAccPropServices));
if (SUCCEEDED(hr))
{
// Annotating the Role of this object to be STATICTEXT
VARIANT var;
var.vt = VT_I4;
var.lVal = ROLE_SYSTEM_STATICTEXT;
hr = pAccPropServices->SetHwndProp(
_hwnd, OBJID_CLIENT, CHILDID_SELF,
PROPID_ACC_ROLE, var);
pAccPropServices->Release();
}
return hr;
}
More complete examples can be found in the following locations.
- WndWithCustomizedAccessibleInfo.cpp and WndWithCustomizedAccessibleInfo.h
Implements the CWndWithCustomizedAccessibleInfo class, a ATL/MFC class that uses Direct Annotation to customize the accessible info of a window.
- DirectAnnotation.cpp and DirectAnnotation.h
Implements the DirectAnnotation class, a Window API class that uses Direct Annotation to customize the accessible info of a window.
Note that using the IAccPropServices::SetHwndProp method or the IAccPropServices::SetHwndPropStr method will not send WinEvents. It is your responsibility to send appropriate events by calling the NotifyWinEvent function after the IAccPropServices::SetHwndProp method or the IAccPropServices::SetHwndPropStr method has been called.
For example, if you use IAccPropServices::SetHwndPropStr method to set the Name property of an element, send an EVENT_OBJECT_NAMECHANGE event for that object after SetHwndPropStr returns.
The CWndWithCustomizedAccessibleInfo class and DirectAnnotation class both make the necessary calls to the NotifyWinEvent function for you.