winform下显示XML信息(非WPF)

之前使用的是FastColoredTextBox,github地址如下:

https://github.com/PavelTorgashov/FastColoredTextBox 

但是有个问题,它支持中文,wordwrap=true,自动换行时,无法正常显示

如果在中文XML下使用它的话,建议加水平滚动条,不要将wordwrap设置为true

因反馈意见,修改控件为ScintillaNET

GitHub - jacobslusser/ScintillaNET: A Windows Forms control, wrapper, and bindings for the Scintilla text editor.A Windows Forms control, wrapper, and bindings for the Scintilla text editor. - jacobslusser/ScintillaNETicon-default.png?t=N7T8https://github.com/jacobslusser/ScintillaNET它本身带的demo显示XML就是基本的显示,无法折叠节点

另外几个比较全面的Demo示例地址:

https://github.com/hgupta9/ScintillaNET.Demo

https://github.com/uuf6429/ScintillaNET-Kitchen

这两个,一个是显示cpp代码状态下的例子,一个是设置样式之类的操作。不符合我修改的内容。

===============================================

代码如下:

Scintilla scintilla = new Scintilla();
scintilla.WrapMode = WrapMode.Word;

scintilla.Lexer = Lexer.Xml;
scintilla.Dock = DockStyle.Fill;
//设置XML文本内容
scintilla.Text = File.ReadAllText(filePath, Encoding.UTF8);


// Configure folding
scintilla.SetProperty("fold", "1");
scintilla.SetProperty("fold.compact", "1");
scintilla.SetProperty("fold.html", "1");

// Set margin for folding symbols
scintilla.Margins[2].Type = MarginType.Symbol;
scintilla.Margins[2].Mask = Marker.MaskFolders;
scintilla.Margins[2].Sensitive = true;
scintilla.Margins[2].Width = 20;

// Define folding markers with respective symbols
for (int i = Marker.FolderEnd; i <= Marker.FolderOpen; i++)
{
    scintilla.Markers[i].SetForeColor(SystemColors.ControlLightLight);
    scintilla.Markers[i].SetBackColor(SystemColors.ControlDark);
}

//折叠的样式配置 1
scintilla.Markers[Marker.Folder].Symbol = MarkerSymbol.BoxPlus;
scintilla.Markers[Marker.FolderOpen].Symbol = MarkerSymbol.BoxMinus;
scintilla.Markers[Marker.FolderEnd].Symbol = MarkerSymbol.BoxPlusConnected;
scintilla.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
scintilla.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.BoxMinusConnected;
scintilla.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine;
scintilla.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner;

//折叠的样式配置 2
//scintilla.Markers[Marker.Folder].Symbol = MarkerSymbol.CirclePlus ;
//scintilla.Markers[Marker.FolderOpen].Symbol = MarkerSymbol.CircleMinus ;
//scintilla.Markers[Marker.FolderEnd].Symbol = MarkerSymbol.CirclePlusConnected ;
//scintilla.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
//scintilla.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.CircleMinusConnected;
//scintilla.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine;
//scintilla.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner;

//XML显示的样式信息,包含节点和INNERTEXT,ATTRIBUTE等
scintilla.Styles[ScintillaNET.Style.Xml.Default].Font = "宋体";
scintilla.Styles[ScintillaNET.Style.Xml.Default].Size = 10;
scintilla.Styles[ScintillaNET.Style.Xml.Tag].Font = "Verdana";
scintilla.Styles[ScintillaNET.Style.Xml.Tag].ForeColor = Color.DarkRed;
scintilla.Styles[ScintillaNET.Style.Xml.Tag].Size = 10;
scintilla.Styles[ScintillaNET.Style.Xml.Attribute].Font = "Verdana";
scintilla.Styles[ScintillaNET.Style.Xml.Attribute].ForeColor = Color.Red;
scintilla.Styles[ScintillaNET.Style.Xml.Attribute].Size = 10;
scintilla.Styles[ScintillaNET.Style.Xml.Number].Font = "Verdana";
scintilla.Styles[ScintillaNET.Style.Xml.Number].Size = 10;
scintilla.Styles[ScintillaNET.Style.Xml.TagEnd].Font = "Verdana";
scintilla.Styles[ScintillaNET.Style.Xml.TagEnd].Size = 10;
scintilla.Styles[ScintillaNET.Style.Xml.XmlStart].Font = "Verdana";
scintilla.Styles[ScintillaNET.Style.Xml.XmlStart].Size = 10;
scintilla.Styles[ScintillaNET.Style.Xml.XmlEnd].Font = "Verdana";
scintilla.Styles[ScintillaNET.Style.Xml.XmlEnd].Size = 10;
scintilla.Styles[ScintillaNET.Style.Xml.CData].Font = "Verdana";
scintilla.Styles[ScintillaNET.Style.Xml.CData].Size = 10;
scintilla.Styles[ScintillaNET.Style.Xml.Value].Font = "Verdana";
scintilla.Styles[ScintillaNET.Style.Xml.Value].Size = 10;

//显示XML行号
scintilla.Margins[0].Width = 40; 
scintilla.Margins[0].Type = MarginType.Number;

// Enable automatic folding
scintilla.AutomaticFold = (AutomaticFold.Show | AutomaticFold.Click | AutomaticFold.Change);

//自定义对于所有的"<",">" 显示蓝色
int indicatorNumber = 0;
//这个定义的是底色
//scintilla1.Indicators[indicatorNumber].Style = IndicatorStyle.StraightBox;
//这个定义的是字体颜色
scintilla.Indicators[indicatorNumber].Style = IndicatorStyle.TextFore;
scintilla.Indicators[indicatorNumber].ForeColor = Color.Blue;

int startPosition = 0;
while (true)
{
    int position = scintilla.Text.IndexOf(">", startPosition);
    if (position == -1) break;
    scintilla.IndicatorCurrent = indicatorNumber;
    scintilla.IndicatorFillRange(position, 1);
    startPosition = position + 1;
}

startPosition = 0;
while (true)
{
    int position = scintilla.Text.IndexOf("<", startPosition);
    if (position == -1) break;
    scintilla.IndicatorCurrent = indicatorNumber;
    scintilla.IndicatorFillRange(position, 1);
    startPosition = position + 1;
}

scintilla.ReadOnly = true;
this.Controls.Add(scintilla);

新建一个WINFORM,然后写在load方法中即可。记得添加ScintillaNET的引用,或者在nuget上拉取,都可以。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部