刚刚在我的 Winform 中添加了一个 fastcoloredtextbox 并将语言设置为 XML。无法更改样式。我怎样才能改变蓝色和红色。
using FastColoredTextBoxNS;
using System.Drawing;
using System.Windows.Forms;
namespace JFlowV2
{
public partial class BaseFastColoredTextBox : FastColoredTextBox
{
public BaseFastColoredTextBox()
{
this.Load += BaseFastColoredTextBox_Load;
}
private void BaseFastColoredTextBox_Load(object sender, System.EventArgs e)
{
ApplyDarkTheme();
}
public void ApplyDarkTheme()
{
this.BackColor = Color.FromArgb(30, 30, 30); // Editor background
this.ForeColor = Color.FromArgb(220, 220, 220); // Editor text
this.IndentBackColor = Color.FromArgb(45, 45, 48); // Indentation background
this.ServiceLinesColor = Color.FromArgb(50, 50, 50); // Service lines
this.LineNumberColor = Color.FromArgb(43, 145, 175); // Line numbers
this.SelectionColor = Color.FromArgb(60, 75, 110, 175); // Selection color
this.CurrentLineColor = Color.FromArgb(40, 40, 40); // Current line highlight
}
}
}
您需要修改应用于每个 XML 元素(例如标签、属性和值)的样式。您还需要通过定义自己的文本样式来覆盖用于 XML 令牌的样式
因此,在您的情况下,添加到 ApplyDarkTheme() 中的现有代码中,如下所示
tagStyle = new TextStyle(new SolidBrush(Color.LightBlue), null, FontStyle.Regular); // Color for tags
attributeNameStyle = new TextStyle(new SolidBrush(Color.LightCoral), null, FontStyle.Regular); // Color for attribute names
attributeValueStyle = new TextStyle(new SolidBrush(Color.LightGreen), null, FontStyle.Regular); // Color for attribute values
// Apply the custom styles
this.Language = Language.Custom;
this.ClearStylesBuffer();
this.AddStyle(tagStyle);
this.AddStyle(attributeNameStyle);
this.AddStyle(attributeValueStyle);
// Set up the rules for XML elements
this.Range.ClearStyle(StyleIndex.All);
this.Range.SetStyle(tagStyle, @"</?[a-zA-Z]+"); // Tag name (e.g., <tag> or </tag>)
this.Range.SetStyle(attributeNameStyle, @"\b[a-zA-Z0-9_:-]+(?=\=)"); // Attribute names (e.g., name=)
this.Range.SetStyle(attributeValueStyle, @"""[^""]*"""); // Attribute values (e.g., "value")
如果您想了解更多相关内容,我建议您关注https://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting