TextPattern.ForegroundColorAttribute 字段

定义

标识文本范围的 ForegroundColor (COLORREF) 属性。

public: static initonly System::Windows::Automation::AutomationTextAttribute ^ ForegroundColorAttribute;
public static readonly System.Windows.Automation.AutomationTextAttribute ForegroundColorAttribute;
 staticval mutable ForegroundColorAttribute : System.Windows.Automation.AutomationTextAttribute
Public Shared ReadOnly ForegroundColorAttribute As AutomationTextAttribute 

字段值

AutomationTextAttribute

示例

以下示例演示如何获取 Microsoft 记事本中文本范围的属性值。

备注

Microsoft 记事本仅用于文本提供程序。 不能保证 Microsoft 记事本支持所请求的属性值。

private void GetForegroundColorAttribute()
{
    // Start application.
    Process p = Process.Start("Notepad.exe", "text.txt");

    // target --> The root AutomationElement.
    AutomationElement target = AutomationElement.FromHandle(p.MainWindowHandle);

    // Specify the control type we're looking for, in this case 'Document'
    PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document);

    AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);

    TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

    if (textpatternPattern == null)
    {
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
        return;
    }

    Object oAttribute = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.ForegroundColorAttribute);
    if (oAttribute == TextPattern.MixedAttributeValue)
    {
        Console.WriteLine("Mixed foreground colors.");
    }
    else
    {
        Console.WriteLine(oAttribute.ToString());
    }
}
Private Sub GetForegroundColorAttribute()
    ' Start application.
    Dim p As Process = Process.Start("Notepad.exe", "text.txt")

    ' target --> The root AutomationElement.
    Dim target As AutomationElement = AutomationElement.FromHandle(p.MainWindowHandle)

    ' Specify the control type we're looking for, in this case 'Document'
    Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)

    Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)

    Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)

    If (textpatternPattern Is Nothing) Then
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
        Return
    End If

    Dim oAttribute As Object = textpatternPattern.DocumentRange.GetAttributeValue(TextPattern.ForegroundColorAttribute)
    If (oAttribute = TextPattern.MixedAttributeValue) Then
        Console.WriteLine("Mixed foreground colors.")
    Else
        Console.WriteLine(oAttribute.ToString())
    End If
End Sub

注解

此属性的值等效于文本或字体颜色。

此标识符由UI 自动化客户端应用程序使用。 UI 自动化提供程序应使用等效字段。TextPatternIdentifiers

UI 自动化客户端通过调用GetAttributeValue获取特性的值。

此属性的值的类型 Int32。 默认值是 zero

适用于

另请参阅