如何将 Microsoft 自动图形布局 (msagl) 中的图形集成到特定表单,以便我可以拖动图形,因为它是按钮或文本框
我刚开始使用 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、Form1 和 MSAGL 图表 连续/单独显示(<强>MSAGL graph 不是 Form1 中的元素)。
我的问题是:
这是我想要创建的 Windows 窗体,以便 MSAGL 图形可以是我想要在这个绿色区域中创建的 Form1 的元素。 (如果可能的话,可以拖动/修改属性,因为它是按钮或表单中的任何元素)。如何实施?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为在 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.