如何将 Microsoft Automatic Graph Layout (msagl) 中的图形集成到特定表单中,以便我可以将图形作为按钮或文本框进行拖动

How to integrate a graph in Microsoft Automatic Graph Layout (msagl) to a particular form so that in I can drag the graph as it is a Button or textBox

我刚开始使用 C# 并尝试创建 Windows Form App 项目。

Form1.Designer.cshere 下面是 Form1.cs打算创建)

using System;
using System.Windows.Forms;

namespace LearnCSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            /* --Upper bound of Block-- A */
            System.Windows.Forms.Form form = new System.Windows.Forms.Form();
            //create a viewer object 
            Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
            //create a graph object 
            Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");
            //create the graph content 
            graph.AddEdge("A", "B");
            graph.AddEdge("B", "C");
            graph.AddEdge("A", "C").Attr.Color = Microsoft.Msagl.Drawing.Color.Green;
            graph.FindNode("A").Attr.FillColor = Microsoft.Msagl.Drawing.Color.Magenta;
            graph.FindNode("B").Attr.FillColor = Microsoft.Msagl.Drawing.Color.MistyRose;
            Microsoft.Msagl.Drawing.Node c = graph.FindNode("C");
            c.Attr.FillColor = Microsoft.Msagl.Drawing.Color.PaleGreen;
            c.Attr.Shape = Microsoft.Msagl.Drawing.Shape.Diamond;
            //bind the graph to the viewer 
            viewer.Graph = graph;
            //associate the viewer with the form 
            form.SuspendLayout();
            viewer.Dock = System.Windows.Forms.DockStyle.Fill;
            form.Controls.Add(viewer);
            form.ResumeLayout();
            //show the form 
            form.ShowDialog();
            /* --Lower bound of Block-- A */
        }
    }
}

Block A 是我从 MSAGL code sample

得到的模板

当我构建解决方案和 运行 .exe、Form1MSAGL 图 显示consecutively/separatelyMSAGL图不是Form1[=35中的元素=]).


我的问题是: 这是我要创建的 windows 表单,以便 MSAGL 图可以成为我要在这个绿色区域中创建的 Form1 的元素。 (如果可能,可以 dragged/modified 属性,因为它是按钮或表单中的任何元素)。如何实施?

那是因为在 Form1 的构造函数中,您创建了一个全新的独立表单: System.Windows.Forms.Form form = new System.Windows.Forms.Form(); 并显示它: form.ShowDialog();

相反,只需将图形添加到 Form1: this.Controls.Add(viewer); 并且由于您构建了所有这些并将其添加到构造函数中,因此无需 SuspendLayout/ResumeLayout.

所以再次总结一下你应该做什么,删除这一行:

System.Windows.Forms.Form form = new System.Windows.Forms.Form();

和任何引用 form 的行,然后添加此行:

this.Controls.Add(viewer);

并保留所有其他行。