我正在尝试使用富文本框控件设计一个类似 IDE(不可编辑)的程序。基本上,每当用户单击 +/- 按钮时,我需要位于 RTB 左侧的树视图来展开/折叠我的代码的某个部分。可扩展的可折叠范围定义为可以看到大括号的任何位置。例如,在 RTB 中,如果我有类似的东西:
int main()
{
if (...)
{
if (...)
{
}
}
else
{
}
}
如果我点击最上面的大括号,它会折叠主函数内的所有内容。基本上,大括号内包含的内容就是折叠的内容。所以总而言之,我正在尝试设计一些与 Visual Studio 的展开/折叠代码功能非常相似的东西,除了它也可以使用 if/else 功能。
我知道括号匹配算法,并且我实现了一个堆栈以了解哪些括号对匹配(存储在元组列表中的行号)。
我遇到的主要问题是如何设计实际的树视图。我需要树视图以线性方式,其中没有节点被添加到另一个之上。我不知道有任何方法可以添加展开/折叠按钮,而无需在另一个节点上实际添加子节点。
此外,除了 +/- 按钮和单个垂直线之外,我需要树视图节点不可编辑、不可见和不可点击。
最后,这是假设如果我满足上述要求,我还需要 RTB 的垂直滚动事件来正确滚动树视图。也就是说,Treeview 的折叠/展开部分将根据 RTB 上可见的代码部分进行更新。
这是我用来初始化树的一段代码:
public partial class LogicSimulationViewerForm : Form
{
private List<Tuple<string,Boolean>> visibleLines = new List<Tuple<string,Boolean>>();
private List<Tuple<int, int>> collapseRange = new List<Tuple<int, int>>();
private void TreeInit()
{
TreeNode tn;
Stack<int> openBracketLine = new Stack<int>();
int i = 0;
TreeLogicCode.Nodes.Clear();
foreach (string s in rtbLogicCode.Lines)
{
visibleLines.Add(Tuple.Create(s, true));
if (s == "{")
{
openBracketLine.Push(i);
}
else if (s == "}")
{
collapseRange.Add(Tuple.Create(openBracketLine.Pop(),i));
}
i++;
}
}
这是 Designer.sc 源代码,虽然我相信这不是必需的,但以防万一:
namespace DDCUI
{
partial class LogicSimulationViewerForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.TreeLogicCode = new System.Windows.Forms.TreeView();
this.labelLogicCode = new System.Windows.Forms.Label();
this.rtbLogicCode = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// TreeLogicCode
//
this.TreeLogicCode.Dock = System.Windows.Forms.DockStyle.Left;
this.TreeLogicCode.Location = new System.Drawing.Point(50, 0);
this.TreeLogicCode.Name = "TreeLogicCode";
this.TreeLogicCode.Scrollable = false;
this.TreeLogicCode.Size = new System.Drawing.Size(40, 600);
this.TreeLogicCode.TabIndex = 4;
//
// labelLogicCode
//
this.labelLogicCode.BackColor = System.Drawing.Color.LightGray;
this.labelLogicCode.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.labelLogicCode.Dock = System.Windows.Forms.DockStyle.Left;
this.labelLogicCode.ForeColor = System.Drawing.SystemColors.ControlText;
this.labelLogicCode.Location = new System.Drawing.Point(0, 0);
this.labelLogicCode.Margin = new System.Windows.Forms.Padding(3);
this.labelLogicCode.Name = "labelLogicCode";
this.labelLogicCode.Padding = new System.Windows.Forms.Padding(3);
this.labelLogicCode.Size = new System.Drawing.Size(50, 600);
this.labelLogicCode.TabIndex = 3;
this.labelLogicCode.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// rtbLogicCode
//
this.rtbLogicCode.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtbLogicCode.Location = new System.Drawing.Point(90, 0);
this.rtbLogicCode.Name = "rtbLogicCode";
this.rtbLogicCode.Size = new System.Drawing.Size(510, 600);
this.rtbLogicCode.TabIndex = 5;
this.rtbLogicCode.Text = "";
this.rtbLogicCode.VScroll += new System.EventHandler(this.rtbLogicCode_VScroll);
//
// LogicSimulationViewerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(600, 600);
this.Controls.Add(this.rtbLogicCode);
this.Controls.Add(this.TreeLogicCode);
this.Controls.Add(this.labelLogicCode);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "LogicSimulationViewerForm";
this.Text = "LogicSimulationViewerForm";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TreeView TreeLogicCode;
private System.Windows.Forms.Label labelLogicCode;
private System.Windows.Forms.RichTextBox rtbLogicCode;
}
}
我非常感谢任何有关解决此问题的指导。提前致谢。