using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Visio; using AxMicrosoft.Office.Interop.VisOcx; namespace Lskj.Visio { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } /// /// 模块类 /// private class MouduleClass { public string MouduleId;//模块Id public string MouduleName;//模块名称 public string MouduleDll; //模块绑定dll } /// /// 连接形状(暂未用) /// /// /// /// private static void ConnectShapes(Microsoft.Office.Interop.Visio.Shape shape1, Microsoft.Office.Interop.Visio.Shape shape2, Microsoft.Office.Interop.Visio.Shape connector) { 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); beginXCell.GlueTo(shape1.get_CellsSRC( (short)Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject, (short)Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut, (short)Microsoft.Office.Interop.Visio.VisCellIndices.visXFormPinX)); 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); endXCell.GlueTo(shape2.get_CellsSRC( (short)Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject, (short)Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut, (short)Microsoft.Office.Interop.Visio.VisCellIndices.visXFormPinX)); } protected override void OnLoad(EventArgs e) { //axDrawingControl1.Window.Zoom = 0.5; //设置缩放比例 axDrawingControl1.Window.ShowScrollBars = (short)Microsoft.Office.Interop.Visio.VisScrollbarStates.visScrollBarBoth; //滚动条属性 axDrawingControl1.Window.ShowRulers = 100;//标尺 axDrawingControl1.Window.BackgroundColor = (uint)ColorTranslator.ToOle(System.Drawing.Color.White); axDrawingControl1.Window.BackgroundColorGradient = (uint)ColorTranslator.ToOle(System.Drawing.Color.White); axDrawingControl1.Window.ZoomBehavior = Microsoft.Office.Interop.Visio.VisZoomBehavior.visZoomVisio; axDrawingControl1.Window.ShowPageTabs = false; //visio 的内容是从1开始的 Microsoft.Office.Interop.Visio.Page currentPage = axDrawingControl1.Document.Pages[1]; //通过 openEx打开模板 Microsoft.Office.Interop.Visio.Document currentStencil = axDrawingControl1.Document.Application.Documents.OpenEx("D:\\ERP_CS_New\\插件库\\Lskj.Visio\\系统模块模型.vss", (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked); //默认模具 Microsoft.Office.Interop.Visio.Shape connector = currentPage.Drop(currentStencil.Masters["连接"], 4.50, 5.50); connector.Delete(); var targetApplication = (Microsoft.Office.Interop.Visio.Application)axDrawingControl1.Window.Application; EventSink visioEventSink = new EventSink(); visioEventSink.AddAdvise(targetApplication, currentStencil); visioEventSink.OnTextEdit += new Lskj.Visio.EventSink.VisioEventHandler(ShapeExitedTextEdit); //通过实验发现用软件默认能添加箭头,是因为模版,对于没模版只有通过创建一次 } private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Visio.Page currentPage = axDrawingControl1.Document.Pages[1]; int i = 0; List moudules = new List(); //连接组合 List shapeConnects = new List(); foreach (Shape sp in currentPage.Shapes) { string strShapes = ";"; //找连接线 if (sp.FillStyle == "Connector") { foreach (Connect connect in sp.Connects) { //获取连接组合 strShapes += GetConnectShape(sp, connect.ToSheet); } string[] shapes = strShapes.Split(';'); ConnectShape con = new ConnectShape(); //连接线起始图形 con.StartShape = currentPage.Shapes.get_ItemFromID(Convert.ToInt32(shapes[1])); //连接到的图形 if (!string.IsNullOrEmpty(shapes[2])) con.EndShape = currentPage.Shapes.get_ItemFromID(Convert.ToInt32(shapes[2])); shapeConnects.Add(con); } if (sp.FillStyle == "基本") { //sp.ShapeExitedTextEdit += new EShape_ShapeExitedTextEditEventHandler(ShapeExitedTextEdit); Cell cell1 = sp.get_CellsSRC((short)VisSectionIndices.visSectionProp, (short)0, (short)0); Cell cell2 = sp.get_CellsSRC((short)VisSectionIndices.visSectionProp, (short)1, (short)0); //字段属性 Cell cell3 = sp.get_CellsSRC((short)VisSectionIndices.visSectionProp, (short)2, (short)0); MouduleClass moudule = new MouduleClass(); moudule.MouduleId = FormulaToString(cell1.Formula); moudule.MouduleName = FormulaToString(cell2.Formula); moudule.MouduleDll = FormulaToString(cell3.Formula); moudules.Add(moudule); } } } /// /// 修改模块名称事件 /// /// private void ShapeExitedTextEdit(object sender, EventArgs e) { Shape shapeItem = sender as Shape; Cell cell = shapeItem.get_CellsSRC((short)VisSectionIndices.visSectionProp, (short)1, (short)0); cell.FormulaU = "\"" + shapeItem.Text + "\""; } //Cell.Formula默认带两个双引号和杠,进行格式化处理 private string FormulaToString(string formula) { string Formula = string.Empty; const string flag = "\""; if (formula.Contains(flag)) { Formula = formula.Replace(flag, ""); } return Formula; } /// /// 连接组合ID /// /// /// /// private string GetConnectShape(Shape shape, Shape toFromSheet) { string strShapes = string.Empty; if (toFromSheet != null) { if (shape.ID != toFromSheet.ID) { strShapes += string.Format("{0};", toFromSheet.ID); } } return strShapes; } //连接组合 连接线先后 public class ConnectShape { //起始图形 public Shape StartShape; //连接到的图形 public Shape EndShape; } } }