2012-03-25 67 views
1

我做了一個簡單的文本編輯器。其中,可以使用用於更改字體系列,樣式和顏色的組合框。我成功地更改了所選文本的字體屬性。 我的代碼C#richtextbox字體屬性

private void fontFamilySelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (notesCon[currentNotes].SelectedText != "") 
      { 
       notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style);     
      } 
     } 

     private void fontStyleSelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (notesCon[currentNotes].SelectedText != "") 
      { 
       switch (fontStyle.SelectedItem.ToString()) 
       { 
        case "Bold": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Bold); 
         break; 
        case "Italic": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Italic); 
         break; 
        case "Regular": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Regular); 
         break; 
        case "Strikeout": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Strikeout); 
         break; 
        case "Underline": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Underline); 
         break; 

       } 
      }    
     } 

     private void fontColorSelectedValueChanged(object sender, EventArgs e) 
     { 
      if (notesCon[currentNotes].SelectedText != "") 
      { 
       notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style); 
       notesCon[currentNotes].SelectionColor = fontColor.SelectedItem.Color; 
      } 
     } 

我的困境:

每當我試着豐富的文本框的當前字體屬性設置爲一個新的,它成功地改變了它,但不保留字體屬性以前的文字。我應該如何更改字體屬性,而不必更改富文本框中文本的以前的字體屬性? 請幫助,提前致謝!

回答

0

這就是你需要的。我也有這個問題。 首先,您必須迭代選定文本的所有字符,然後應用字體。

private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle) 
    { 
     _maskChanges = true; 
     try 
     { 
      int txtStartPosition = txtFunctionality.SelectionStart; 
      int selectionLength = txtFunctionality.SelectionLength; 
      if (selectionLength > 0) 
       using (RichTextBox txtTemp = new RichTextBox()) 
       { 
        txtTemp.Rtf = txtFunctionality.SelectedRtf; 
        for (int i = 0; i < selectionLength; ++i) 
        { 
         txtTemp.Select(i, 1); 
         txtTemp.SelectionFont = RenderFont(txtTemp.SelectionFont, familyName, emSize, fontStyle, enableFontStyle); 
        } 

        txtTemp.Select(0, selectionLength); 
        txtFunctionality.SelectedRtf = txtTemp.SelectedRtf; 
        txtFunctionality.Select(txtStartPosition, selectionLength); 
       } 
     } 
     finally 
     { 
      _maskChanges = false; 
     } 
    } 

和渲染字體:

/// <summary> 
    /// Changes a font from originalFont appending other properties 
    /// </summary> 
    /// <param name="originalFont">Original font of text 
    /// <param name="familyName">Target family name 
    /// <param name="emSize">Target text Size 
    /// <param name="fontStyle">Target font style 
    /// <param name="enableFontStyle">true when enable false when disable 
    /// <returns>A new font with all provided properties added/removed to original font</returns> 
    private Font RenderFont(Font originalFont, string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle) 
    { 
     if (fontStyle.HasValue && fontStyle != FontStyle.Regular && fontStyle != FontStyle.Bold && fontStyle != FontStyle.Italic && fontStyle != FontStyle.Underline) 
      throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyleForSelectedText"); 

     Font newFont; 
     FontStyle? newStyle = null; 
     if (fontStyle.HasValue) 
     { 
      if (fontStyle.HasValue && fontStyle == FontStyle.Regular) 
       newStyle = fontStyle.Value; 
      else if (originalFont != null && enableFontStyle.HasValue && enableFontStyle.Value) 
       newStyle = originalFont.Style | fontStyle.Value; 
      else 
       newStyle = originalFont.Style & ~fontStyle.Value; 
     } 

     newFont = new Font(!string.IsNullOrEmpty(familyName) ? familyName : originalFont.FontFamily.Name, 
          emSize.HasValue ? emSize.Value : originalFont.Size, 
          newStyle.HasValue ? newStyle.Value : originalFont.Style); 
     return newFont; 
    } 

更多的細節和步驟,你可以閱讀這篇文章:http://how-to-code-net.blogspot.ro/2014/01/how-to-make-custom-richtextbox-control.html