ScrollPattern.SetScrollPercent(Double, Double) 方法

定义

将水平和/或垂直滚动位置设置为内 AutomationElement总内容区域的百分比。

public:
 void SetScrollPercent(double horizontalPercent, double verticalPercent);
public void SetScrollPercent(double horizontalPercent, double verticalPercent);
member this.SetScrollPercent : double * double -> unit
Public Sub SetScrollPercent (horizontalPercent As Double, verticalPercent As Double)

参数

horizontalPercent
Double

总水平内容区域的百分比。 NoScroll 如果无法按此方向滚动控件,则应传入该控件。

verticalPercent
Double

垂直内容总量区域的百分比。 NoScroll 如果无法按此方向滚动控件,则应传入该控件。

例外

无法转换为双精度值的值将传入。

传入大于 100 或小于 0 的值(除 -1 外,等效)。NoScroll HorizontalScrollPercent规范VerticalScrollPercent化为 0% 或 100%。

尝试滚动到不受支持的方向。

示例

在以下示例中, ScrollPattern 从某个 AutomationElement 控件模式获取,然后用于将可查看区域滚动到内容区域的左上角“主页”位置。

///--------------------------------------------------------------------
/// <summary>
/// Obtains a ScrollPattern control pattern from an 
/// automation element.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <returns>
/// A ScrollPattern object.
/// </returns>
///--------------------------------------------------------------------
private ScrollPattern GetScrollPattern(
    AutomationElement targetControl)
{
    ScrollPattern scrollPattern = null;

    try
    {
        scrollPattern =
            targetControl.GetCurrentPattern(
            ScrollPattern.Pattern)
            as ScrollPattern;
    }
    // Object doesn't support the ScrollPattern control pattern
    catch (InvalidOperationException)
    {
        return null;
    }

    return scrollPattern;
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains a ScrollPattern control pattern from an 
''' automation element.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <returns>
''' A ScrollPattern object.
''' </returns>
'''--------------------------------------------------------------------
Private Function GetScrollPattern( _
ByVal targetControl As AutomationElement) As ScrollPattern
    Dim scrollPattern As ScrollPattern = Nothing

    Try
        scrollPattern = DirectCast( _
        targetControl.GetCurrentPattern(scrollPattern.Pattern), _
        ScrollPattern)
    Catch
        ' Object doesn't support the ScrollPattern control pattern
        Return Nothing
    End Try

    Return scrollPattern

End Function 'GetScrollPattern
///--------------------------------------------------------------------
/// <summary>
/// Obtains a ScrollPattern control pattern from an automation 
/// element and attempts to scroll to the 'home' position.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
///--------------------------------------------------------------------
private void ScrollHome(AutomationElement targetControl)
{
    if (targetControl == null)
    {
        throw new ArgumentNullException(
            "AutomationElement argument cannot be null.");
    }

    ScrollPattern scrollPattern = GetScrollPattern(targetControl);

    if (scrollPattern == null)
    {
        return;
    }

    try
    {
        scrollPattern.SetScrollPercent(0, 0);
    }
    catch (InvalidOperationException)
    {
        // Control not able to scroll in the direction requested;
        // when scrollable property of that direction is False
        // TO DO: error handling.
    }
    catch (ArgumentOutOfRangeException)
    {
        // A value greater than 100 or less than 0 is passed in 
        // (except -1 which is equivalent to NoScroll).
        // TO DO: error handling.
    }
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains a ScrollPattern control pattern from an automation 
''' element and attempts to scroll to the top left 'home' position.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
'''--------------------------------------------------------------------
Private Sub ScrollHome(ByVal targetControl As AutomationElement) 
    If targetControl Is Nothing Then
        Throw New ArgumentNullException( _
        "AutomationElement argument cannot be null.")
    End If
    
    Dim scrollPattern As ScrollPattern = _
    GetScrollPattern(targetControl)
    
    If scrollPattern Is Nothing Then
        Return
    End If
    
    Try
        scrollPattern.SetScrollPercent(0, 0)
    Catch exc As InvalidOperationException
        ' Control not able to scroll in the direction requested;
        ' when scrollable property of that direction is False
        ' TO DO: error handling.
    Catch exc As ArgumentOutOfRangeException
        ' A value greater than 100 or less than 0 is passed in 
        ' (except -1 which is equivalent to NoScroll).
        ' TO DO: error handling.
    End Try

End Sub

注解

仅当控件的内容区域大于可见区域时,此方法才有用。

传入值 NoScroll 表示没有在指定方向滚动。

适用于