/// <summary> /// 高亮显示 /// </summary> /// <param name="targetTextBox"/><param name="s"/> internal void PrintWithSyntaxHighlighting(EfficientTextBox targetTextBox, string s) { if (s.Trim().StartsWith("[Example(")) { return; } if (s.Trim().StartsWith("//")) { targetTextBox.SelectionColor = Color.Green; targetTextBox.AppendText(s + Environment.NewLine); targetTextBox.SelectionColor = Color.Black; } else { targetTextBox.SelectionBackColor = Color.White; if (s.Contains("//highlight")) { s = s.Replace("//highlight", ""); targetTextBox.SelectionBackColor = Color.Yellow; } MatchCollection matchCollection = this.reg.Matches(s); int startIndex = 0; foreach (Match match in matchCollection) { targetTextBox.AppendText(s.Substring(startIndex, match.Index - startIndex)); startIndex = match.Index + match.Length; string str = s.Substring(match.Index, match.Length); if (this.IsReservedWord(str)) { targetTextBox.SelectionColor = Color.Blue; } targetTextBox.AppendText(str); targetTextBox.SelectionColor = Color.Black; } if (startIndex < s.Length) { targetTextBox.AppendText(s.Substring(startIndex)); } targetTextBox.AppendText(Environment.NewLine); } }
protected void OnSelectionChanged() { Type selectedExample = this.SelectedExample; if (selectedExample == (Type)null) { return; } this.SuspendLayout(); this.exampleSummaryBox.Clear(); this.exampleSummaryBox.SelectionColor = Color.DarkBlue; this.exampleSummaryBox.SelectionFont = new Font(FontFamily.GenericSansSerif, 11f, FontStyle.Bold); this.exampleSummaryBox.AppendText(selectedExample.Name + Environment.NewLine); ExampleAttribute exampleAttribute = this.GetExampleAttribute(selectedExample); this.exampleSummaryBox.SelectionFont = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular); this.exampleSummaryBox.SelectionColor = Color.FromArgb(0, 0, 100); string text = ""; if (exampleAttribute != null) { text = exampleAttribute.Description; } this.exampleSummaryBox.AppendText(text); this.exampleSummaryBox.Size = this.exampleSummaryBox.GetPreferredSize(this.exampleSummaryBox.Size); this.exampleSummaryBox.Height += 30; this.exampleSummaryBox.Refresh(); string sourceCodeFilename = this.GetSourceCodeFilename(selectedExample); this.DoubleBuffered = true; try { if (sourceCodeFilename == null) { this.sourceTextBox.Text = ""; this.sourceTextBox.AppendText("源码没有找到(Example source code was not found). " + Environment.NewLine + "在源文件-属性-复制到输出目录 修改为 始终复制 " + Environment.NewLine + "(Go to the properties of the source file in Visual Studio and set 'Copy To Output Directory' to 'Copy if newer')."); } else { RichTextBox rtb = new RichTextBox(); rtb.Font = this.sourceTextBox.Font; rtb.SelectionTabs = this.sourceTextBox.SelectionTabs; EfficientTextBox targetTextBox = new EfficientTextBox(rtb); StreamReader streamReader = new StreamReader(sourceCodeFilename); while (true) { string s = streamReader.ReadLine(); if (s != null) { this.PrintWithSyntaxHighlighting(targetTextBox, s); } else { break; } } targetTextBox.Flush(); streamReader.Close(); this.sourceTextBox.Rtf = rtb.Rtf; } } finally { this.ResumeLayout(true); } }