当前位置:   article > 正文

开源节点框架STNodeEditor使用_stnodeeditor免费?

stnodeeditor免费?

节点,一般都为树形Tree结构,如TreeNode,XmlNode。

树形结构有其关键属性Parent【父节点】,Children【子节点】

LinkedListNode为链表线性结构,有其关键属性Next【下一个】,Previous【上一个】,

可以用其进行工作流workFlow设计

右键 项目 STNodeDemo,管理NuGet程序包

输入关键字STNodeEditor

安装完成后

我们可以查看工具箱

开源的 .NET 轻量级且功能强大的节点编辑器STNodeEditor

STNodeEditor 是一个轻量且功能强大的节点编辑器 使用方式非常简洁 提供了丰富的属性以及事件可以非常方便的完成节点之间数据的交互及通知 大量的虚函数可供开发者重写具有很高的自由性。

当有很多应用程序(模块) 它们之间需要相互调用传递数据来完成一整套流程的工作 开发单一功能的应用程序(模块)相对比较容易 而实现一整套很多功能相互调用的应用程序相对比较繁琐 此套框架开发者只需要定义好传递的数据类型 然后分别实现单一节点功能 至于执行流程交给框架和用户布线即可。

项目地址

https://github.com/DebugST/STNodeEditor

STNodeEditor是基于WinForm的一套框架 使用GDI+开发 不包含任何额外依赖 整个调用库仅100+kb 

项目主页:https://debugst.github.io/STNodeEditor/

教程文档:https://debugst.github.io/STNodeEditor/doc_cn.html 

NuGet:https://www.nuget.org/packages/ST.Library.UI/

GitHub:https://github.com/DebugST/STNodeEditor 

由上图可见 STNodeEditor 包含3部分 TreeView PropertyGrid NodeEditor 这三部分组成了一套完整的可使用框架

TreeView
开发这可以把执行功能编码到一个节点中 而TreeView则负责展示以及检索节点 在TreeView中的节点可直接拖拽添加到NodeEditor中
PropertyGrid
类似与WinForm开发使用的属性窗口 作为一个节点 它也是可以有属性的 而作者在编辑器进行设计的过程中也把一个节点视作一个Form让开发者几乎没有什么学习成本直接上手一个节点的开发
NodeEditor
NodeEditor是用户组合自己执行流程的地方 使得功能模块执行流程可视化
 

因ST.Library.UI.NodeEditor.STNode 是一个抽象类,因此我们需要新建一个子类SnakeNode 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using ST.Library.UI.NodeEditor;
  9. namespace STNodeDemo
  10. {
  11. public class SnakeNode : ST.Library.UI.NodeEditor.STNode
  12. {
  13. public SnakeNode(string title)
  14. {
  15. this.Title = title;
  16. this.TitleColor = Color.FromArgb(200, Color.Goldenrod);
  17. }
  18. /// <summary>
  19. /// 输出节点集合
  20. /// </summary>
  21. public List<STNodeOption> OutputOptionCollection = new List<STNodeOption>();
  22. STNodeOption outputTest;
  23. protected override void OnCreate()
  24. {
  25. base.OnCreate();
  26. this.Title = "SnakeNode";
  27. //输入项集合
  28. int index = this.InputOptions.Add(new STNodeOption("Input1", typeof(string), false));
  29. STNodeOption nodeIn2 = this.InputOptions.Add("Input2", typeof(int), false);
  30. STNodeOption nodeIn3 = this.InputOptions.Add("Input3", typeof(float), false);
  31. //输出项集合
  32. STNodeOption nodeOut = this.OutputOptions.Add("Output1", typeof(string), false);
  33. outputTest = this.OutputOptions.Add("OutputTime", typeof(DateTime), false);
  34. OutputOptionCollection.Add(nodeOut);
  35. OutputOptionCollection.Add(outputTest);
  36. //STNodeOption[] ss = this.GetOutputOptions();
  37. }
  38. //当所有者发生改变(即:在NodeEditor中被添加或移除)
  39. //应当像容器提交自己拥有数据类型的连接点 所期望显示的颜色
  40. //颜色主要用于区分不同的数据类型
  41. protected override void OnOwnerChanged()
  42. {
  43. base.OnOwnerChanged();
  44. if (this.Owner == null) return;
  45. this.Owner.SetTypeColor(typeof(string), Color.Yellow);
  46. //当前容器中已有的颜色会被替换
  47. this.Owner.SetTypeColor(typeof(int), Color.DodgerBlue, true);
  48. this.Owner.SetTypeColor(typeof(float), Color.Pink, true);
  49. this.Owner.SetTypeColor(typeof(DateTime), Color.Green, true);
  50. //下面的代码将忽略容器中已有的颜色
  51. //this.SetOptionDotColor(op, Color.Red); //无需在OnOwnerChanged()中设置
  52. Task.Factory.StartNew(() =>
  53. {
  54. for (int i = 0; i < 50; i++)
  55. {
  56. System.Threading.Thread.Sleep(1000);
  57. //STNodeOption.TransferData(object)会自动设置STNodeOption.Data
  58. //然后自动向所有连接的选项进行数据传递
  59. outputTest.TransferData(DateTime.Now);
  60. }
  61. });
  62. }
  63. }
  64. }

新建测试接收节点类ViperNode

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using ST.Library.UI.NodeEditor;
  9. namespace STNodeDemo
  10. {
  11. public class ViperNode : ST.Library.UI.NodeEditor.STNode
  12. {
  13. public ViperNode(string title)
  14. {
  15. this.Title = title;
  16. this.TitleColor = Color.FromArgb(200, Color.Goldenrod);
  17. }
  18. /// <summary>
  19. /// 输入节点集合
  20. /// </summary>
  21. public List<STNodeOption> InputOptionCollection = new List<STNodeOption>();
  22. STNodeOption inputTest;
  23. protected override void OnCreate()
  24. {
  25. base.OnCreate();
  26. this.Title = "ViperNode";
  27. //输入项集合
  28. inputTest = this.InputOptions.Add("ViperTime", typeof(DateTime), false);
  29. inputTest.DataTransfer += new STNodeOptionEventHandler(InputTest_DataTransfer);
  30. InputOptionCollection.Add(inputTest);
  31. }
  32. private void InputTest_DataTransfer(object sender, STNodeOptionEventArgs e)
  33. {
  34. //当连接的建立与断开都会触发此事件 所以需要判断连接状态
  35. if (e.Status != ConnectionStatus.Connected || e.TargetOption.Data == null)
  36. {
  37. //当 STNode.AutoSize=true 并不建议使用STNode.SetOptionText
  38. //因为当文本发生改变时候会重新计算布局 正确的做法是自定义一个如Lable控件
  39. //作为时间的显示 当然这里为了演示方式采用此方案
  40. this.SetOptionText(inputTest, "--");
  41. }
  42. else
  43. {
  44. this.SetOptionText(inputTest, Convert.ToDateTime(e.TargetOption.Data).ToString("yyyy-MM-dd HH:mm:ss.fff"));
  45. }
  46. }
  47. }
  48. }

新建测试窗体FormSTNode,设计如下

FormSTNode.Designer.cs设计器代码如下:

  1. namespace STNodeDemo
  2. {
  3. partial class FormSTNode
  4. {
  5. /// <summary>
  6. /// 必需的设计器变量。
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9. /// <summary>
  10. /// 清理所有正在使用的资源。
  11. /// </summary>
  12. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13. protected override void Dispose(bool disposing)
  14. {
  15. if (disposing && (components != null))
  16. {
  17. components.Dispose();
  18. }
  19. base.Dispose(disposing);
  20. }
  21. #region Windows 窗体设计器生成的代码
  22. /// <summary>
  23. /// 设计器支持所需的方法 - 不要修改
  24. /// 使用代码编辑器修改此方法的内容。
  25. /// </summary>
  26. private void InitializeComponent()
  27. {
  28. this.stNodeEditor1 = new ST.Library.UI.NodeEditor.STNodeEditor();
  29. this.btnConnectLine = new System.Windows.Forms.Button();
  30. this.SuspendLayout();
  31. //
  32. // stNodeEditor1
  33. //
  34. this.stNodeEditor1.AllowDrop = true;
  35. this.stNodeEditor1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(34)))), ((int)(((byte)(34)))), ((int)(((byte)(34)))));
  36. this.stNodeEditor1.Curvature = 0.3F;
  37. this.stNodeEditor1.Location = new System.Drawing.Point(12, 67);
  38. this.stNodeEditor1.LocationBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(120)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
  39. this.stNodeEditor1.MarkBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
  40. this.stNodeEditor1.MarkForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
  41. this.stNodeEditor1.MinimumSize = new System.Drawing.Size(100, 100);
  42. this.stNodeEditor1.Name = "stNodeEditor1";
  43. this.stNodeEditor1.Size = new System.Drawing.Size(1160, 475);
  44. this.stNodeEditor1.TabIndex = 0;
  45. this.stNodeEditor1.Text = "stNodeEditor1";
  46. //
  47. // btnConnectLine
  48. //
  49. this.btnConnectLine.Font = new System.Drawing.Font("宋体", 16F);
  50. this.btnConnectLine.Location = new System.Drawing.Point(52, 12);
  51. this.btnConnectLine.Name = "btnConnectLine";
  52. this.btnConnectLine.Size = new System.Drawing.Size(169, 33);
  53. this.btnConnectLine.TabIndex = 1;
  54. this.btnConnectLine.Text = "连线-显示时间";
  55. this.btnConnectLine.UseVisualStyleBackColor = true;
  56. this.btnConnectLine.Click += new System.EventHandler(this.btnConnectLine_Click);
  57. //
  58. // FormSTNode
  59. //
  60. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  61. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  62. this.ClientSize = new System.Drawing.Size(1200, 554);
  63. this.Controls.Add(this.btnConnectLine);
  64. this.Controls.Add(this.stNodeEditor1);
  65. this.Name = "FormSTNode";
  66. this.Text = "STNodeEditor开源框架,输入输出";
  67. this.Load += new System.EventHandler(this.FormSTNode_Load);
  68. this.ResumeLayout(false);
  69. }
  70. #endregion
  71. private ST.Library.UI.NodeEditor.STNodeEditor stNodeEditor1;
  72. private System.Windows.Forms.Button btnConnectLine;
  73. }
  74. }

窗体FormSTNode.cs测试代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using ST.Library.UI.NodeEditor;
  11. namespace STNodeDemo
  12. {
  13. public partial class FormSTNode : Form
  14. {
  15. public FormSTNode()
  16. {
  17. InitializeComponent();
  18. //参考地址 https://blog.csdn.net/crystal_lz/article/details/117131080
  19. /*single-connection
  20. 单连接模式 在单连接模式下一个连接点同时 只能被一个 同数据类型点的连接
  21. multi-connection
  22. 多连接模式 在多连接模式下一个连接点同时 可以被多个 同数据类型点连接
  23. 数据交互:
  24. STNodeOption可以通过绑定DataTransfer事件获取到传入该选项的所有数据
  25. STNodeOption可以通过TransferData(object obj)向该选项上所有连接的选项进行数据投递
  26. */
  27. }
  28. private void FormSTNode_Load(object sender, EventArgs e)
  29. {
  30. SnakeNode stNode = new SnakeNode("STNode的标题");
  31. stNode.Location = new Point(10, 10);
  32. stNodeEditor1.Nodes.Add(stNode);
  33. ViperNode destNode = new ViperNode("显示时间");
  34. destNode.Location = new Point(400, 10);
  35. stNodeEditor1.Nodes.Add(destNode);
  36. }
  37. /// <summary>
  38. /// 获取指定的输出节点选项
  39. /// </summary>
  40. /// <param name="node"></param>
  41. /// <param name="strText"></param>
  42. /// <returns></returns>
  43. private STNodeOption GetOutNodeOption(SnakeNode node, string strText)
  44. {
  45. STNodeOption outNodeOption = node.OutputOptionCollection.FirstOrDefault(option => option.Text == strText);
  46. return outNodeOption;
  47. }
  48. /// <summary>
  49. /// 获取指定的输入节点选项
  50. /// </summary>
  51. /// <param name="node"></param>
  52. /// <param name="strText"></param>
  53. /// <returns></returns>
  54. private STNodeOption GetInNodeOption(ViperNode node, string strText)
  55. {
  56. STNodeOption inNodeOption = node.InputOptionCollection.FirstOrDefault(option => option.Text == strText);
  57. return inNodeOption;
  58. }
  59. private void btnConnectLine_Click(object sender, EventArgs e)
  60. {
  61. STNodeOption outNodeOption = GetOutNodeOption((SnakeNode)stNodeEditor1.Nodes[0], "OutputTime");
  62. STNodeOption inNodeOption = GetInNodeOption((ViperNode)stNodeEditor1.Nodes[1], "ViperTime");
  63. ConnectionStatus connectionStatus = outNodeOption.ConnectOption(inNodeOption);
  64. MessageBox.Show($"输出节点 连接 输入节点 的状态结果【{connectionStatus}】", "提示");
  65. }
  66. }
  67. }

测试运行如图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/128884
推荐阅读
相关标签
  

闽ICP备14008679号