AccessibleObject 类

定义

提供辅助功能应用程序用于为残障用户调整应用程序的用户界面 (UI) 的信息。

C#
[System.Runtime.InteropServices.ComVisible(true)]
public class AccessibleObject : MarshalByRefObject, Accessibility.IAccessible, System.Reflection.IReflect
C#
[System.Runtime.InteropServices.ComVisible(true)]
public class AccessibleObject : System.Runtime.InteropServices.StandardOleMarshalObject, Accessibility.IAccessible, System.Reflection.IReflect
C#
public class AccessibleObject : System.Runtime.InteropServices.StandardOleMarshalObject, Accessibility.IAccessible, System.Reflection.IReflect
继承
AccessibleObject
继承
派生
属性
实现

示例

下面的代码示例演示如何创建辅助功能感知图表控件,并使用 AccessibleObjectControl.ControlAccessibleObject 类公开可访问的信息。 控件绘制两条曲线以及一个图例。 ChartControlAccessibleObject派生自 ControlAccessibleObject的 类在 方法中CreateAccessibilityInstance用于为图表控件提供自定义可访问信息。 由于图表图例不是基于实际 Control 控件的控件,而是由图表控件绘制的,因此它没有任何内置的可访问信息。 因此, ChartControlAccessibleObject 类重写 GetChild 方法,以返回 CurveLegendAccessibleObject 表示图例每个部分的可访问信息的 。 当可访问的应用程序使用此控件时,该控件可以提供必要的可访问信息。

C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ChartControl
{
    public class Form1 : System.Windows.Forms.Form
    {
        // Test out the Chart Control.
        private ChartControl chart1;

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        public Form1() {
            // Create a chart control and add it to the form.
            this.chart1 = new ChartControl();
            this.ClientSize = new System.Drawing.Size(920, 566);

            this.chart1.Location = new System.Drawing.Point(47, 16);
            this.chart1.Size = new System.Drawing.Size(600, 400);

            this.Controls.Add(this.chart1);
        }
    }

    // Declare a chart control that demonstrates accessibility in Windows Forms.
    public class ChartControl : System.Windows.Forms.UserControl
    {
        private CurveLegend legend1;
        private CurveLegend legend2; 

        public ChartControl()
        {
            // The ChartControl draws the chart in the OnPaint override.
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            this.BackColor = System.Drawing.Color.White;
            this.Name = "ChartControl";

            this.Click += new System.EventHandler(this.ChartControl_Click);
            this.QueryAccessibilityHelp += 
                new System.Windows.Forms.QueryAccessibilityHelpEventHandler(
                                        this.ChartControl_QueryAccessibilityHelp);

            // The CurveLengend is not Control-based, it just
            // represents the parts of the legend.
            legend1 = new CurveLegend(this, "A");
            legend1.Location = new Point(20, 30);
            legend2 = new CurveLegend(this, "B");        
            legend2.Location = new Point(20, 50);
        }

        // Overridden to return the custom AccessibleObject 
        // for the entire chart.
        protected override AccessibleObject CreateAccessibilityInstance() 
        {            
            return new ChartControlAccessibleObject(this);
        }

        protected override void OnPaint(PaintEventArgs e) 
        {
            // The ChartControl draws the chart in the OnPaint override.
            base.OnPaint(e);

            Rectangle bounds = this.ClientRectangle;
            int border = 5;

            // Draws the legends first.
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            
            if (legend1 != null) {
                if (legend1.Selected) {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Blue), legend1.Bounds);
                } else {
                    e.Graphics.DrawRectangle(Pens.Blue, legend1.Bounds);
                }

                e.Graphics.DrawString(legend1.Name, this.Font, Brushes.Black, legend1.Bounds, format);                
            }
            if (legend2 != null) {
                if (legend2.Selected) {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Red), legend2.Bounds);
                } else {
                    e.Graphics.DrawRectangle(Pens.Red, legend2.Bounds);
                }
                e.Graphics.DrawString(legend2.Name, this.Font, Brushes.Black, legend2.Bounds, format);
            }            

            // Charts out the actual curves that represent data in the Chart.
            bounds.Inflate(-border, -border);
            Point[] curve1 = new Point[] {new Point(bounds.Left, bounds.Bottom),
                            new Point(bounds.Left + bounds.Width / 3, bounds.Top + bounds.Height / 5),
                            new Point(bounds.Right - bounds.Width / 3, (bounds.Top + bounds.Bottom) / 2),
                            new Point(bounds.Right, bounds.Top)};

            Point[] curve2 = new Point[] {new Point(bounds.Left, bounds.Bottom - bounds.Height / 3),
                            new Point(bounds.Left + bounds.Width / 3, bounds.Top + bounds.Height / 5),
                            new Point(bounds.Right - bounds.Width / 3, (bounds.Top + bounds.Bottom) / 2),
                            new Point(bounds.Right, bounds.Top + bounds.Height / 2)};

            // Draws the actual curve only if it is selected.
            if (legend1.Selected) e.Graphics.DrawCurve(Pens.Blue, curve1);
            if (legend2.Selected) e.Graphics.DrawCurve(Pens.Red, curve2);

            e.Graphics.DrawRectangle(Pens.Blue, bounds);            
        }

        // Handles the QueryAccessibilityHelp event.
        private void ChartControl_QueryAccessibilityHelp(object sender, 
                                    System.Windows.Forms.QueryAccessibilityHelpEventArgs e)
        {            
            e.HelpString = "Displays chart data";
        }          

        // Handles the Click event for the chart. 
        // Toggles the selection of whatever legend was clicked on
        private void ChartControl_Click(object sender, System.EventArgs e)
        {
            Point pt = this.PointToClient(Control.MousePosition);
            if (legend1.Bounds.Contains(pt)) {
                legend1.Selected = !legend1.Selected;
            } else if (legend2.Bounds.Contains(pt)) {
                legend2.Selected = !legend2.Selected;
            }
        }

        // Gets an array of CurveLengends used in the Chart.
        public CurveLegend[] Legends
        {   
            get {                
                return new CurveLegend[] { legend1, legend2 };
            }            
        }                

        // Inner class ChartControlAccessibleObject represents accessible information associated with the ChartControl.
        // The ChartControlAccessibleObject is returned in the ChartControl.CreateAccessibilityInstance override.
        public class ChartControlAccessibleObject : ControlAccessibleObject
        {
            ChartControl chartControl;

            public ChartControlAccessibleObject(ChartControl ctrl) : base(ctrl) 
            {
                chartControl = ctrl;
            }

            // Gets the role for the Chart. This is used by accessibility programs.
            public override AccessibleRole Role
            {  
                get {
                    return AccessibleRole.Chart;
                }
            }

            // Gets the state for the Chart. This is used by accessibility programs.
            public override AccessibleStates State
            {  
                get {                    
                    return AccessibleStates.ReadOnly;
                }
            }

            // The CurveLegend objects are "child" controls in terms of accessibility so 
            // return the number of ChartLengend objects.
            public override int GetChildCount()
            {  
                return chartControl.Legends.Length;
            }

            // Gets the Accessibility object of the child CurveLegend idetified by index.
            public override AccessibleObject GetChild(int index)
            {  
                if (index >= 0 && index < chartControl.Legends.Length) {
                    return chartControl.Legends[index].AccessibilityObject;
                }                
                return null;
            }

            // Helper function that is used by the CurveLegend's accessibility object
            // to navigate between sibiling controls. Specifically, this function is used in
            // the CurveLegend.CurveLegendAccessibleObject.Navigate function.
            internal AccessibleObject NavigateFromChild(CurveLegend.CurveLegendAccessibleObject child, 
                                                        AccessibleNavigation navdir) 
            {  
                switch(navdir) {
                    case AccessibleNavigation.Down:
                    case AccessibleNavigation.Next:
                        return GetChild(child.ID + 1);
                        
                    case AccessibleNavigation.Up:
                    case AccessibleNavigation.Previous:
                        return GetChild(child.ID - 1);                        
                }
                return null;
            }

            // Helper function that is used by the CurveLegend's accessibility object
            // to select a specific CurveLegend control. Specifically, this function is used
            // in the CurveLegend.CurveLegendAccessibleObject.Select function.
            internal void SelectChild(CurveLegend.CurveLegendAccessibleObject child, AccessibleSelection selection) 
            {   
                int childID = child.ID;

                // Determine which selection action should occur, based on the
                // AccessibleSelection value.
                if ((selection & AccessibleSelection.TakeSelection) != 0) {
                    for(int i = 0; i < chartControl.Legends.Length; i++) {
                        if (i == childID) {
                            chartControl.Legends[i].Selected = true;                        
                        } else {
                            chartControl.Legends[i].Selected = false;
                        }
                    }

                    // AccessibleSelection.AddSelection means that the CurveLegend will be selected.
                    if ((selection & AccessibleSelection.AddSelection) != 0) {
                        chartControl.Legends[childID].Selected = true;                        
                    }

                    // AccessibleSelection.AddSelection means that the CurveLegend will be unselected.
                    if ((selection & AccessibleSelection.RemoveSelection) != 0) {
                        chartControl.Legends[childID].Selected = false;                        
                    }
                }            
            }
        }

        // Inner Class that represents a legend for a curve in the chart.
        public class CurveLegend 
        {
            private string name;
            private ChartControl chart;
            private CurveLegendAccessibleObject accObj;
            private bool selected = true;
            private Point location;

            public CurveLegend(ChartControl chart, string name) 
            {
                this.chart = chart;
                this.name = name;
            }

            // Gets the accessibility object for the curve legend.
            public AccessibleObject AccessibilityObject
            {
                get
                {
                    accObj ??= new CurveLegendAccessibleObject(this);
                    return accObj;
                }
            }
            
            // Gets the bounds for the curve legend.
            public Rectangle Bounds
            {   
                get
                {
                    return new Rectangle(Location, Size);
                }
            }

            // Gets or sets the location for the curve legend.
            public Point Location
            {   
                get {
                    return location;
                }
                set {
                    location = value;
                    chart.Invalidate();

                    // Notifies the chart of the location change. This is used for
                    // the accessibility information. AccessibleEvents.LocationChange
                    // tells the chart the reason for the notification.

                    chart.AccessibilityNotifyClients(AccessibleEvents.LocationChange, 
                        ((CurveLegendAccessibleObject)AccessibilityObject).ID);
                }
            }            
        
            // Gets or sets the Name for the curve legend.
            public string Name
            {   
                get {
                    return name;
                }
                set {
                    if (name != value) 
                    {
                        name = value;
                        chart.Invalidate();

                        // Notifies the chart of the name change. This is used for
                        // the accessibility information. AccessibleEvents.NameChange
                        // tells the chart the reason for the notification.

                        chart.AccessibilityNotifyClients(AccessibleEvents.NameChange, 
                            ((CurveLegendAccessibleObject)AccessibilityObject).ID);
                    }
                }
            }

            // Gets or sets the Selected state for the curve legend.
            public bool Selected
            {   
                get {
                    return selected;
                }
                set {
                    if (selected != value) 
                    {
                        selected = value;
                        chart.Invalidate();

                        // Notifies the chart of the selection value change. This is used for
                        // the accessibility information. The AccessibleEvents value depends upon
                        // if the selection is true (AccessibleEvents.SelectionAdd) or 
                        // false (AccessibleEvents.SelectionRemove).
                        chart.AccessibilityNotifyClients(
                            selected ? AccessibleEvents.SelectionAdd : AccessibleEvents.SelectionRemove, 
                            ((CurveLegendAccessibleObject)AccessibilityObject).ID);
                    }
                }
            }

            // Gets the Size for the curve legend.
            public Size Size
            {   
                get {                    
                    int legendHeight = chart.Font.Height + 4;
                    Graphics g = chart.CreateGraphics();
                    int legendWidth = (int)g.MeasureString(Name, chart.Font).Width + 4;            

                    return new Size(legendWidth, legendHeight);
                }
            }
    
            // Inner class CurveLegendAccessibleObject represents accessible information 
            // associated with the CurveLegend object.
            public class CurveLegendAccessibleObject : AccessibleObject
            {
                private CurveLegend curveLegend;

                public CurveLegendAccessibleObject(CurveLegend curveLegend) : base() 
                {
                    this.curveLegend = curveLegend;                    
                }                

                // Private property that helps get the reference to the parent ChartControl.
                private ChartControlAccessibleObject ChartControl
                {   
                    get {
                        return Parent as ChartControlAccessibleObject;
                    }
                }

                // Internal helper function that returns the ID for this CurveLegend.
                internal int ID
                {
                    get {
                        for(int i = 0; i < ChartControl.GetChildCount(); i++) {
                            if (ChartControl.GetChild(i) == this) {
                                return i;
                            }
                        }
                        return -1;
                    }
                }

                // Gets the Bounds for the CurveLegend. This is used by accessibility programs.
                public override Rectangle Bounds
                {
                    get {                        
                        // The bounds is in screen coordinates.
                        Point loc = curveLegend.Location;
                        return new Rectangle(curveLegend.chart.PointToScreen(loc), curveLegend.Size);
                    }
                }

                // Gets or sets the Name for the CurveLegend. This is used by accessibility programs.
                public override string Name
                {
                    get {
                        return curveLegend.Name;
                    }
                    set {
                        curveLegend.Name = value;                        
                    }
                }

                // Gets the Curve Legend Parent's Accessible object.
                // This is used by accessibility programs.
                public override AccessibleObject Parent
                {
                    get {
                        return curveLegend.chart.AccessibilityObject;
                    }
                }

                // Gets the role for the CurveLegend. This is used by accessibility programs.
                public override AccessibleRole Role 
                {
                    get {
                        return AccessibleRole.StaticText;
                    }
                }

                // Gets the state based on the selection for the CurveLegend. 
                // This is used by accessibility programs.
                public override AccessibleStates State 
                {
                    get {
                        AccessibleStates state = AccessibleStates.Selectable;
                        if (curveLegend.Selected) 
                        {
                            state |= AccessibleStates.Selected;
                        }
                        return state;
                    }
                }

                // Navigates through siblings of this CurveLegend. This is used by accessibility programs.
                public override AccessibleObject Navigate(AccessibleNavigation navdir) 
                {
                    // Uses the internal NavigateFromChild helper function that exists
                    // on ChartControlAccessibleObject.
                    return ChartControl.NavigateFromChild(this, navdir);
                }

                // Selects or unselects this CurveLegend. This is used by accessibility programs.
                public override void Select(AccessibleSelection selection) 
                {
                    // Uses the internal SelectChild helper function that exists
                    // on ChartControlAccessibleObject.
                    ChartControl.SelectChild(this, selection);
                }
            }
        }
    }
}

注解

辅助功能应用程序可以调整应用程序的功能,以提高残障用户的可用性。

对于视力受损的用户,你可以调整软件和操作系统功能以满足其需求。 例如,可以放大文本和图像,并使用对比度呈现它们。 此外,还可以通过适当使用颜色来适应色盲。 对于严重视力受损的用户,计算机可通过屏幕审查辅助功能进行访问,这些辅助工具将屏幕上的文本转换为语音或动态、可刷新的盲文显示器。

对于有听力困难的用户,你可以设计使用视觉提示的程序,如闪烁工具栏;或者,可以将口述消息显示为文本。 例如,启用SoundSentry该功能(控制面板中的辅助功能选项)在系统发出警报声音时提供视觉警告。

对于有运动障碍的用户,你可以设计控件来优化或消除键盘和鼠标的使用,从而提高计算机的辅助功能。 控制面板提供帮助。 例如,一种替代方法是使用数字键盘而不是鼠标进行导航。 另一个选项称为 StickyKeys,使不能一次按住两个或更多键的用户 ((如 CTRL+P) )一次键入一个键即可获得相同的结果。

对于有认知和语言障碍的用户,你可以设计软件程序以更好地满足其需求。 例如,使用显眼或提示的排序、简单显示、更少的单词和针对小学标准的阅读水平,这些用户都受益匪浅。

对于有癫痫发作障碍的用户,你可以设计软件程序来消除癫痫发作诱发模式。

有关辅助功能的详细信息,包括有关辅助功能应用程序的信息,请参阅 Windows 辅助功能

备注

若要使用 AccessibleObject,必须添加对Accessibility随 .NET Framework安装的程序集的引用。 Windows 窗体仅支持 Active Accessibility 2.0。

继承者说明

从此类继承时,可以重写所有成员。

构造函数

AccessibleObject()

初始化 AccessibleObject 类的新实例。

属性

Bounds

获取辅助性对象的位置和大小。

DefaultAction

获取描述对象默认操作的字符串。 并非所有对象都有默认操作。

Description

获取说明指定对象可视外观的字符串。 并非所有对象都有说明。

Help

获取对象用途或如何使用对象的说明。

KeyboardShortcut

获取辅助性对象的快捷键或访问键。

Name

获取或设置对象名。

Parent

获取辅助性对象的父级。

Role

获取此辅助性对象的角色。

State

获取此辅助性对象的状态。

Value

获取或设置辅助性对象的值。

方法

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
DoDefaultAction()

执行与该辅助性对象相关的默认操作。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetChild(Int32)

检索与指定索引对应的可访问子级。

GetChildCount()

检索属于可访问对象的子级的个数。

GetFocused()

检索具有键盘焦点的对象。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetHelpTopic(String)

获取帮助主题标识符及与该辅助性对象相关的帮助文件的路径。

GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetSelected()

检索当前选定的子级。

GetType()

获取当前实例的 Type

(继承自 Object)
HitTest(Int32, Int32)

检索位于指定屏幕坐标的子对象。

InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
Navigate(AccessibleNavigation)

定位到另一个辅助性对象。

RaiseAutomationNotification(AutomationNotificationKind, AutomationNotificationProcessing, String)

引发 UI 自动化通知事件。

RaiseLiveRegionChanged()

引发 LiveRegionChanged UI 自动化事件。

Select(AccessibleSelection)

修改选择内容或移动辅助性对象的键盘焦点。

ToString()

返回表示当前对象的字符串。

(继承自 Object)
UseStdAccessibleObjects(IntPtr)

根据对象的句柄,将一个对象与 AccessibleObject 的一个实例关联起来。

UseStdAccessibleObjects(IntPtr, Int32)

根据对象的句柄和对象 ID,将一个对象与 AccessibleObject 的一个实例关联起来。

显式接口实现

IAccessible.accChildCount

获取属于该对象的子接口的数量。 有关此成员的说明,请参见 accChildCount

IAccessible.accDoDefaultAction(Object)

执行指定对象的默认操作。 并非所有对象都有默认操作。 有关此成员的说明,请参见 accDoDefaultAction(Object)

IAccessible.accFocus

获取具有键盘焦点的对象。 有关此成员的说明,请参见 accFocus

IAccessible.accHitTest(Int32, Int32)

在指定的屏幕坐标位置获取子对象。 有关此成员的说明,请参见 accHitTest(Int32, Int32)

IAccessible.accLocation(Int32, Int32, Int32, Int32, Object)

获取对象的当前屏幕位置。 有关此成员的说明,请参见 accLocation(Int32, Int32, Int32, Int32, Object)

IAccessible.accNavigate(Int32, Object)

相对于当前对象定位到一个辅助性对象。 有关此成员的说明,请参见 accNavigate(Int32, Object)

IAccessible.accParent

获取该对象的父辅助性对象。 有关此成员的说明,请参见 accParent

IAccessible.accSelect(Int32, Object)

修改选择内容或移动辅助性对象的键盘焦点。 有关此成员的说明,请参见 accSelect(Int32, Object)

IAccessible.accSelection

获取辅助性对象的选定子对象。 有关此成员的说明,请参见 accSelection

IReflect.GetField(String, BindingFlags)

获取与指定字段和绑定标志对应的 FieldInfo 对象。 有关此成员的说明,请参见 GetField(String, BindingFlags)

IReflect.GetFields(BindingFlags)

获取与当前类的所有字段对应的 FieldInfo 对象的数组。 有关此成员的说明,请参见 GetFields(BindingFlags)

IReflect.GetMember(String, BindingFlags)

获取 MemberInfo 对象数组,这些对象对应于所有公共成员或与指定的名称匹配的所有成员。 有关此成员的说明,请参见 GetMember(String, BindingFlags)

IReflect.GetMembers(BindingFlags)

获取 MemberInfo 对象数组,这些对象对应于所有公共成员或者当前类的所有成员。 有关此成员的说明,请参见 GetMembers(BindingFlags)

IReflect.GetMethod(String, BindingFlags)

在指定的搜索约束下获取与指定方法对应的 MethodInfo 对象。 有关此成员的说明,请参见 GetMethod(String, BindingFlags)

IReflect.GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

获取与指定方法对应的 MethodInfo 对象(使用类型数组从重载方法中进行选择)。 有关此成员的说明,请参见 GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

IReflect.GetMethods(BindingFlags)

获取 MethodInfo 对象的数组,其中包含了所有公共方法或当前类的所有方法。 有关此成员的说明,请参见 GetMethods(BindingFlags)

IReflect.GetProperties(BindingFlags)

获取 PropertyInfo 对象数组,这些对象对应于所有公共属性或当前类的所有属性。 有关此成员的说明,请参见 GetProperties(BindingFlags)

IReflect.GetProperty(String, BindingFlags)

在指定的搜索约束下获取与指定属性对应的 PropertyInfo 对象。 有关此成员的说明,请参见 GetProperty(String, BindingFlags)

IReflect.GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

在指定的搜索约束下获取与指定属性对应的 PropertyInfo 对象。 有关此成员的说明,请参见 GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

IReflect.InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])

调用指定的成员。 有关此成员的说明,请参见 InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])

IReflect.UnderlyingSystemType

获取表示 IReflect 对象的基础类型。 有关此成员的说明,请参见 UnderlyingSystemType

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9