ScrollPattern.SetScrollPercent(Double, Double) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將水平及/或垂直捲動位置設定為該區域內 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)
參數
例外狀況
無法轉換成雙重的值會被傳遞進去。
大於 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 表示指定方向沒有滾動。