UI 自動化控制項模式概觀UI Automation Control Patterns Overview


這份文件適用於想要使用 UI 自動化UI Automation 命名空間中定義之 Managed System.Windows.Automation 類別的 .NET Framework 開發人員。This documentation is intended for .NET Framework developers who want to use the managed UI 自動化UI Automation classes defined in the System.Windows.Automation namespace. 如需 UI 自動化UI Automation的最新資訊,請參閱 Windows Automation API:UI 自動化For the latest information about UI 自動化UI Automation, see Windows Automation API: UI Automation.

本概觀介紹 Microsoft UI 自動化Microsoft UI Automation 控制項模式。This overview introduces Microsoft UI 自動化Microsoft UI Automation control patterns. 控制項模式提供一種方式,分類及公開與控制項類型或控制項外觀無關的控制項功能。Control patterns provide a way to categorize and expose a control's functionality independent of the control type or the appearance of the control.

UI 自動化UI Automation 使用控制項模式來代表通用控制項的行為。uses control patterns to represent common control behaviors. 例如,您可以使用叫用控制項模式,用於可以叫用 (例如按鈕) 的控制項;也可以使用捲軸控制項模式,用於捲軸控制項 (例如清單方塊、清單檢視或下拉式方塊)。For example, you use the Invoke control pattern for controls that can be invoked (such as buttons) and the Scroll control pattern for controls that have scroll bars (such as list boxes, list views, or combo boxes). 因為每個控制項模式都代表個別的功能,所以可以結合起來,用以描述特定控制項所支援的完整功能集。Because each control pattern represents a separate functionality, they can be combined to describe the full set of functionality supported by a particular control.


彙總控制項以可提供 使用者介面 (UI)user interface (UI) 用於父系所公開功能的子控制項所有建立,這應該實作通常會與每個子控制項相關聯的所有控制項模式。Aggregate controls—built with child controls that provide the 使用者介面 (UI)user interface (UI) for functionality exposed by the parent—should implement all control patterns normally associated with each child control. 然後相同的控制項模式便不需要由子控制項所實作。In turn, those same control patterns are not required to be implemented by the child controls.

使用者介面自動化控制項模式元件UI Automation Control Pattern Components

控制項模式支援定義可在控制項中使用個別功能所需的方法、屬性、事件和關聯性。Control patterns support the methods, properties, events, and relationships needed to define a discrete piece of functionality available in a control.

  • 使用者介面自動化項目和其父系、子系和同層級之間的關聯性描述 UI 自動化UI Automation 樹狀結構內的項目結構。The relationship between a UI Automation element and its parent, children and siblings describes the element's structure within the UI 自動化UI Automation tree.

  • 這些方法可以讓使用者介面自動化用戶端操作此控制項。The methods allow UI Automation clients to manipulate the control.

  • 屬性和事件提供控制項模式功能以及控制項狀態的相關資訊。The properties and events provide information about the control pattern's functionality as well as information about the state of the control.

控制項模式與 UIUI 相關聯,如同介面與元件物件模型(COM)物件相關聯。Control patterns relate to UIUI as interfaces relate to Component Object Model (COM) objects. 在 COM 中,您可以查詢物件來詢問它支援哪些介面,然後使用這些介面來存取功能。In COM, you can query an object to ask what interfaces it supports and then use those interfaces to access functionality. UI 自動化UI Automation,使用者介面自動化用戶端可以詢問控制項支援何種控制項模式,然後透過支援的控制項模式所公開的屬性、方法、事件和結構與其互動。In UI 自動化UI Automation, UI Automation clients can ask a control which control patterns it supports and then interact with the control through the properties, methods, events, and structures exposed by the supported control patterns. 例如,對於多行編輯方塊,使用者介面自動化提供者可實作 IScrollProviderFor example, for a multiline edit box, UI Automation providers implement IScrollProvider. 當用戶端知道 AutomationElement 支援 ScrollPattern 控制項模式時,它便可以使用該控制項模式所公開的屬性、方法和事件來操作控制項,或存取此控制項的相關資訊。When a client knows that an AutomationElement supports the ScrollPattern control pattern, it can use the properties, methods, and events exposed by that control pattern to manipulate the control, or access information about the control.

使用者介面自動化提供者和用戶端UI Automation Providers and Clients

使用者介面自動化提供者實作控制項模式來公開此控制項所支援特定功能的適當行為。UI Automation providers implement control patterns to expose the appropriate behavior for a specific piece of functionality supported by the control.

使用者介面自動化用戶端存取 UI 自動化UI Automation 控制模式類別的方法和屬性,並使用它們來取得 UIUI的相關資訊,或操作 UIUIUI Automation clients access methods and properties of UI 自動化UI Automation control pattern classes and use them to get information about the UIUI, or to manipulate the UIUI. 這些控制項模式類別位於 System.Windows.Automation 命名空間 (例如 InvokePatternSelectionPattern)。These control pattern classes are found in the System.Windows.Automation namespace (for example, InvokePattern and SelectionPattern).

用戶端會使用 AutomationElement 方法(例如 AutomationElement.GetCurrentPropertyValueAutomationElement.GetCachedPropertyValue)或 common language runtime (CLR)存取子來存取模式的 UI 自動化UI Automation 屬性。Clients use AutomationElement methods (such as AutomationElement.GetCurrentPropertyValue or AutomationElement.GetCachedPropertyValue) or the common language runtime (CLR) accessors to access the UI 自動化UI Automation properties on a pattern. 每個控制項模式類別都有一個欄位成員(例如,InvokePattern.PatternSelectionPattern.Pattern)識別該控制項模式,而且可以做為參數傳遞至 GetCachedPatternGetCurrentPattern 來取得 AutomationElement的該模式。Each control pattern class has a field member (for example, InvokePattern.Pattern or SelectionPattern.Pattern) that identifies that control pattern and can be passed as a parameter to GetCachedPattern or GetCurrentPattern to retrieve that pattern for an AutomationElement.

動態控制項模式Dynamic Control Patterns

有些控制項不一定支援一組相同的控制項模式。Some controls do not always support the same set of control patterns. 當控制項模式可用於使用者介面自動化用戶端時,就可視為受支援。Control patterns are considered supported when they are available to a UI Automation client. 例如,只有當多行編輯方塊包含的文字超出可檢視區域內可顯示的行數時,才會啟用垂直捲動。For example, a multiline edit box enables vertical scrolling only when it contains more lines of text than can be displayed in its viewable area. 移除足夠的文字,因而不再需要捲動之後,捲動便會停用。Scrolling is disabled when enough text is removed so that scrolling is no longer required. 此範例中,ScrollPattern 控制項模式會根據控制項 (編輯方塊中有多少文字) 的目前狀態,受到動態支援。For this example, the ScrollPattern control pattern is dynamically supported depending on the current state of the control (how much text is in the edit box).

控制項模式類別和介面Control Pattern Classes and Interfaces

下表描述 UI 自動化UI Automation 控制模式。The following table describes the UI 自動化UI Automation control patterns. 這個表格也列出使用者介面自動化用戶端用來存取控制模式的類別,以及使用者介面自動化提供者用來實作這些類別的介面。The table also lists the classes used by UI Automation clients to access the control patterns, as well as the interfaces used by UI Automation providers to implement them.

控制項模式類別Control Pattern Class 提供者介面Provider Interface 描述Description
DockPattern IDockProvider 用於可停駐在停駐容器中的控制項。Used for controls that can be docked in a docking container. 例如工具列或工具板。For example, toolbars or tool palettes.
ExpandCollapsePattern IExpandCollapseProvider 用於可展開或摺疊的控制項。Used for controls that can be expanded or collapsed. 例如,應用程式中的功能表項目,像是 [檔案] 功能表。For example, menu items in an application such as the File menu.
GridPattern IGridProvider 用於支援格線功能 (例如調整大小及移動至指定的儲存格) 的控制項。Used for controls that support grid functionality such as sizing and moving to a specified cell. 例如,Windows Explorer 中的大型圖示視圖,或 Microsoft Word 中沒有標頭的簡單資料表。For example, the large icon view in Windows Explorer or simple tables without headers in Microsoft Word.
GridItemPattern IGridItemProvider 用於格線中有儲存格的控制項。Used for controls that have cells within grids. 個別儲存格應該支援 GridItem 模式。The individual cells should support the GridItem pattern. 例如,Microsoft Windows Explorer 詳細資料檢視中的每個儲存格。For example, each cell in Microsoft Windows Explorer detail view.
InvokePattern IInvokeProvider 用於可以叫用的控制項,例如按鈕。Used for controls that can be invoked, such as a button.
MultipleViewPattern IMultipleViewProvider 用於可以在相同資訊、資料或子系的集合中,切換多種表示方式的控制項。Used for controls that can switch between multiple representations of the same set of information, data, or children. 例如,清單檢視控制項,資料就在縮圖、並排顯示、圖示、清單或詳細資料檢視中。For example, a list view control where data is available in thumbnail, tile, icon, list, or detail views.
RangeValuePattern IRangeValueProvider 用於範圍值可以套用至控制項的控制項。Used for controls that have a range of values that can be applied to the control. 例如,包含年份之微調控制項的範圍可能是 1900 到 2010,而另一個代表月份的微調控制項的範圍值則是 1 至 12。For example, a spinner control containing years might have a range of 1900 to 2010, while another spinner control presenting months would have a range of 1 to 12.
ScrollPattern IScrollProvider 用於可捲動的控制項。Used for controls that can scroll. 例如,具有捲軸,且當包含的資訊超出控制項可檢視區中可顯示的範圍時,捲軸即成為作用中的控制項。For example, a control that has scroll bars that are active when there is more information than can be displayed in the viewable area of the control.
ScrollItemPattern IScrollItemProvider 用於在可捲動的清單中具有個別項目的控制項。Used for controls that have individual items in a list that scrolls. 例如,在捲動清單中 (例如下拉式方塊控制項) 有個別項目的清單控制項。For example, a list control that has individual items in the scroll list, such as a combo box control.
SelectionPattern ISelectionProvider 用於選取容器控制項。Used for selection container controls. 例如清單方塊及下拉式方塊。For example, list boxes and combo boxes.
SelectionItemPattern ISelectionItemProvider 用於選取容器控制項 (例如清單方塊及下拉式方塊) 中的個別項目。Used for individual items in selection container controls, such as list boxes and combo boxes.
TablePattern ITableProvider 用於有格線以及標題資訊的控制項。Used for controls that have a grid as well as header information. 例如,Microsoft Excel 工作表。For example, Microsoft Excel worksheets.
TableItemPattern ITableItemProvider 用於表格中的項目。Used for items in a table.
TextPattern ITextProvider 用於公開文字資訊的編輯控制項及文件。Used for edit controls and documents that expose textual information.
TogglePattern IToggleProvider 用於可切換狀態的控制項。Used for controls where the state can be toggled. 例如,核取方塊及可勾選的功能表項目。For example, check boxes and checkable menu items.
TransformPattern ITransformProvider 用於可以調整大小、移動和旋轉的控制項。Used for controls that can be resized, moved, and rotated. 轉換控制項模式通常使用於設計工具、表單、圖形編輯器,以及繪圖應用程式。Typical uses for the Transform control pattern are in designers, forms, graphical editors, and drawing applications.
ValuePattern IValueProvider 允許用戶端對不支援範圍值的控制項,取得或設定一個值。Allows clients to get or set a value on controls that do not support a range of values. 例如日期時間選擇器。For example, a date time picker.
WindowPattern IWindowProvider 公開 windows 特有的資訊,這是 Microsoft Windows 作業系統的基本概念。Exposes information specific to windows, a fundamental concept to the Microsoft Windows operating system. Windows 控制項的範例包括最上層應用程式視窗(Microsoft Word、Microsoft Windows Explorer 等等)、多重文件介面(MDI)子視窗和對話方塊。Examples of controls that are windows are top-level application windows (Microsoft Word, Microsoft Windows Explorer, and so on), multiple-document interface (MDI) child windows, and dialogs.

請參閱See also