public async void ZamenaPic(string textNaZamenu1, StorageFile file) { if (file != null) { using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { string textToFind = textNaZamenu1; if (textToFind != null) { RichEditBox richEditBox = new RichEditBox(); richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, stringRich); ITextRange searchRange = richEditBox.Document.GetRange(0, 0); if (searchRange.FindText(textToFind, TextConstants.MaxUnitCount, FindOptions.Word) > 0) { await new MessageDialog(searchRange.EndPosition.ToString()).ShowAsync(); // searchRange.SetText(TextSetOptions.FormatRtf, zamena); BitmapImage image = new BitmapImage(); await image.SetSourceAsync(fileStream); searchRange.InsertImage(100, 100, 0, VerticalCharacterAlignment.Baseline, "img", fileStream); } string ss = String.Empty; richEditBox.TextDocument.GetText(Windows.UI.Text.TextGetOptions.FormatRtf, out ss); stringRich = ss; } } } }
private void FindAndHighlightText(string textToFind) { ClearAllHighlightedWords(); ITextRange searchRange = editor.Document.GetRange(0, TextConstants.MaxUnitCount); searchRange.Move(0, 0); bool textFound = true; do { if (searchRange.FindText(textToFind, TextConstants.MaxUnitCount, FindOptions.None) < 1) { textFound = false; } else { ITextRange ss = searchRange.GetClone(); m_highlightedWords.Add(searchRange.GetClone()); ITextCharacterFormat charFormatting = searchRange.CharacterFormat; charFormatting.BackgroundColor = Colors.Yellow; searchRange.CharacterFormat = charFormatting; } } while (textFound); }
private void FindBoxHighlightMatches() { FindBoxRemoveHighlights(); string textToFind = findBox.Text; if (textToFind != null) { ITextRange searchRange = editor.Document.GetRange(0, 0); while (searchRange.FindText(textToFind, TextConstants.MaxUnitCount, FindOptions.None) > 0) { searchRange.CharacterFormat.BackgroundColor = Colors.Yellow; } } }
private void FindBoxHighlightMatches() { FindBoxRemoveHighlights(); Color highlightBackgroundColor = (Color)App.Current.Resources["SystemColorHighlightColor"]; Color highlightForegroundColor = (Color)App.Current.Resources["SystemColorHighlightTextColor"]; string textToFind = findBox.Text; if (textToFind != null) { ITextRange searchRange = editor.Document.GetRange(0, 0); while (searchRange.FindText(textToFind, TextConstants.MaxUnitCount, FindOptions.None) > 0) { searchRange.CharacterFormat.BackgroundColor = highlightBackgroundColor; searchRange.CharacterFormat.ForegroundColor = highlightForegroundColor; } } }
private void HighlightSearchBoxMatches() { ClearHighlights(); Color highlightBackgroundColor = Colors.Orange; string?textToFind = SearchTextBox.Text; if (textToFind != null) { ITextRange searchRange = CodeEditor.Document.GetRange(0, 0); FindOptions findOptions = MatchCaseToggleButton.IsChecked !.Value ? FindOptions.Case : FindOptions.None; findOptions = MatchWordToggleButton.IsChecked !.Value ? FindOptions.Word : findOptions; while (searchRange.FindText(textToFind, TextConstants.MaxUnitCount, findOptions) > 0) { searchRange.CharacterFormat.BackgroundColor = highlightBackgroundColor; } } }
// 高亮显示用户搜索的字符 private void btnSearch_Click(object sender, RoutedEventArgs e) { // 清除高亮字符的高亮效果 ITextCharacterFormat charFormat; for (int i = 0; i < _highlightedWords.Count; i++) { charFormat = _highlightedWords[i].CharacterFormat; charFormat.BackgroundColor = Colors.Transparent; _highlightedWords[i].CharacterFormat = charFormat; } _highlightedWords.Clear(); // 获取全部文本,并将操作点移动到文本的起点 ITextRange searchRange = txtEditor.Document.GetRange(0, TextConstants.MaxUnitCount); searchRange.Move(0, 0); bool textFound = true; do { // 在全部文本中搜索指定的字符串 if (searchRange.FindText(txtSearch.Text, TextConstants.MaxUnitCount, FindOptions.None) < 1) { textFound = false; } else { _highlightedWords.Add(searchRange.GetClone()); // 实体化一个 ITextCharacterFormat,指定字符背景颜色为黄色 ITextCharacterFormat charFormatting = searchRange.CharacterFormat; charFormatting.BackgroundColor = Colors.Orange; // 设置指定文本的字符格式(高亮效果) searchRange.CharacterFormat = charFormatting; } } while (textFound); }
private void HighlightLinks() { string textContent; this.Document.GetText(TextGetOptions.None, out textContent); int start = 0; int end = textContent.Length; ITextRange range = this.Document.GetRange(start, end); range.CharacterFormat.Underline = UnderlineType.None; range.CharacterFormat.ForegroundColor = this.textForegroundColor; if (this.Document.Selection == null) { return; } int startPosition = this.Document.Selection.StartPosition; int endPosition = this.Document.Selection.EndPosition; foreach (string uri in RegexHelper.FindUris(textContent)) { int result = range.FindText(uri, end - start, FindOptions.None); if (result > 0) { this.Document.Selection.SetRange(range.StartPosition, range.EndPosition); range.CharacterFormat.Underline = UnderlineType.Single; range.CharacterFormat.ForegroundColor = this.hyperlinkForegroundColor; range.ScrollIntoView(PointOptions.None); } } this.Document.Selection.SetRange(startPosition, endPosition); }