TextPointer.GetPositionAtOffset 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从 TextPointer 内容开头返回指定偏移量(以符号表示)所指示的位置。
重载
| 名称 | 说明 |
|---|---|
| GetPositionAtOffset(Int32, LogicalDirection) |
从当前 TextPointer 的开头和指定方向返回由指定偏移量(以符号表示)所指示的位置 TextPointer。 |
| GetPositionAtOffset(Int32) |
从当前 TextPointer的开头返回指定偏移量所指示的位置的 TextPointer(以符号为单位)。 |
GetPositionAtOffset(Int32, LogicalDirection)
从当前 TextPointer 的开头和指定方向返回由指定偏移量(以符号表示)所指示的位置 TextPointer。
public:
System::Windows::Documents::TextPointer ^ GetPositionAtOffset(int offset, System::Windows::Documents::LogicalDirection direction);
public System.Windows.Documents.TextPointer GetPositionAtOffset(int offset, System.Windows.Documents.LogicalDirection direction);
member this.GetPositionAtOffset : int * System.Windows.Documents.LogicalDirection -> System.Windows.Documents.TextPointer
Public Function GetPositionAtOffset (offset As Integer, direction As LogicalDirection) As TextPointer
参数
- offset
- Int32
要计算并返回位置的偏移量(以符号为单位)。 如果偏移量为负值,则返回 TextPointer 的偏移量位于当前 TextPointer之前;否则,则返回后跟。
- direction
- LogicalDirection
指定 LogicalDirection 返回 TextPointer的逻辑方向的值之一。
返回
指定偏移量指示的位置的 A TextPointer ,或者 null 偏移量是否超过内容的末尾。
注解
以下任一项都被视为符号:
元素的 TextElement 开始或结束标记。
UIElement包含在或 InlineUIContainerBlockUIContainer. 中的元素。 请注意,此类 UIElement 始终计为一个符号;UIElement 包含的任何其他内容或元素不计为符号。
文本 Run 元素内的 16 位 Unicode 字符。
另请参阅
适用于
GetPositionAtOffset(Int32)
从当前 TextPointer的开头返回指定偏移量所指示的位置的 TextPointer(以符号为单位)。
public:
System::Windows::Documents::TextPointer ^ GetPositionAtOffset(int offset);
public System.Windows.Documents.TextPointer GetPositionAtOffset(int offset);
member this.GetPositionAtOffset : int -> System.Windows.Documents.TextPointer
Public Function GetPositionAtOffset (offset As Integer) As TextPointer
参数
- offset
- Int32
要计算并返回位置的偏移量(以符号为单位)。 如果偏移量为负值,则按属性指示 LogicalDirection 的逻辑方向计算位置。
返回
指定偏移量指示的位置的 A TextPointer ,或者 null 找不到相应的位置。
示例
以下示例演示了此方法的用法。 该示例使用 GetPositionAtOffset 该方法实现一对方法,一个方法计算相对于任何宿主段落的指定位置的偏移量,另一 TextPointer 个方法返回指定段落中的指定偏移量。
// Returns the offset for the specified position relative to any containing paragraph.
int GetOffsetRelativeToParagraph(TextPointer position)
{
// Adjust the pointer to the closest forward insertion position, and look for any
// containing paragraph.
Paragraph paragraph = (position.GetInsertionPosition(LogicalDirection.Forward)).Paragraph;
// Some positions may be not within any Paragraph;
// this method returns an offset of -1 to indicate this condition.
return (paragraph == null) ? -1 : paragraph.ContentStart.GetOffsetToPosition(position);
}
// Returns a TextPointer to a specified offset into a specified paragraph.
TextPointer GetTextPointerRelativeToParagraph(Paragraph paragraph, int offsetRelativeToParagraph)
{
// Verify that the specified offset falls within the specified paragraph. If the offset is
// past the end of the paragraph, return a pointer to the farthest offset position in the paragraph.
// Otherwise, return a TextPointer to the specified offset in the specified paragraph.
return (offsetRelativeToParagraph > paragraph.ContentStart.GetOffsetToPosition(paragraph.ContentEnd))
? paragraph.ContentEnd : paragraph.ContentStart.GetPositionAtOffset(offsetRelativeToParagraph);
}
' Returns the offset for the specified position relative to any containing paragraph.
Private Function GetOffsetRelativeToParagraph(ByVal position As TextPointer) As Integer
' Adjust the pointer to the closest forward insertion position, and look for any
' containing paragraph.
Dim paragraph As Paragraph = (position.GetInsertionPosition(LogicalDirection.Forward)).Paragraph
' Some positions may be not within any Paragraph
' this method returns an offset of -1 to indicate this condition.
Return If((paragraph Is Nothing), -1, paragraph.ContentStart.GetOffsetToPosition(position))
End Function
' Returns a TextPointer to a specified offset into a specified paragraph.
Private Function GetTextPointerRelativeToParagraph(ByVal paragraph As Paragraph, ByVal offsetRelativeToParagraph As Integer) As TextPointer
' Verify that the specified offset falls within the specified paragraph. If the offset is
' past the end of the paragraph, return a pointer to the farthest offset position in the paragraph.
' Otherwise, return a TextPointer to the specified offset in the specified paragraph.
Return If((offsetRelativeToParagraph > paragraph.ContentStart.GetOffsetToPosition(paragraph.ContentEnd)), paragraph.ContentEnd, paragraph.ContentStart.GetPositionAtOffset(offsetRelativeToParagraph))
End Function
注解
以下任一项都被视为符号:
元素的 TextElement 开始或结束标记。
UIElement包含在或 InlineUIContainerBlockUIContainer. 中的元素。 请注意,此类 UIElement 始终计为一个符号;UIElement 包含的任何其他内容或元素不计为符号。
文本 Run 元素内的 16 位 Unicode 字符。