当前位置: 首页>>代码示例>>C#>>正文


C# TextPointer.GetNextContextPosition方法代码示例

本文整理汇总了C#中System.Windows.Documents.TextPointer.GetNextContextPosition方法的典型用法代码示例。如果您正苦于以下问题:C# TextPointer.GetNextContextPosition方法的具体用法?C# TextPointer.GetNextContextPosition怎么用?C# TextPointer.GetNextContextPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Windows.Documents.TextPointer的用法示例。


在下文中一共展示了TextPointer.GetNextContextPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetTextPositionAtOffset

        TextPointer GetTextPositionAtOffset(TextPointer position, int characterCount)
        {
            while (position != null)
            {
                var context = position.GetPointerContext(LogicalDirection.Forward);
                if (context == TextPointerContext.Text)
                {
                    var count = position.GetTextRunLength(LogicalDirection.Forward);
                    if (characterCount <= count)
                    {
                        return position.GetPositionAtOffset(characterCount);
                    }

                    characterCount -= count;
                }
                else if (position.Parent is LineBreak)
                {
                    var count = 2;
                    if (characterCount <= count)
                    {
                        return position.GetPositionAtOffset(characterCount);
                    }

                    characterCount -= count;
                }
                
                var nextContextPosition = position.GetNextContextPosition(LogicalDirection.Forward);
                if (nextContextPosition == null)
                    return position;

                position = nextContextPosition;
            }

            return position;
        }
开发者ID:ElemarJR,项目名称:Jujubas,代码行数:35,代码来源:MainWindow.xaml.cs

示例2: FindWordFromPosition

        private static TextRange 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)
                    {
                        TextPointer start = position.GetPositionAtOffset(indexInRun);
                        TextPointer end = start.GetPositionAtOffset(word.Length);
                        return new TextRange(start, end);
                    }
                }

                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }

            // position will be null if "word" is not found.
            return null;
        }
开发者ID:diegoRodriguezAguila,项目名称:SGAM.Elfec.Admin,代码行数:24,代码来源:RichTextBoxExtensions.cs

示例3: ApplyStructuralInlinePropertyAcrossParagraphs

        // Helper that walks paragraphs between start and end positions, applying passed formattingProperty value on them.
        private static void ApplyStructuralInlinePropertyAcrossParagraphs(TextPointer start, TextPointer end, DependencyProperty formattingProperty, object value) 
        {
            // We assume to call this method only for paragraph crossing case
            Invariant.Assert(start.Paragraph != null);
            Invariant.Assert(start.Paragraph.ContentEnd.CompareTo(end) < 0); 

            // Apply to first Paragraph 
            SetStructuralInlineProperty(start, start.Paragraph.ContentEnd, formattingProperty, value); 
            start = start.Paragraph.ElementEnd;
 
            // Apply to last paragraph
            if (end.Paragraph != null)
            {
                SetStructuralInlineProperty(end.Paragraph.ContentStart, end, formattingProperty, value); 
                end = end.Paragraph.ElementStart;
            } 
 
            // Now, loop through paragraphs between start and end positions
            while (start != null && start.CompareTo(end) < 0) 
            {
                if (start.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart &&
                    start.Parent is Paragraph)
                { 
                    Paragraph paragraph = (Paragraph)start.Parent;
 
                    // Apply property to paragraph just found. 
                    SetStructuralInlineProperty(paragraph.ContentStart, paragraph.ContentEnd, formattingProperty, value);
 
                    // Jump to Paragraph end to skip Inline formatting tags.
                    start = paragraph.ElementEnd;
                }
 
                start = start.GetNextContextPosition(LogicalDirection.Forward);
            } 
        } 
开发者ID:sjyanxin,项目名称:WPFSource,代码行数:36,代码来源:TextRangeEdit.cs

示例4: ClearPropertyValueFromSpansAndRuns

        // Helper that walks Run and Span elements between start and end positions, 
        // clearing value of passed formattingProperty on them.
        // 
        private static void ClearPropertyValueFromSpansAndRuns(TextPointer start, TextPointer end, DependencyProperty formattingProperty)
        {
            // Normalize start position forward.
            start = start.GetPositionAtOffset(0, LogicalDirection.Forward); 

            // Move to next context position before entering loop below, 
            // since in the loop we look backward. 
            start = start.GetNextContextPosition(LogicalDirection.Forward);
 
            while (start != null && start.CompareTo(end) < 0)
            {
                if (start.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart &&
                    TextSchema.IsFormattingType(start.Parent.GetType())) // look for Run/Span elements 
                {
                    start.Parent.ClearValue(formattingProperty); 
 
                    // Remove unnecessary Spans around this position, delete empty formatting elements (if any)
                    // and merge with adjacent inlines if they have identical set of formatting properties. 
                    MergeFormattingInlines(start);
                }

                start = start.GetNextContextPosition(LogicalDirection.Forward); 
            }
        } 
开发者ID:sjyanxin,项目名称:WPFSource,代码行数:27,代码来源:TextRangeEdit.cs

示例5: GetNextRun

        // Finds a Run element with ElementStart at or after the given pointer
        // Creates Runs at potential run positions if encounters some. 
        private static Run GetNextRun(TextPointer pointer, TextPointer limit) 
        {
            Run run = null; 

            while (pointer != null && pointer.CompareTo(limit) < 0)
            {
                if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementStart && 
                    (run = pointer.GetAdjacentElement(LogicalDirection.Forward) as Run) != null)
                { 
                    break; 
                }
 
                if (TextPointerBase.IsAtPotentialRunPosition(pointer))
                {
                    pointer = TextRangeEditTables.EnsureInsertionPosition(pointer);
                    Invariant.Assert(pointer.Parent is Run); 
                    run = pointer.Parent as Run;
                    break; 
                } 

                // Advance the scanning pointer 
                pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
            }

            return run; 
        }
开发者ID:sjyanxin,项目名称:WPFSource,代码行数:28,代码来源:TextRangeEdit.cs

示例6: GetNextBlock

        // Finds a Paragraph/BlockUIContainer/List element with ElementStart before or at the given pointer 
        // Creates implicit paragraphs at potential paragraph positions if needed 
        private static Block GetNextBlock(TextPointer pointer, TextPointer limit)
        { 
            Block block = null;

            while (pointer != null && pointer.CompareTo(limit) <= 0)
            { 
                if (pointer.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart)
                { 
                    block = pointer.Parent as Block; 
                    if (block is Paragraph || block is BlockUIContainer || block is List)
                    { 
                        break;
                    }
                }
 
                if (TextPointerBase.IsAtPotentialParagraphPosition(pointer))
                { 
                    pointer = TextRangeEditTables.EnsureInsertionPosition(pointer); 
                    block = pointer.Paragraph;
                    Invariant.Assert(block != null); 
                    break;
                }

                // Advance the scanning pointer 
                pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
            } 
 
            return block;
        } 
开发者ID:sjyanxin,项目名称:WPFSource,代码行数:31,代码来源:TextRangeEdit.cs

示例7: HighlightText

        private void HighlightText(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)
                    {
                        TextPointer start = position.GetPositionAtOffset(indexInRun);
                        TextPointer end = start.GetPositionAtOffset(word.Length);
                        new TextRange(start, end).ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                    }
                }

                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }
        }
开发者ID:felipecsl,项目名称:mensagei.ro,代码行数:21,代码来源:MainWindow.xaml.cs

示例8: ApplyContextualProperty

        // Applies one property to a range from start to end to simulate inheritance of this property from source conntext
        private static void ApplyContextualProperty(Type targetType, TextPointer start, TextPointer end, DependencyProperty property, object value)
        {
            if (TextSchema.ValuesAreEqual(start.Parent.GetValue(property), value))
            {
                return; // The property at insertion position is the same as it was in source context. Nothing to do.
            }
            
            // Advance start pointer to enter pasted fragment
            start = start.GetNextContextPosition(LogicalDirection.Forward);

            while (start != null && start.CompareTo(end) < 0)
            {
                TextPointerContext passedContext = start.GetPointerContext(LogicalDirection.Backward);
                if (passedContext == TextPointerContext.ElementStart)
                {
                    TextElement element = (TextElement)start.Parent;

                    // Check if this element affects the property in question
                    if (element.ReadLocalValue(property) != DependencyProperty.UnsetValue ||
                        !TextSchema.ValuesAreEqual(element.GetValue(property), element.Parent.GetValue(property)))
                    {
                        // The element affects this property, so we can skip it
                        start = element.ElementEnd;
                    }
                    else if (targetType.IsAssignableFrom(element.GetType()))
                    {
                        start = element.ElementEnd;

                        if (targetType == typeof(Block) && start.CompareTo(end) > 0)
                        {
                            // Contextual properties should not apply to the last paragraph
                            // when it is merged with the following content -
                            // to avoid affecting the folowing visible content formatting.
                            break;
                        }

                        // This is topmost-level inline element which inherits this property.
                        // Set the value explicitly
                        if (!TextSchema.ValuesAreEqual(value, element.GetValue(property)))
                        {
                            element.ClearValue(property);
                            if (!TextSchema.ValuesAreEqual(value, element.GetValue(property)))
                            {
                                element.SetValue(property, value);
                            }

                            TextRangeEdit.MergeFormattingInlines(element.ElementStart);
                        }
                    }
                    else
                    {
                        // Traverse down into a structured (non-innline) element
                        start = start.GetNextContextPosition(LogicalDirection.Forward);
                    }
                }
                else
                {
                    // Traverse up from any element
                    Invariant.Assert(passedContext != TextPointerContext.None, "TextPointerContext.None is not expected");
                    start = start.GetNextContextPosition(LogicalDirection.Forward);
                }
            }
        }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:64,代码来源:TextRangeSerialization.cs

示例9: InsertInkAtPosition

        // Inserts an InkInteropObject at a specified position.
        private TextPointer InsertInkAtPosition(TextPointer insertionPosition, InkInteropObject inkobject, out UnsafeNativeMethods.TS_TEXTCHANGE change)
        {
            int symbolsAddedBefore = 0;
            int symbolsAddedAfter = 0;

            // Prepare an insertion position for InlineUIContainer.
            // As an optimization, shift outside of any formatting tags to avoid
            // splitting tags below.
            while (insertionPosition.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart &&
                TextSchema.IsFormattingType(insertionPosition.Parent.GetType()))
            {
                insertionPosition = insertionPosition.GetNextContextPosition(LogicalDirection.Backward);
            }
            while (insertionPosition.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd &&
                TextSchema.IsFormattingType(insertionPosition.Parent.GetType()))
            {
                insertionPosition = insertionPosition.GetNextContextPosition(LogicalDirection.Forward);
            }

            // If we need to, split the current parent TextElement and prepare
            // a suitable home for an InlineUIContainer.
            if (!TextSchema.IsValidParent(insertionPosition.Parent.GetType(), typeof(InlineUIContainer)))
            {
                insertionPosition = TextRangeEditTables.EnsureInsertionPosition(insertionPosition, out symbolsAddedBefore, out symbolsAddedAfter);
                Invariant.Assert(insertionPosition.Parent is Run, "position must be in Run scope");

                insertionPosition = TextRangeEdit.SplitElement(insertionPosition);
                // We need to remember how many symbols were added into addition
                // to the InlineUIContainer itself.
                // Account for the two element edges just added.
                symbolsAddedBefore += 1;
                symbolsAddedAfter += 1;
            }

            // Create an InlineUIContainer.
            InlineUIContainer inlineUIContainer = new InlineUIContainer(inkobject);

            change.start = ((ITextPointer)insertionPosition).Offset - symbolsAddedBefore;
            change.oldEnd = change.start;

            // Insert it into the insertionPosition.  This adds 3 symbols.
            insertionPosition.InsertTextElement(inlineUIContainer);

            change.newEnd = change.start + symbolsAddedBefore + inlineUIContainer.SymbolCount + symbolsAddedAfter;

            // Return a position after the inserted object.
            return inlineUIContainer.ElementEnd.GetInsertionPosition(LogicalDirection.Forward);
        }
开发者ID:mind0n,项目名称:hive,代码行数:49,代码来源:TextStore.cs

示例10: FindTextFromTextPointerPosition

        /// <summary>
        /// Looks for the provided text from the provided location.
        /// </summary>
        TextRange FindTextFromTextPointerPosition(TextPointer textPointerPosition, string text)
        {
            while (textPointerPosition != null)
            {
                if (textPointerPosition.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    string textRun = textPointerPosition.GetTextInRun(LogicalDirection.Forward);

                    int indexInRun = textRun.IndexOf(text);
                    if (indexInRun >= 0)
                    {
                        TextPointer textPointerStartPosition = textPointerPosition.GetPositionAtOffset(indexInRun);
                        TextPointer textPointerEndPosition = textPointerStartPosition.GetPositionAtOffset(text.Length);
                        return new TextRange(textPointerStartPosition, textPointerEndPosition);
                    }
                }

                textPointerPosition = textPointerPosition.GetNextContextPosition(LogicalDirection.Forward);
            }

            return null;
        }
开发者ID:Woodje,项目名称:DaCoder,代码行数:25,代码来源:MainWindowViewModel.cs

示例11: searchHyperlink

    /// <summary>
    /// Test if the text pointer is in a hyperlink
    /// </summary>
    /// <param name="pos"></param>
    /// <returns></returns>
    private bool searchHyperlink(TextPointer pos)
    {
      if (null == pos)
        return false;

      TextPointerContext context = pos.GetPointerContext(LogicalDirection.Backward);

      if (null == context)
        return false;

      if (TextPointerContext.ElementStart == context)
      {
        object elem = pos.GetAdjacentElement(LogicalDirection.Backward);
        if (elem is Hyperlink)
        {
          return true;
        }
      }
      else if (TextPointerContext.ElementEnd == context)
      {
        object elem = pos.GetAdjacentElement(LogicalDirection.Backward);
        if (elem is Hyperlink)
        {
          return false;
        }
      }

      // go backwards...   
      TextPointer back = pos.GetNextContextPosition(LogicalDirection.Backward);
      return searchHyperlink(back);
    }
开发者ID:Innovate-Inc,项目名称:EMEMetadataToolKit,代码行数:36,代码来源:EditorRichTextBox.xaml.cs

示例12: SeekEnclosingStartTag

    private TextPointer SeekEnclosingStartTag(TextPointer point)
    {
      if (null == point)
        return null;

      TextPointerContext ctx = point.GetPointerContext(LogicalDirection.Backward);
      if (TextPointerContext.ElementStart == ctx)
        return point;

      // seek backward...
      return SeekEnclosingStartTag(point.GetNextContextPosition(LogicalDirection.Backward));
    }
开发者ID:Innovate-Inc,项目名称:EMEMetadataToolKit,代码行数:12,代码来源:EditorRichTextBox.xaml.cs

示例13: ChangeFontSize

    private void ChangeFontSize(TextPointer currentPoint, double size, TextPointer endingPoint, bool increase)
    {
      // currentPoint can split text, but size is the text's size based on the enclosing tag

      // find the next position
      TextPointer next = currentPoint.GetNextContextPosition(LogicalDirection.Forward);
      //TextPointerContext nextCtx = currentPoint.GetPointerContext(LogicalDirection.Forward);

      int currentOffset = next.GetOffsetToPosition(endingPoint);
      TextPointer endOfApply = (currentOffset <= 0) ? endingPoint : next;

      // apply the style for this portion
      TextRange range = new TextRange(currentPoint, endOfApply);
      try
      {        
        string text = currentPoint.GetTextInRun(LogicalDirection.Forward);
        double newSize = increase ? GetNextFontSize(size) : GetPreviousFontSize(size);
        range.ApplyPropertyValue(TextElement.FontSizeProperty, newSize);
      }
      catch (Exception)
      {
        // ignore
        return;
      }

      if (currentOffset <= 0)
        return; // done

      // what's next?
      object element = endOfApply.GetAdjacentElement(LogicalDirection.Forward);
      TextPointerContext ctx = endOfApply.GetPointerContext(LogicalDirection.Forward);
      if (element is UIElement)
      {
        UIElement uiElement = element as UIElement;
      }
      else if (element is TextElement)
      {
        TextElement textElement = element as TextElement;
        size = textElement.FontSize;
        ChangeFontSize(endOfApply, size, endingPoint, increase);
      }
      else
      {
        ChangeFontSize(endOfApply, size, endingPoint, increase);
      }

      return; // don't know what to do = done
    }
开发者ID:Innovate-Inc,项目名称:EMEMetadataToolKit,代码行数:48,代码来源:EditorRichTextBox.xaml.cs

示例14: WriteContainer

        /// <summary>
        /// Writes the container into the specified XmlWriter.
        /// </summary>
        private void WriteContainer(TextPointer start, TextPointer end, XmlWriter writer)
        {
            TextElement textElement;

            System.Diagnostics.Debug.Assert(start != null);
            System.Diagnostics.Debug.Assert(end != null);
            System.Diagnostics.Debug.Assert(writer != null);

            _writer = writer;

            WriteWordXmlHead();

            _cursor = start;
            while (_cursor.CompareTo(end) < 0)
            {
                switch (_cursor.GetPointerContext(_dir))
                {
                    case TextPointerContext.None:
                        System.Diagnostics.Debug.Assert(false,
                            "Next symbol should never be None if cursor < End.");
                        break;
                    case TextPointerContext.Text:
                        RequireOpenRange();
                        _writer.WriteStartElement(WordXmlSerializer.WordTextTag);
                        _writer.WriteString(_cursor.GetTextInRun(_dir));
                        _writer.WriteEndElement();
                        break;
                    case TextPointerContext.EmbeddedElement:
                        DependencyObject obj = _cursor.GetAdjacentElement(LogicalDirection.Forward);
                        if (obj is LineBreak)
                        {
                            RequireOpenRange();
                            _writer.WriteStartElement(WordXmlSerializer.WordBreakTag);
                            _writer.WriteEndElement();
                        }
                        // TODO: try to convert some known embedded objects.
                        break;
                    case TextPointerContext.ElementStart:
                        TextPointer position;
                        position = _cursor;
                        position = position.GetNextContextPosition(LogicalDirection.Forward);
                        textElement = position.Parent as TextElement;

                        if (textElement is Paragraph)
                        {
                            RequireClosedRange();
                            RequireOpenParagraph();
                        }
                        else if (textElement is Inline)
                        {
                            RequireClosedRange();
                            RequireOpenParagraph();
                            RequireOpenRange();
                        }
                        break;
                    case TextPointerContext.ElementEnd:
                        textElement = _cursor.Parent as TextElement;

                        if (textElement is Inline)
                        {
                            RequireClosedRange();
                        }
                        else if (textElement is Paragraph)
                        {
                            RequireClosedParagraph();
                        }
                        break;
                }
                _cursor = _cursor.GetNextContextPosition(_dir);
            }

            RequireClosedRange();
            WriteWordXmlTail();
        }
开发者ID:ClemensT,项目名称:WPF-Samples,代码行数:77,代码来源:wordxmlwriter.cs

示例15: FindWordFromPosition

        private TextRange FindWordFromPosition(TextPointer position, string word, SearchDirection searchDirection)
        {
            string wordToFind;

            if (!CaseSensitive)
            {
                wordToFind = word.ToLower();
            }
            else
            {
                wordToFind = word;
            }

            LogicalDirection logicalDirection = SearchDirectionToLogicalDirection(searchDirection);

            while (position != null)
            {
                if (position.Parent is ConversationContentRun)
                {
                    string textRun = position.GetTextInRun(logicalDirection);

                    int indexInRun = FindWordInString(wordToFind, textRun, searchDirection);

                    if (indexInRun >= 0)
                    {
                        int startOffset;

                        if (searchDirection == SearchDirection.Down)
                        {
                            startOffset = indexInRun;
                        }
                        else
                        {
                            startOffset = -1 * (textRun.Length - indexInRun);
                        }

                        TextPointer start = position.GetPositionAtOffset(startOffset, logicalDirection);
                        TextPointer end = start.GetPositionAtOffset(wordToFind.Length, logicalDirection);
                        return new TextRange(start, end);
                    }
                }

                position = position.GetNextContextPosition(logicalDirection);
            }

            return null;
        }
开发者ID:jzajac2,项目名称:AllYourTexts,代码行数:47,代码来源:FindDialogModel.cs


注:本文中的System.Windows.Documents.TextPointer.GetNextContextPosition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。