如何将 Microsoft 自动图形布局 (msagl) 中的图形集成到特定表单,以便我可以拖动图形,因为它是按钮或文本框

发布于 2025-01-14 05:45:51 字数 2450 浏览 2 评论 0 原文

我刚开始使用 C# 并尝试创建 Windows 窗体应用程序项目。

Form1.Designer.cs 位于此处,下面是 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 代码示例

当我构建解决方案并运行 .exe、Form1MSAGL 图表 连续/单独显示(<强>MSAGL graph 不是 Form1 中的元素)。


我的问题是: 这是我想要创建的 Windows 窗体,以便 MSAGL 图形可以是我想要在这个绿色区域中创建的 Form1 的元素。 (如果可能的话,可以拖动/修改属性,因为它是按钮或表单中的任何元素)。如何实施? 表单

I am new to using C# and trying to create a Windows Form App project.

Form1.Designer.cs is here and below is the Form1.cs (the form that I intend to create)

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 is a template that I get from MSAGL code sample

When I build the solution and run the .exe, the Form1 and the MSAGL graph show consecutively/separately (The MSAGL graph is not an element in Form1).


My question is:
Here is the windows form that I want to create so that the MSAGL graph can be the element of Form1 that I want to create in this green area. (and if possible can be dragged/modified the properties as it is a button or any element in the form). How to implement that?
Form

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强者自强 2025-01-21 05:45:51

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

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

因此,再次回顾一下您应该做什么,请删除此行:

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

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

this.Controls.Add(viewer);

并保留所有其他行不变。

Well that's because inside the constructor for Form1, you create a totally new and separate form:
System.Windows.Forms.Form form = new System.Windows.Forms.Form();
and display it:
form.ShowDialog();

Instead, just add the graph to Form1:
this.Controls.Add(viewer);
and since your building all this and adding it in the constructor, no need to SuspendLayout/ResumeLayout.

So once again, to recap what you should do, delete this line:

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

and any lines that reference form, and then add this line:

this.Controls.Add(viewer);

and leave all other lines as is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文