刚好有个博文有代码介绍连接两个矩形: http://blog.csdn.net/namespace20100823/article/details/7338628 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Visio;namespace 连线测试 { public partial class Form1 : Form { private Page m_page; private Master m_master; private Document m_doc; Microsoft.Office.Interop.Visio.Shape connector; public Form1() { InitializeComponent(); }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Visio;namespace 连线测试
{
public partial class Form1 : Form
{
private Page m_page;
private Master m_master;
private Document m_doc;
Microsoft.Office.Interop.Visio.Shape connector; public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Visio.Application m_app = new Microsoft.Office.Interop.Visio.Application();
m_doc = m_app.Documents.Add("");
m_doc = m_app.Documents.OpenEx("Basic Shapes.vss", (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked);
m_page = m_app.ActivePage; //添加第一个矩形
m_master = m_doc.Masters.get_ItemU(@"Rectangle");
Microsoft.Office.Interop.Visio.Shape m_sp1 = m_page.Drop(m_master, 1.0, 1.0);
m_sp1.Text = @"第一个矩形";
//添加第二个矩形
m_master = m_doc.Masters.get_ItemU(@"Rectangle");
Microsoft.Office.Interop.Visio.Shape m_sp2 = m_page.Drop(m_master, 3.0, 3.0);
m_sp2.Text = @"第二个矩形";
//调用ConnectTogether方法,参数是两个矩形的id号,这里要说的是,visio中添加的第1个图形,id号是1,第二个是2,依此类推
ConnectTogether(1, 2);
MessageBox.Show("两个矩形连接完成了连接!");
}
/// <summary>
/// 接收两个矩形的id号,然后将两个矩形用“动态连接线”连接起来
/// </summary>
/// <param name="BeginID"></param>
/// <param name="EndID"></param>
public void ConnectTogether(int BeginID, int EndID)
{
//添加动态连接线
connector = m_page.Drop(m_doc.Masters["动态连接线"], 4.50, 4.50);
//将动态连接线设置为直线,关键是要知道FormulaU这个属性,16表示是直线
connector.get_CellsSRC(
(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowShapeLayout,
(short)VisCellIndices.visSLORouteStyle).FormulaU = "16"; //获取动态连接线的始端
Cell beginXCell = connector.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject,
(short)Microsoft.Office.Interop.Visio.VisRowIndices.visRowXForm1D,
(short)Microsoft.Office.Interop.Visio.VisCellIndices.vis1DBeginX);
//获取第1个矩形的连接点
Cell FirstRectangleBeginPoint = m_page.Shapes.get_ItemFromID(BeginID).get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject,
(short)Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut,
(short)Microsoft.Office.Interop.Visio.VisCellIndices.visXFormPinX); //动态连接线的始端与第1个矩形的连接点连接
beginXCell.GlueTo(FirstRectangleBeginPoint); //获取动态连接线的末端
Cell endXCell = connector.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject,
(short)Microsoft.Office.Interop.Visio.VisRowIndices.visRowXForm1D,
(short)Microsoft.Office.Interop.Visio.VisCellIndices.vis1DEndX);
//获取第2个矩形的连接点
Cell ShapeConnectorEndPoint = m_page.Shapes.get_ItemFromID(EndID).get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject,
(short)Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut,
(short)Microsoft.Office.Interop.Visio.VisCellIndices.visXFormPinX); //动态连接线的末端与第2个矩形的连接点连接
endXCell.GlueTo(ShapeConnectorEndPoint);
}
}
}
关于保存,有三种方式: //保存已存在文件
this.Application.ActiveDocument.Save();
//保存为新文件名
string docPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\test\MyDrawing.vsd";
this.Application.ActiveDocument.SaveAs(docPath); //保存为新文件名并带参数
string newDocPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\test\MyOtherDrawing.vsd";
this.Application.ActiveDocument.SaveAsEx(newDocPath,
((short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visSaveAsRO +
(short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visSaveAsListInMRU));