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


C# TextPointer.GetPositionAtOffset方法代码示例

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


在下文中一共展示了TextPointer.GetPositionAtOffset方法的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: SafePositionAtOffset

        internal static TextPointer SafePositionAtOffset(FlowDocument document, TextPointer textPointer, int offsetStart)
        {
            var pointer = textPointer.GetPositionAtOffset(offsetStart);
            if (pointer == null)
                    return document.ContentEnd;

            return pointer;
        }
开发者ID:fednep,项目名称:UV-Outliner,代码行数:8,代码来源:UndoHelpers.cs

示例3: RemoveHyperlink

        public static TextPointer RemoveHyperlink(TextPointer start)
        {
            var backspacePosition = start.GetNextInsertionPosition(LogicalDirection.Backward);
            Hyperlink hyperlink;
            if (backspacePosition == null || !IsHyperlinkBoundaryCrossed(start, backspacePosition, out hyperlink))
            {
                return null;
            }

            // Remember caretPosition with forward gravity. This is necessary since we are going to delete
            // the hyperlink element preceeding caretPosition and after deletion current caretPosition
            // (with backward gravity) will follow content preceeding the hyperlink.
            // We want to remember content following the hyperlink to set new caret position at.

            var newCaretPosition = start.GetPositionAtOffset(0, LogicalDirection.Forward);

            // Deleting the hyperlink is done using logic below.

            // 1. Copy its children Inline to a temporary array.
            var hyperlinkChildren = hyperlink.Inlines;
            var inlines = new Inline[hyperlinkChildren.Count];
            hyperlinkChildren.CopyTo(inlines, 0);

            // 2. Remove each child from parent hyperlink element and insert it after the hyperlink.
            for (int i = inlines.Length - 1; i >= 0; i--)
            {
                hyperlinkChildren.Remove(inlines[i]);
                hyperlink.SiblingInlines.InsertAfter(hyperlink, inlines[i]);
            }

            // 3. Apply hyperlink's local formatting properties to inlines (which are now outside hyperlink scope).
            var localProperties = hyperlink.GetLocalValueEnumerator();
            var inlineRange = new TextRange(inlines[0].ContentStart, inlines[inlines.Length - 1].ContentEnd);

            while (localProperties.MoveNext())
            {
                var property = localProperties.Current;
                var dp = property.Property;
                object value = property.Value;

                if (!dp.ReadOnly &&
                    dp != Inline.TextDecorationsProperty && // Ignore hyperlink defaults.
                    dp != TextElement.ForegroundProperty &&
                    dp != BaseUriHelper.BaseUriProperty &&
                    !IsHyperlinkProperty(dp))
                {
                    inlineRange.ApplyPropertyValue(dp, value);
                }
            }

            // 4. Delete the (empty) hyperlink element.
            hyperlink.SiblingInlines.Remove(hyperlink);

            return newCaretPosition;
        }
开发者ID:JackWangCUMT,项目名称:Plainion,代码行数:55,代码来源:DocumentFacade.cs

示例4: 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

示例5: Word

 public Word(TextPointer wordStart, TextPointer wordEnd)
 {
     _wordStart = wordStart.GetPositionAtOffset(0, LogicalDirection.Forward);
     _wordEnd = wordEnd.GetPositionAtOffset(0, LogicalDirection.Backward);
 }
开发者ID:ForNeVeR,项目名称:PoshConsole,代码行数:5,代码来源:ConsoleTextBox.cs

示例6: GetCell

        public BufferCell GetCell(TextPointer position, LogicalDirection direction)
        {
            TextRange range = null;
            try
            {
                range = new TextRange(position, position.GetPositionAtOffset(1, direction));

                return new BufferCell(
                        (range.IsEmpty || range.Text[0] == '\n') ? ' ' : range.Text[0],
                        ConsoleColorFromBrush((Brush)range.GetPropertyValue(TextElement.ForegroundProperty)),
                        ConsoleColorFromBrush((Brush)range.GetPropertyValue(TextElement.BackgroundProperty)),
                        BufferCellType.Complete);
            }
            catch
            {
                return new BufferCell();
            }
        }
开发者ID:ForNeVeR,项目名称:PoshConsole,代码行数:18,代码来源:ConsoleTextBox.cs

示例7: 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

示例8: 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

示例9: FindWordFromPosition

        List<TextRange> FindWordFromPosition(TextPointer position, string word)
        {
            List<TextRange> ranges = new List<TextRange>();
            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);
                        ranges.Add(new TextRange(start, end));
                    }
                }
                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }
            return ranges;
        }
开发者ID:richardspiers,项目名称:Sharp-IMS-Client,代码行数:22,代码来源:Debug_window.xaml.cs

示例10: GetCharacterAtPosition

        public static string GetCharacterAtPosition(RichTextBox rtb, TextPointer position, int offset)
        {
            TextPointer nextPosition = position.GetPositionAtOffset(offset, LogicalDirection.Forward);
            if (nextPosition != null)
            {
                rtb.Selection.Select(position, nextPosition);
                if (rtb.Selection.Text.Length > 0)
                {
                    string c = rtb.Selection.Text;
                    rtb.Selection.Select(position, position); //reset selection
                    return (c);
                }
            }

            return (string.Empty);
        }
开发者ID:avatar29A,项目名称:Motion-Studio,代码行数:16,代码来源:RtbHelper.cs

示例11: while

       /*private static TextPointer GetPointOld(TextPointer start, int x)
       {
           var ret = start;
           var i = 0;
           while (i < x && ret != null)
           {
               if (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text || ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None)
                   i++;              
               if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
                   return ret;
               ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward);
           }
           return ret;
       }*/
       private static TextPointer GetPoint(TextPointer start, int x)
       {                      
           TextPointer ret = start.GetPositionAtOffset(x);           
           while (new TextRange(start, ret).Text.Length < x)
           {
               if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
                   return ret;
               ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward);
           }

           return ret;
       }
开发者ID:JoeyEremondi,项目名称:tikzedt,代码行数:26,代码来源:Adapters.cs

示例12: 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

示例13: 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

示例14: CheckWords

 private void CheckWords(string text, TextPointer start)
 {
     List<SyntaxProvider.Range> range = SyntaxProvider.CheckWords(text);
     foreach (SyntaxProvider.Range syntaxRange in range)
     {
         Range item = new Range
         {
             StartPosition = start.GetPositionAtOffset(syntaxRange.Start),
             EndPosition = start.GetPositionAtOffset(syntaxRange.End),
             Foreground = new SolidColorBrush(syntaxRange.Foreground)
         };
         _ranges.Add(item);
     }
 }
开发者ID:MotorViper,项目名称:FormGenerator,代码行数:14,代码来源:RichTextBoxEditor.cs

示例15: ReadDocument

        /// <summary>
        /// Reads the document from the reader into the specified position.
        /// </summary>
        private void ReadDocument(XmlReader reader, TextPointer position)
        {
            // Initialize all fields.
            _cursor = position;
            _dir = LogicalDirection.Forward;
            _otherDir = LogicalDirection.Backward;
            _reader = reader;

            // Set the gravity of the cursor to always look forward.
            _cursor = _cursor.GetPositionAtOffset(0, _dir);

            while (_reader.Read())
            {
                switch (_reader.NodeType)
                {
                    case XmlNodeType.Attribute:
                        System.Diagnostics.Debug.Assert(false,
                            "Attributes should never be processed by top-level convertion loop.");
                        break;
                    case XmlNodeType.EndElement:
                        System.Diagnostics.Trace.WriteLine("WordXmlReader.ReadDocument - EndElement [" +
                            _reader.Name + "]");
                        switch (_reader.Name)
                        {
                            case WordXmlSerializer.WordParagraphTag:
                                MoveOutOfElement(typeof(Paragraph));
                                _inParagraph = false;
                                break;
                            case WordXmlSerializer.WordRangeTag:
                                MoveOutOfElement(typeof(Run));
                                _inRange = false;
                                break;
                            case WordXmlSerializer.WordStyleTag:
                                _currentStyle = null;
                                break;
                            case WordXmlSerializer.WordTextTag:
                                _inText = false;
                                break;
                        }
                        break;
                    case XmlNodeType.Element:
                        System.Diagnostics.Trace.WriteLine("WordXmlReader.ReadDocument - Element [" +
                            _reader.Name + "]");
                        switch (_reader.Name)
                        {
                            case WordXmlSerializer.WordParagraphTag:
                                if (_inParagraph)
                                {
                                    throw CreateUnexpectedNodeException(_reader);
                                }
                                SurroundWithElement(new Paragraph(new Run()));
                                ApplyStyleToParagraph();
                                _inParagraph = true;
                                if (_reader.IsEmptyElement)
                                {
                                    MoveOutOfElement(typeof(Paragraph));
                                    _inParagraph = false;
                                }
                                break;
                            case WordXmlSerializer.WordRangeTag:
                                SurroundWithElement(new Run());
                                _inRange = true;
                                break;
                            case WordXmlSerializer.WordNameTag:
                                if (!IsPopulatingStyle)
                                {
                                    throw new ArgumentException("w:name only supported on styles.");
                                }
                                break;
                            case WordXmlSerializer.WordStyleTag:
                                _currentStyle = new Style();
                                SetupCurrentStyle();
                                break;
                            case WordXmlSerializer.WordTextTag:
                                _inText = true;
                                break;
                            case WordXmlSerializer.WordBreakTag:
                                if (_inRange)
                                {
                                    MoveOutOfElement(typeof(Run));
                                    new LineBreak(_cursor);
                                    SurroundWithElement(new Run());
                                }
                                else
                                {
                                    new LineBreak(_cursor);
                                }
                                break;
                            default:
                                SetSimpleProperty();
                                break;
                        }
                        break;
                    case XmlNodeType.SignificantWhitespace:
                    case XmlNodeType.CDATA:
                    case XmlNodeType.Text:
                        if (_inText)
//.........这里部分代码省略.........
开发者ID:ClemensT,项目名称:WPF-Samples,代码行数:101,代码来源:wordxmlreader.cs


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