1 
2 #include "resource.h"
3 #include <initguid.h>
4 #include "objbase.h"
5 #include "uiautomation.h"
6 #include "accessibility_props_service.h"
7 
8 static IAccPropServices* _pAccPropServices = NULL;
9 
10 // Create accessibility property service
CreateAccPropService()11 void CreateAccPropService()
12 {
13    CoCreateInstance(
14         CLSID_AccPropServices,
15         nullptr,
16         CLSCTX_INPROC,
17         IID_PPV_ARGS(&_pAccPropServices));
18 }
19 
20 // Release accessibility property service
ReleaseAccPropService()21 void ReleaseAccPropService()
22 {
23     if (_pAccPropServices != nullptr)
24     {
25         _pAccPropServices->Release();
26         _pAccPropServices = NULL;
27     }
28 }
29 
30 
31 // Run when the UI is created.
SetControlAccessibleName(HWND ctr_handle,LPCWSTR name)32 void SetControlAccessibleName(HWND ctr_handle, LPCWSTR name)
33 {
34     if (_pAccPropServices != nullptr)
35     {
36         // Now set the name on the edit control. This gets exposed through UIA as the
37         // element's Name property.
38         _pAccPropServices->SetHwndPropStr(
39             ctr_handle,
40             OBJID_CLIENT,
41             CHILDID_SELF,
42             Name_Property_GUID,
43             name);
44     }
45 }
46 
47 // Annotate role property
SetControlAccessibleRole(HWND ctr_handle,LONG role)48 void SetControlAccessibleRole(HWND ctr_handle, LONG role)
49 {
50     if (_pAccPropServices != nullptr)
51     {
52         // Annotating the Role of this object to be STATICTEXT
53         VARIANT var;
54         var.vt = VT_I4;
55         var.lVal = role;
56 
57         _pAccPropServices->SetHwndProp(ctr_handle,
58             OBJID_CLIENT,
59             CHILDID_SELF,
60             PROPID_ACC_ROLE,
61             var);
62     }
63 }
64 
65 // Set accessible help string for a control
SetAccessibleHelpString(HWND ctr_handle,LPCWSTR help_string)66 void SetAccessibleHelpString(HWND ctr_handle, LPCWSTR help_string)
67 {
68     if (_pAccPropServices != nullptr)
69     {
70         // Expose the help string through UIA as the element's HelpText property.
71         _pAccPropServices->SetHwndPropStr(
72             ctr_handle,
73             OBJID_CLIENT,
74             CHILDID_SELF,
75             PROPID_ACC_HELP,
76             help_string);
77     }
78 }
79 
80 // Set a lable to be a LiveRegion
SetLiveRegion(HWND ctr_handle)81 void SetLiveRegion(HWND ctr_handle)
82 {
83     if (_pAccPropServices != nullptr)
84     {
85         // Set up the status label to be an assertive LiveRegion. The assertive property
86             // is used to request that the screen reader announces the update immediately.
87             // And alternative setting of polite requests that the screen reader completes
88             // any in-progress announcement before announcing the LiveRegion update.
89 
90         VARIANT var;
91         var.vt = VT_I4;
92         var.lVal = Assertive;
93 
94        _pAccPropServices->SetHwndProp(
95             ctr_handle,
96             OBJID_CLIENT,
97             CHILDID_SELF,
98             LiveSetting_Property_GUID,
99             var);
100     }
101 }
102 
103 // Set accessible full description for a control
SetAccessibleFullDescription(HWND ctr_handle,LPCWSTR string)104 void SetAccessibleFullDescription(HWND ctr_handle, LPCWSTR string)
105 {
106     if (_pAccPropServices != nullptr)
107     {
108         // Expose a localized string which can contain extended description text for an element.
109         // FullDescription can contain a more complete description of an element than maybe
110         // appropriate for the element name.
111         _pAccPropServices->SetHwndPropStr(
112             ctr_handle,
113             OBJID_CLIENT,
114             CHILDID_SELF,
115             FullDescription_Property_GUID,
116             string);
117     }
118 }
119 
120 // Set accessible description string for a control
SetAccessibleDescription(HWND ctr_handle,LPCWSTR help_string)121 void SetAccessibleDescription(HWND ctr_handle, LPCWSTR help_string)
122 {
123     if (_pAccPropServices != nullptr)
124     {
125         // Expose the descriotion through UIA as the element's Description property.
126         _pAccPropServices->SetHwndPropStr(
127             ctr_handle,
128             OBJID_CLIENT,
129             CHILDID_SELF,
130             PROPID_ACC_DESCRIPTION,
131             help_string);
132     }
133 }