private void lower_ico_MouseUp(object sender, MouseButtonEventArgs e) { TextRange range = new TextRange(docBox.Selection.Start, docBox.Selection.End); var underline = range.GetPropertyValue(Run.TextDecorationsProperty); var italic = range.GetPropertyValue(Inline.FontStyleProperty); var bold = range.GetPropertyValue(RichTextBox.FontWeightProperty); var color = range.GetPropertyValue(RichTextBox.ForegroundProperty); var font = range.GetPropertyValue(RichTextBox.FontFamilyProperty); range.Text = range.Text.ToLower(); range.ApplyPropertyValue(Run.TextDecorationsProperty, underline); range.ApplyPropertyValue(Inline.FontStyleProperty, italic); range.ApplyPropertyValue(RichTextBox.FontWeightProperty, bold); range.ApplyPropertyValue(RichTextBox.ForegroundProperty, color); range.ApplyPropertyValue(RichTextBox.FontFamilyProperty, font); }
public static double GetDesiredWidth(this TableCell cell) { TextRange textRange = new TextRange(cell.ContentStart, cell.ContentEnd); return(new FormattedText( textRange.Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface( textRange.GetPropertyValue(TextElement.FontFamilyProperty) as FontFamily, (FontStyle)textRange.GetPropertyValue(TextElement.FontStyleProperty), (FontWeight)textRange.GetPropertyValue(TextElement.FontWeightProperty), FontStretches.Normal), (double)textRange.GetPropertyValue(TextElement.FontSizeProperty), Brushes.Black, null, TextFormattingMode.Display).Width); }
public IEnumerable <XD.TextAttribute> GetAttributes() { var range = new TextRange(TextBox.Selection.Start, TextBox.Selection.End); var ff = range.GetPropertyValue(FlowDocument.FontFamilyProperty) as SW.Media.FontFamily; var size = (double)range.GetPropertyValue(FlowDocument.FontSizeProperty); if (ff != null) { yield return new XD.FontDataAttribute { FontFamily = ff.FamilyNames.First().Value, FontSize = size } } ; var fw = (SW.FontWeight)range.GetPropertyValue(FlowDocument.FontWeightProperty); yield return(new XD.FontWeightTextAttribute { Weight = fw.ToXwtFontWeight() }); var fs = (SW.FontStyle)range.GetPropertyValue(FlowDocument.FontStyleProperty); yield return(new XD.FontStyleTextAttribute { Style = fs.ToXwtFontStyle() }); var decor = range.GetPropertyValue(Inline.TextDecorationsProperty) as SW.TextDecorationCollection; if (decor != null) { yield return(new XD.StrikethroughTextAttribute { Strikethrough = decor.Any(d => d.Location == SW.TextDecorationLocation.Strikethrough) }); yield return(new XD.UnderlineTextAttribute { Underline = decor.Any(d => d.Location == SW.TextDecorationLocation.Underline) }); } var brush = range.GetPropertyValue(FlowDocument.BackgroundProperty) as SWM.Brush; if (brush != null) { yield return new XD.BackgroundTextAttribute { Color = brush.ToXwtColor() } } ; brush = range.GetPropertyValue(FlowDocument.ForegroundProperty) as SWM.Brush; if (brush != null) { yield return new XD.ColorTextAttribute { Color = brush.ToXwtColor() } } ; }
private static void ConvertTextRangeToGeometry(TextRange textRange, FlowDirection flowDirection, List <PathGeometry> pathList) { TextPointer position1 = textRange.Start.GetInsertionPosition(LogicalDirection.Forward); for (TextPointer insertionPosition = position1.GetNextInsertionPosition(LogicalDirection.Forward); insertionPosition != null && insertionPosition.CompareTo(textRange.End) <= 0; insertionPosition = insertionPosition.GetNextInsertionPosition(LogicalDirection.Forward)) { TextRange textRange1 = new TextRange(position1, insertionPosition); Rect characterRect = position1.GetCharacterRect(LogicalDirection.Forward); characterRect.Union(insertionPosition.GetCharacterRect(LogicalDirection.Backward)); FontFamily fontFamily = (FontFamily)textRange1.GetPropertyValue(TextElement.FontFamilyProperty); FontStyle style = (FontStyle)textRange1.GetPropertyValue(TextElement.FontStyleProperty); FontWeight weight = (FontWeight)textRange1.GetPropertyValue(TextElement.FontWeightProperty); FontStretch stretch = (FontStretch)textRange1.GetPropertyValue(TextElement.FontStretchProperty); double emSize = (double)textRange1.GetPropertyValue(TextElement.FontSizeProperty); Typeface typeface = new Typeface(fontFamily, style, weight, stretch); PathConversionHelper.ConvertFormattedTextToGeometry(new FormattedText(textRange1.Text, CultureInfo.CurrentCulture, flowDirection, typeface, emSize, (Brush)Brushes.Red), characterRect.TopLeft, pathList); position1 = insertionPosition; } }
public static object GetPropertyValueEx(this TextRange range, DependencyProperty prop) { object propertyValue = range.GetPropertyValue(prop); if (propertyValue == DependencyProperty.UnsetValue) { TextRange textRange = new TextRange(range.Start, range.Start); propertyValue = textRange.GetPropertyValue(prop); } return(propertyValue); }
private void ChangeFontSize(int size) { TextRange currentTextRange = new TextRange(MainTextBox.Selection.Start, MainTextBox.Selection.End); double? currentStyledTextRange = currentTextRange.GetPropertyValue(Inline.FontSizeProperty) as double?; if (currentStyledTextRange is not null) { currentStyledTextRange = size; currentTextRange.ApplyPropertyValue(Inline.FontSizeProperty, currentStyledTextRange); } }
private void DoItalicTextChanges() { TextRange currentTextRange = new TextRange(MainTextBox.Selection.Start, MainTextBox.Selection.End); FontStyle?currentStyledTextRange = currentTextRange.GetPropertyValue(Inline.FontStyleProperty) as FontStyle?; if (currentStyledTextRange is not null) { currentStyledTextRange = FontStyles.Italic; currentTextRange.ApplyPropertyValue(Inline.FontStyleProperty, currentStyledTextRange); } }
private void DoBoldTextChanges() { TextRange currentTextRange = new TextRange(MainTextBox.Selection.Start, MainTextBox.Selection.End); FontWeight?currentStyledTextRange = currentTextRange.GetPropertyValue(Inline.FontWeightProperty) as FontWeight?; if (currentStyledTextRange is not null) { currentStyledTextRange = FontWeights.Bold; currentTextRange.ApplyPropertyValue(Inline.FontWeightProperty, currentStyledTextRange); } }
private void TextColorChanged(SolidColorBrush brush) { TextRange currentTextRange = new TextRange(MainTextBox.Selection.Start, MainTextBox.Selection.End); var currentStyledTextRange = currentTextRange.GetPropertyValue(Inline.ForegroundProperty) as SolidColorBrush; if (currentStyledTextRange is not null) { currentStyledTextRange = brush; currentTextRange.ApplyPropertyValue(Inline.ForegroundProperty, currentStyledTextRange); } }
public static ComplexStyle GetCurrentStyle(TextRange range, double defaultsize) { var test = range.GetPropertyValue(FlowDocument.FontSizeProperty); var applyableStyle = new ComplexStyle(); applyableStyle.FontFamily = range.GetPropertyValue(Control.FontFamilyProperty) as FontFamily; applyableStyle.FontSize = test != DependencyProperty.UnsetValue ? (double)test : defaultsize; applyableStyle.IsBold = range.GetPropertyValue(Control.FontWeightProperty).Equals(FontWeights.Bold); applyableStyle.IsItalic = range.GetPropertyValue(Control.FontStyleProperty).Equals(FontStyles.Italic); applyableStyle.IsCondenced = range.GetPropertyValue(Control.FontStretchProperty).Equals(FontStretches.Condensed); applyableStyle.IsUnderlined = Equals(range.GetPropertyValue(Inline.TextDecorationsProperty), TextDecorations.Underline); applyableStyle.IsStrikedOut = Equals(range.GetPropertyValue(Inline.TextDecorationsProperty), TextDecorations.Strikethrough); applyableStyle.BackgroundBrush = range.GetPropertyValue(FlowDocument.BackgroundProperty) as SolidColorBrush; applyableStyle.ForegroundBrush = range.GetPropertyValue(FlowDocument.ForegroundProperty) as SolidColorBrush; return(applyableStyle); }
public static void SelectLine(int line, RichTextBox editor) { int c = 0; TextRange r; var highlightedBackground = editor.GetValue(HighlightedBackgroundProperty); if (highlightedBackground == null) { return; } if (line < 0) { foreach (var item in editor.Document.Blocks) { //if (line == c) { r = new TextRange(item.ContentStart, item.ContentEnd); if (r.Text.Trim().Equals("")) { continue; } if (Equals(r.GetPropertyValue(TextElement.BackgroundProperty), highlightedBackground)) { r.ApplyPropertyValue(TextElement.BackgroundProperty, editor.Background); r.ApplyPropertyValue(TextElement.ForegroundProperty, editor.Foreground); return; } // } c++; } } foreach (var item in editor.Document.Blocks) { if (line == c) { r = new TextRange(item.ContentStart, item.ContentEnd); if (r.Text.Trim().Equals("")) { continue; } r.ApplyPropertyValue(TextElement.BackgroundProperty, highlightedBackground); r.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.White); return; } c++; } }
private void DoUnderlineTextChanges() { TextRange currentTextRange = new TextRange(MainTextBox.Selection.Start, MainTextBox.Selection.End); TextDecorationCollection tdc = currentTextRange.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection; if (tdc is not null) { TextDecorationCollection tdc_clone = new TextDecorationCollection(tdc); tdc_clone.Add(TextDecorations.Underline); currentTextRange.ApplyPropertyValue(Inline.TextDecorationsProperty, tdc_clone); } }
private void RichTextControl_SelectionChanged(object sender, RoutedEventArgs e) { // markierten Text holen TextRange selectionRange = new TextRange(rtbEditor.Selection.Start, rtbEditor.Selection.End); if (selectionRange.GetPropertyValue(FlowDocument.TextAlignmentProperty).ToString() == "Left") { ToolStripButtonAlignLeft.IsChecked = true; } if (selectionRange.GetPropertyValue(FlowDocument.TextAlignmentProperty).ToString() == "Center") { ToolStripButtonAlignCenter.IsChecked = true; } if (selectionRange.GetPropertyValue(FlowDocument.TextAlignmentProperty).ToString() == "Right") { ToolStripButtonAlignRight.IsChecked = true; } }
private static T GetPropertyValueOrDefault <T>(this TextRange obj, DependencyProperty property, T defaultValue) { object propertyValue = obj.GetPropertyValue(property); if (propertyValue == DependencyProperty.UnsetValue) { return(defaultValue); } else { return((T)propertyValue); } }
private void Content_LostFocus(object sender, RoutedEventArgs e) { MainWindow.ColorSelect -= ColorSelect; MainWindow.Instance.HideColorBar(); TextRange document = new TextRange(Content.Document.ContentStart, Content.Document.ContentEnd); TextPointer start = Content.Document.ContentStart; for (int n = 0; n < 10; n++) { TextRange color = new TextRange(start.GetPositionAtOffset(n), start.GetPositionAtOffset(n + 1)); if (color.GetPropertyValue(TextElement.ForegroundProperty) is Brush) { Brush brush = color.GetPropertyValue(TextElement.ForegroundProperty) as Brush; Console.WriteLine(brush.ToString() + ": " + color.Text + " / " + n + " <> " + (n + 1)); } else { Console.WriteLine("nc"); } } }
public FontProperties(TextRange range) { __FontWeight = range.GetPropertyValue(RichTextBox.FontWeightProperty); __FontSize = range.GetPropertyValue(RichTextBox.FontSizeProperty); __FontStyle = range.GetPropertyValue(RichTextBox.FontStyleProperty); __TextDecorations = range.GetPropertyValue(TextBlock.TextDecorationsProperty) as TextDecorationCollection; __FontFamily = range.GetPropertyValue(RichTextBox.FontFamilyProperty) as FontFamily; __ForegroundColor = range.GetPropertyValue(RichTextBox.ForegroundProperty) as Brush; }
// -- END https://nlog.codeplex.com/workitem/6272 private void SendTheMessageToRichTextBox(string logMessage, WpfRichTextBoxRowColoringRule rule) { RichTextBox rtbx = TargetRichTextBox; var tr = new TextRange(rtbx.Document.ContentEnd, rtbx.Document.ContentEnd) { Text = logMessage + "\r" }; tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(GetColorFromString(rule.FontColor, (Brush)tr.GetPropertyValue(TextElement.ForegroundProperty))) ); tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(GetColorFromString(rule.BackgroundColor, (Brush)tr.GetPropertyValue(TextElement.BackgroundProperty))) ); tr.ApplyPropertyValue(TextElement.FontStyleProperty, rule.Style); tr.ApplyPropertyValue(TextElement.FontWeightProperty, rule.Weight); if (MaxLines > 0) { _lineCount++; if (_lineCount > MaxLines) { tr = new TextRange(rtbx.Document.ContentStart, rtbx.Document.ContentEnd); tr.Text.Remove(0, tr.Text.IndexOf('\n')); _lineCount--; } } if (AutoScroll) { rtbx.ScrollToEnd(); } }
/// <summary> /// Is TextAlignment property of the selection set to AlignJustify. /// </summary> /// <param name="Selection"></param> /// <returns></returns> public static bool IsAlignJustify(this TextSelection Selection) { bool isAlignJustify = false; if (Selection != null) { var tr = new TextRange(Selection.Start, Selection.End); var pv = tr.GetPropertyValue(FlowDocument.TextAlignmentProperty); if (pv != null) { isAlignJustify = pv.Equals(TextAlignment.Left); } } return(isAlignJustify); }
/// <summary> /// is the FontWeight property of the Selection set to Bold /// </summary> /// <param name="Selection"></param> /// <returns></returns> public static bool IsBold(this TextSelection Selection) { bool isBold = false; if (Selection != null) { var tr = new TextRange(Selection.Start, Selection.End); var fwp = tr.GetPropertyValue(TextElement.FontWeightProperty); if (fwp != null) { isBold = fwp.Equals(FontWeights.Bold); } } return(isBold); }
/// <summary> /// is the FontStyle property of the Selection set to Italic /// </summary> /// <param name="Selection"></param> /// <returns></returns> public static bool IsItalic(this TextSelection Selection) { bool isItalic = false; if (Selection != null) { var tr = new TextRange(Selection.Start, Selection.End); var pv = tr.GetPropertyValue(TextElement.FontStyleProperty); if (pv != null) { isItalic = pv.Equals(FontStyles.Italic); } } return(isItalic); }
private void UpdateFormattingProperties() { TextRange selection = richTextBox.Selection; object fontWeight = selection.GetPropertyValue(TextElement.FontWeightProperty); object fontStyle = selection.GetPropertyValue(TextElement.FontStyleProperty); object textDecorations = selection.GetPropertyValue(Inline.TextDecorationsProperty); ViewModel.IsBold = fontWeight != DependencyProperty.UnsetValue && (FontWeight)fontWeight != FontWeights.Normal; ViewModel.IsItalic = fontStyle != DependencyProperty.UnsetValue && (FontStyle)fontStyle == FontStyles.Italic; ViewModel.IsUnderline = textDecorations != DependencyProperty.UnsetValue && textDecorations == TextDecorations.Underline; bool isNumberedList = false; bool isBulletList = false; if (selection.Start.Paragraph?.Parent is ListItem listItem) { var list = (List)listItem.Parent; isNumberedList = list.MarkerStyle == TextMarkerStyle.Decimal; isBulletList = list.MarkerStyle == TextMarkerStyle.Disc; } ViewModel.IsNumberedList = isNumberedList; ViewModel.IsBulletList = isBulletList; }
public static bool?GetTextDecorationOnSelection(TextRange range, TextDecorationLocation decorationLocation) { if (range.GetPropertyValue(TextBlock.TextDecorationsProperty) == DependencyProperty.UnsetValue) { return(null); } TextDecorationCollection decorations = (TextDecorationCollection)range.GetPropertyValue(TextBlock.TextDecorationsProperty); if (decorations == null) { return(false); } foreach (TextDecoration decoration in decorations) { if (decoration.Location == decorationLocation) { return(true); } } return(false); }
/// <summary> /// is the TextDecorations property of the Selection set to Underline /// </summary> /// <param name="Selection"></param> /// <returns></returns> public static bool IsUnderline(this TextSelection Selection) { bool isUnderline = false; if (Selection != null) { var tr = new TextRange(Selection.Start, Selection.End); var pv = tr.GetPropertyValue(Inline.TextDecorationsProperty); if (pv != null) { isUnderline = pv.Equals(TextDecorations.Underline); } } return(isUnderline); }
private void ButtonSizeBig_Click(object sender, RoutedEventArgs e) { double size; if (TextBox1.Selection.GetPropertyValue(TextElement.FontSizeProperty).GetType() == typeof(Double)) { size = (double)TextBox1.Selection.GetPropertyValue(TextElement.FontSizeProperty); } else { TextRange tmp = new TextRange(TextBox1.Selection.Start, TextBox1.Selection.Start.GetNextContextPosition(LogicalDirection.Backward)); size = (double)tmp.GetPropertyValue(TextElement.FontSizeProperty);; } TextBox1.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, (size * 1.25).ToString("f1")); }
private void underline_ico_MouseUp(object sender, MouseButtonEventArgs e) { // TextDecorations var range = new TextRange(docBox.Selection.Start, docBox.Selection.End); if (range.GetPropertyValue(Run.TextDecorationsProperty) == TextDecorations.Underline) { range.ApplyPropertyValue(Run.TextDecorationsProperty, null); } else { range.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline); } }
private void italic_ico_MouseUp(object sender, MouseButtonEventArgs e) { var range = new TextRange(docBox.Selection.Start, docBox.Selection.End); if (range.GetPropertyValue(Inline.FontStyleProperty).ToString() == FontStyles.Italic.ToString()) { range.ApplyPropertyValue(Inline.FontStyleProperty, FontStyles.Normal); } else { range.ApplyPropertyValue(Inline.FontStyleProperty, FontStyles.Italic); } }
private List <string> get_elements_in_string(TextPointer tp_start, TextPointer tp_end, Color color) { //获取选择区域内部指定颜色的所有单词和短语 List <string> dc_list = new List <string>(); TextPointer tp1 = tp_start; TextPointer tp2 = tp1.GetNextInsertionPosition(LogicalDirection.Forward); string color_mode_str = color.ToString(); string color_select_str; while (tp1 != tp_end) { if (tp2 == null) { break; } TextRange range_char = new TextRange(tp1, tp2); color_select_str = range_char.GetPropertyValue(TextElement.ForegroundProperty).ToString(); if (color_select_str == color_mode_str) { TextPointer tp_dc_start = tp1; TextPointer tp_dc_end; while (color_select_str == color_mode_str) { tp1 = tp2; tp2 = tp1.GetNextInsertionPosition(LogicalDirection.Forward); if (tp2 == null) { break; } range_char = new TextRange(tp1, tp2); color_select_str = range_char.GetPropertyValue(TextElement.ForegroundProperty).ToString(); } tp_dc_end = tp1; TextRange range_word = new TextRange(tp_dc_start, tp_dc_end); string word = range_word.Text; dc_list.Add(word); } else { tp1 = tp2; tp2 = tp1.GetNextInsertionPosition(LogicalDirection.Forward); } } return(dc_list); }
private void bold_ico_MouseUp(object sender, MouseButtonEventArgs e) { var range = new TextRange(docBox.Selection.Start, docBox.Selection.End); if (range.GetPropertyValue(RichTextBox.FontWeightProperty).ToString() == FontWeights.Bold.ToString()) { range.ApplyPropertyValue(RichTextBox.FontWeightProperty, FontWeights.Regular); } else { range.ApplyPropertyValue(RichTextBox.FontWeightProperty, FontWeights.Bold); } }
public static void ApplyFontWeight(TextRange textRange, FontWeight weight) { var cw = FontWeights.Normal; try { cw = (FontWeight)textRange.GetPropertyValue(TextElement.FontWeightProperty); } catch (Exception) { } if (cw == weight) { weight = FontWeights.Normal; } else { weight = FontWeights.Bold; } textRange.ApplyPropertyValue(TextElement.FontWeightProperty, weight); }
public static void GetHighlightedText(int textId, RichTextBox textRTB, RichTextBox legendRTB) { string text = TextManager.GetText(textId); var tags = TextManager.GetTags(textId); TextRange initialText = new TextRange(textRTB.Document.ContentStart, textRTB.Document.ContentEnd); initialText.Text = text; foreach (var tag in tags) { TextRange range = GetTextRange(tag.StartPos, tag.EndPos, textRTB.Document); var oldColor = range.GetPropertyValue(TextElement.BackgroundProperty); range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(tag.Color)); } GetTagsLegend(tags, legendRTB); }