richtextbox空間中操做行間距段間距均可以用發送消息解決,可是字間距卻鮮有人關注,沒法經過PARAFORMAT2消息解決,只能直接操做rtf格式this
字間距主要就是要控制 expand expandtw 這些rtf屬性spa
下面給出一個插入代碼,只能對簡單點的rtf進行操做,word保存出來的rtf不必定能夠blog
操做rtf格式的類庫使用RtfTreeci
public void SetCharacterSpacing(int spacing) { spacing--; if (spacing < 0) { return; } RtfTree doc = new RtfTree(); doc.LoadRtfText(this.Rtf); RtfNodeCollection fileds = doc.MainGroup.ChildNodes; bool hasltrch = false; bool hasrtlch = false; bool hasText = !string.IsNullOrEmpty(this.Text); foreach (RtfTreeNode f in fileds) { if (f.NodeType == RtfNodeType.Keyword) { if (f.NodeKey == "expnd") { f.Parameter = spacing; } if (f.NodeKey == "expndtw") { f.Parameter = spacing * 5; } if (f.NodeKey == "ltrch") { hasltrch = true; } if (f.NodeKey == "rtlch") { hasrtlch = true; } } } if (spacing > 0) { if (hasText) { bool ispar = false; for (int i = fileds.Count - 1; i >= 0; i--) { var f = fileds[i]; if (f.NodeKey == "par") { ispar = true; } if (f.NodeKey == "u" || f.NodeKey == "'" || f.NodeType == RtfNodeType.Text) { continue; } else { if (ispar) { if (fileds[i - 1].NodeKey.Contains("expnd")) { continue; } InsertLetterExpand(spacing, doc, f.Index); ispar = false; } } if (f.NodeType == RtfNodeType.Group) { if (f.ChildNodes[0].NodeKey == "colortbl" || f.ChildNodes[0].NodeKey == "fonttbl") { if (!doc.MainGroup[f.Index + 1].NodeKey.Contains("expnd")) { InsertLetterExpand(spacing, doc, f.Index + 1); i -= 2; } else { continue; } } } if (f.NodeKey == "viewkind") { if (!doc.MainGroup[f.Index - 1].NodeKey.Contains("expnd")) { InsertLetterExpand(spacing, doc, f.Index); i--; } continue; } } } else { for (int i = fileds.Count - 1; i >= 0; i--) { var f = fileds[i]; if (f.NodeKey == "par") { InsertLetterExpand(spacing, doc, f.Index - 1); break; } } } } if (!hasltrch) { doc.MainGroup.InsertChild(1, new RtfTreeNode(RtfNodeType.Keyword, "ltrch", false, 0)); } if (!hasrtlch) { doc.MainGroup.InsertChild(1, new RtfTreeNode(RtfNodeType.Keyword, "rtlch", false, 0)); } this.Rtf = doc.Rtf; } private void InsertLetterExpand(int spacing, RtfTree doc, int index) { doc.MainGroup.InsertChild(index, new RtfTreeNode(RtfNodeType.Keyword, "expnd", true, spacing)); doc.MainGroup.InsertChild(index, new RtfTreeNode(RtfNodeType.Keyword, "expndtw", true, spacing * 5)); }