用c#操作visio画图

   这学期的c++项目是做一个代码分析器,可以自动将选中的代码文件读入分析器进行自动分析,然后将画出的程序流程图从visio中导出,这个项目的难点是代码的分析和visio 画图,我已经把代码分析基本搞定了,现在最大的问题就是visio画图的问题,但是这周经过我们组的两个搞visio那一块的人的努力终于有了点成果,现在可以用c#画图了,虽然不是用vc.net但是已经很好了,估计很快就可以用vc.net画图了,因为他们的类库是基本一样的,现在分享一下用visio画图的经验,这些经验难得啊,花了我们很多时间啊。
    要想通过.net画visio图,要先将visio的com组件导入,就是在引用中添加visio的com组件,导入玩之后就可以写代码画图了。贴个简单的示例代码,便于大家理解:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Visio = Microsoft.Office.Interop.Visio;
using AxVisOcx = AxMicrosoft.Office.Interop.VisOcx;
//using System.Web.UI.WebControls;
using Cell = Microsoft.Office.Interop.Visio.Cell;

namespace visiotest1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private static void ConnectShapes(Visio.Shape shape1, Visio.Shape shape2, Visio.Shape connector)
        {
            // get the cell from the source side of the connector

            Cell beginXCell = connector.get_CellsSRC(

            (short)Visio.VisSectionIndices.visSectionObject,


            (short)Visio.VisRowIndices.visRowXForm1D,

            (short)Visio.VisCellIndices.vis1DBeginX);

            // glue the source side of the connector to the first shape

            beginXCell.GlueTo(shape1.get_CellsSRC(

            (short)Visio.VisSectionIndices.visSectionObject,

            (short)Visio.VisRowIndices.visRowXFormOut,

            (short)Visio.VisCellIndices.visXFormPinX));

            // get the cell from the destination side of the connector

            Cell endXCell = connector.get_CellsSRC(

            (short)Visio.VisSectionIndices.visSectionObject,

            (short)Visio.VisRowIndices.visRowXForm1D,

            (short)Visio.VisCellIndices.vis1DEndX);

            // glue the destination side of the connector to the second shape

            endXCell.GlueTo(shape2.get_CellsSRC(

            (short)Visio.VisSectionIndices.visSectionObject,

            (short)Visio.VisRowIndices.visRowXFormOut,

            (short)Visio.VisCellIndices.visXFormPinX));

        }


        private void Form2_Load(object sender, EventArgs e)
        {
            axDrawingControl1.Window.Zoom = 0.5;
            axDrawingControl1.Window.ShowScrollBars = (short)Visio.VisScrollbarStates.visScrollBarBoth;
            axDrawingControl1.Window.ShowRulers = 0;
            axDrawingControl1.Window.BackgroundColor = (uint)ColorTranslator.ToOle(Color.Red);
            axDrawingControl1.Window.BackgroundColorGradient = (uint)ColorTranslator.ToOle(Color.Red);
            axDrawingControl1.Window.ZoomBehavior = Visio.VisZoomBehavior.visZoomVisioExact;
                        Visio.Page currentPage = axDrawingControl1.Document.Pages[1];
            Visio.Document currentStencil = axDrawingControl1.Document.Application.Documents.OpenEx("Basic_M.vss",
                                                                        (short)Visio.VisOpenSaveArgs.visOpenDocked);
                       Visio.Shape shape1 = currentPage.Drop(currentStencil.Masters["三角形"], 1.50, 1.50);
            Visio.Shape shape2 = currentPage.Drop(currentStencil.Masters["正方形"], 2.50, 3.50);
            Visio.Shape connector = currentPage.Drop(currentStencil.Masters["动态连接线"], 4.50, 4.50);
            ConnectShapes(shape1, shape2, connector);
            Cell arrowCell = connector.get_CellsSRC((short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowLine, (short)Visio.VisCellIndices.visLineEndArrow);
            arrowCell.FormulaU = "5";
            connector.get_Cells("EndArrow").Formula = "=5";

        }
    }
}

posted @ 2007-12-10 23:41  yiling  阅读(4772)  评论(10编辑  收藏  举报