TextPointer 類別

定義

表示 FlowDocumentTextBlock 內的位置。

public ref class TextPointer : System::Windows::Documents::ContentPosition
public class TextPointer : System.Windows.Documents.ContentPosition
type TextPointer = class
    inherit ContentPosition
Public Class TextPointer
Inherits ContentPosition
繼承
TextPointer

範例

下列範例示範如何使用 TextPointer 來尋找位於指定文字容器中第一 Run 個元素內的位置。

// This method returns the position just inside of the first text Run (if any) in a 
// specified text container.
TextPointer FindFirstRunInTextContainer(DependencyObject container)
{
    TextPointer position = null;

    if (container != null){
        if (container is FlowDocument)
            position = ((FlowDocument)container).ContentStart;
        else if (container is TextBlock)
            position = ((TextBlock)container).ContentStart;
        else
            return position;
    }
    // Traverse content in forward direction until the position is immediately after the opening 
    // tag of a Run element, or the end of content is encountered.
    while (position != null)
    {
        // Is the current position just after an opening element tag?
        if (position.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart)
        {
            // If so, is the tag a Run?
            if (position.Parent is Run)
                break;
        }

        // Not what we're looking for; on to the next position.
        position = position.GetNextContextPosition(LogicalDirection.Forward);
    }
        
    // This will be either null if no Run is found, or a position just inside of the first Run element in the
    // specifed text container.  Because position is formed from ContentStart, it will have a logical direction
    // of Backward.
    return position;
}
' This method returns the position just inside of the first text Run (if any) in a 
' specified text container.
Private Function FindFirstRunInTextContainer(ByVal container As DependencyObject) As TextPointer
    Dim position As TextPointer = Nothing

    If container IsNot Nothing Then
        If TypeOf container Is FlowDocument Then
            position = (CType(container, FlowDocument)).ContentStart
        ElseIf TypeOf container Is TextBlock Then
            position = (CType(container, TextBlock)).ContentStart
        Else
            Return position
        End If
    End If
    ' Traverse content in forward direction until the position is immediately after the opening 
    ' tag of a Run element, or the end of content is encountered.
    Do While position IsNot Nothing
        ' Is the current position just after an opening element tag?
        If position.GetPointerContext(LogicalDirection.Backward) = TextPointerContext.ElementStart Then
            ' If so, is the tag a Run?
            If TypeOf position.Parent Is Run Then
                Exit Do
            End If
        End If

        ' Not what we're looking for on to the next position.
        position = position.GetNextContextPosition(LogicalDirection.Forward)
    Loop

    ' This will be either null if no Run is found, or a position just inside of the first Run element in the
    ' specifed text container.  Because position is formed from ContentStart, it will have a logical direction
    ' of Backward.
    Return position
End Function

下列範例會實作使用 TextPointer 設施的簡單尋找演算法。

// This method will search for a specified word (string) starting at a specified position.
TextPointer FindWordFromPosition(TextPointer position, string word)
{
    while (position != null)
    {
         if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
         {
             string textRun = position.GetTextInRun(LogicalDirection.Forward);

             // Find the starting index of any substring that matches "word".
             int indexInRun = textRun.IndexOf(word);
             if (indexInRun >= 0)
             {
                 position = position.GetPositionAtOffset(indexInRun);
                 break;
             }
         }
         else
        {
            position = position.GetNextContextPosition(LogicalDirection.Forward);
        }
    }

     // position will be null if "word" is not found.
     return position; 
}
' This method will search for a specified word (string) starting at a specified position.
Private Function FindWordFromPosition(ByVal position As TextPointer, ByVal word As String) As TextPointer
    Do While position IsNot Nothing
        If position.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then
            Dim textRun As String = position.GetTextInRun(LogicalDirection.Forward)

            ' Find the starting index of any substring that matches "word".
            Dim indexInRun As Integer = textRun.IndexOf(word)
            If indexInRun >= 0 Then
                position = position.GetPositionAtOffset(indexInRun)
                Exit Do
            End If
        Else
            position = position.GetNextContextPosition(LogicalDirection.Forward)
        End If
    Loop

    ' position will be null if "word" is not found.
    Return position
End Function

備註

類別 TextPointer 介紹下列術語:

  • Position - 本質上,一 TextPointer 律指向內容中 的位置 。 這類位置會落在內容中的字元之間,或在定義內容結構的流程內容元素標記之間。

  • 目前位置 - 因為 一 TextPointer 律會指出位置,而且因為可以透過 TextPointer 執行的許多作業都相對於 目前所指向 TextPointer 的位置,所以只要將 所 TextPointer 指示的位置參照為 目前位置,就很合理。

  • 插入 位置 - 插入位置 是可新增內容的位置,而不會中斷相關聯內容的任何語意規則。 實際上,插入位置是在內容中可放置插入號的位置。 不是插入位置的有效 TextPointer 位置範例是兩個相鄰 Paragraph 標籤 (之間的位置,也就是前一個段落的結束記號與下一個段落的開頭標記) 。

  • 符號 - 針對涉及符號的 TextPointer 作業用途,下列任何一項都會被視為 符號

  • 文字 容器 - 文字容器 是構成手邊流程內容最終框線的專案;由 TextPointer 所指示的位置一律落在文字容器內。 目前,文字容器必須是 FlowDocumentTextBlock 。 一般而言,不支援不同文字容器中的實例之間的 TextPointer 作業。

  • 檔 - 文字容器中的內容稱為,如同 方法和 DocumentStartDocumentEnd 屬性一樣 IsInSameDocument

類別 TextPointer 旨在協助周遊和操作由 WPF Windows Presentation Foundation () 流程內容元素所代表的內容,一般而言,這類元素衍生自 TextElement 。 可協助的一些作業 TextPointer 包括下列各項:

物件所指出 TextPointer 的位置和 LogicalDirection 是固定的。 編輯或修改內容時,所 TextPointer 指示的位置不會變更相對於周圍文字;而是會對應地調整該位置與內容開頭的位移,以反映內容中的新相對位置。 例如, TextPointer ,表示指定段落開頭的位置會繼續指向該段落的開頭,即使內容是在段落之前或之後插入或刪除時也一直指向該段落的開頭。

類別 TextPointer 不提供任何公用建構函式。 的 TextPointer 實例是使用其他物件的屬性或方法所建立, (包括其他 TextPointer 物件) 。 下列清單提供一些建立和傳回 TextPointer 的方法和屬性範例。 這份清單並不詳盡:

屬性

DocumentEnd

在與目前位置相關的文字容器中的內容結尾處取得 TextPointer

DocumentStart

在與目前位置相關的文字容器中的內容開頭處取得 TextPointer

HasValidLayout

取得值,這個值表示與目前位置相關的文字容器是否具備有效的 (最新) 配置。

IsAtInsertionPosition

取得值,這個值表示目前位置是否為插入位置。

IsAtLineStartPosition

取得值,這個值表示目前位置是否位於字行開頭。

LogicalDirection

取得與目前位置相關的邏輯方向,這個邏輯方向用來釐清與目前位置相關的內容。

Paragraph

取得涵蓋目前位置的段落 (如果有的話)。

Parent

取得涵蓋目前位置的邏輯父代。

方法

CompareTo(TextPointer)

在目前 TextPointer 指定的位置與第二個指定的 TextPointer 之間執行順序比較。

DeleteTextInRun(Int32)

從目前 TextPointer 指出的位置刪除指定的字元數。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetAdjacentElement(LogicalDirection)

傳回在指定的邏輯方向與目前 TextPointer 相鄰的項目 (如果有的話)。

GetCharacterRect(LogicalDirection)

針對在指定的邏輯方向與目前 Rect 相鄰的內容,傳回週框方塊 (TextPointer)。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetInsertionPosition(LogicalDirection)

TextPointer 傳回至在指定的邏輯方向中的最接近插入位置。

GetLineStartPosition(Int32)

TextPointer 傳回至相對於目前 TextPointer 指定的行開頭。

GetLineStartPosition(Int32, Int32)

TextPointer 傳回至相對於目前 TextPointer 指定的行開頭,並報告已略過多少行。

GetNextContextPosition(LogicalDirection)

將指標傳回至指定的邏輯方向中的下一個符號。

GetNextInsertionPosition(LogicalDirection)

TextPointer 傳回至所指定邏輯方向中的下一個插入位置。

GetOffsetToPosition(TextPointer)

傳回目前 TextPointer 與第二個指定的 TextPointer 之間的符號計數。

GetPointerContext(LogicalDirection)

針對在指定的邏輯方向中與目前 TextPointer 相鄰的內容,傳回分類指示器。

GetPositionAtOffset(Int32)

TextPointer 傳回至指定的位移 (以符號表示) 所指出的位置 (從目前 TextPointer.的開頭算起)。

GetPositionAtOffset(Int32, LogicalDirection)

TextPointer 傳回至指定的位移 (以符號表示) 所指出的位置 (依指定的方向從目前 TextPointer 的開頭算起)。

GetTextInRun(LogicalDirection)

傳回一個字串,其中包含與指定之邏輯方向中目前 TextPointer 相鄰的任何文字。

GetTextInRun(LogicalDirection, Char[], Int32, Int32)

將指定的最大字元數從指定方向中的任何相鄰文字複製到呼叫者提供的字元陣列中。

GetTextRunLength(LogicalDirection)

依指定的邏輯方向,傳回介於目前 TextPointer 與下一個非文字符號之間的 Unicode 字元數。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InsertLineBreak()

在目前位置插入分行符號。

InsertParagraphBreak()

在目前位置插入分段符號。

InsertTextInRun(String)

在目前位置將指定的文字插入文字 Run 中。

IsInSameDocument(TextPointer)

表示指定的位置是否與目前位置位於相同的文字容器中。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

這個類型或成員支援 Windows Presentation Foundation (WPF) 基礎結構,而且不能從程式碼直接使用。

適用於

另請參閱