现在的位置: 首页 > 综合 > 正文

c#控制Visio画图

2013年08月15日 ⁄ 综合 ⁄ 共 5000字 ⁄ 字号 评论关闭
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using AxVisOcx;
using Visio;
using VisOcx;

namespace WindowsApplication1
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button btn_Draw;
        private Visio.Application VsApplication = null;
        private AxVisOcx.AxDrawingControl axDrawingControl1;

        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.btn_Draw = new System.Windows.Forms.Button();
            this.axDrawingControl1 = new AxVisOcx.AxDrawingControl();
            ((System.ComponentModel.ISupportInitialize)(this.axDrawingControl1)).BeginInit();
            this.SuspendLayout();
            //
            // btn_Draw
            //
            this.btn_Draw.Location = new System.Drawing.Point(488, 368);
            this.btn_Draw.Name = "btn_Draw";
            this.btn_Draw.Size = new System.Drawing.Size(112, 23);
            this.btn_Draw.TabIndex = 1;
            this.btn_Draw.Text = "DRAW...";
            this.btn_Draw.Click += new System.EventHandler(this.btn_Draw_Click);
            //
            // axDrawingControl1
            //
            this.axDrawingControl1.Enabled = true;
            this.axDrawingControl1.Location = new System.Drawing.Point(8, 8);
            this.axDrawingControl1.Name = "axDrawingControl1";
            this.axDrawingControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axDrawingControl1.OcxState")));
            this.axDrawingControl1.Size = new System.Drawing.Size(600, 352);
            this.axDrawingControl1.TabIndex = 2;
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(616, 405);
            this.Controls.Add(this.axDrawingControl1);
            this.Controls.Add(this.btn_Draw);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.axDrawingControl1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            System.Windows.Forms.Application.Run(new Form1());
        }

        private void btn_Draw_Click(object sender, System.EventArgs e)
        {
            string[,] fn = {{"PC","Tip_PC1"},{"PC","Tip_PC2"},{"PC","Tip_PC3"},{"PC","Tip_PC4"},{"PC","Tip_PC5"}};
            CreateDrawing (fn);
        }

        private void CreateDrawing(string[,] a)
        {
            VsApplication = axDrawingControl1.Window.Application;

            Visio.Shape shpObjHUB = null;
            Visio.Shape shpObjNodes = null;
            Visio.Document stnObj = null;
            Visio.Master mstObj = null;
            double dblX = 0;
            double dblY = 0;
            double dblDegreeInc = 0;
            double dblRad = 0;
            double dblPageWidth = 0;
            double dblPageHeight = 0;
            const double PI = 3.1415;
            const int CircleRadius = 2;

                        //MessageBox.Show(fn.GetUpperBound(0).ToString());
            dblDegreeInc = 360 / a.GetUpperBound(0);

            dblPageWidth = VsApplication.ActivePage.PageSheet.get_CellsSRC(
                (short)Visio.VisSectionIndices.visSectionObject,
                (short)Visio.VisRowIndices.visRowPage,
                (short)Visio.VisCellIndices.visPageWidth).ResultIU;

            dblPageHeight = VsApplication.ActivePage.PageSheet.get_CellsSRC(
                (short)Visio.VisSectionIndices.visSectionObject,
                (short)Visio.VisRowIndices.visRowPage,
                (short)Visio.VisCellIndices.visPageHeight).ResultIU;

            stnObj = VsApplication.Documents.OpenEx("D://来时拷贝//NetworkElement.vss", (short)Visio.VisOpenSaveArgs.visOpenDocked);

            mstObj = stnObj.Masters[a[0, 0]];
            shpObjHUB = VsApplication.ActivePage.Drop(mstObj, dblPageWidth / 2, dblPageHeight / 2);

            shpObjHUB.Text = a[0, 1];

            for(int i=1;i<=a.GetUpperBound(0);i++)
            {
                mstObj = stnObj.Masters[a[i, 0]];

                dblRad = (dblDegreeInc * i) * PI / 180;
                dblX = CircleRadius * Math.Cos(dblRad) + (dblPageWidth / 2);
                dblY = CircleRadius * Math.Sin(dblRad) + (dblPageHeight / 2);
                //Add shape to drawing in proper location
                shpObjNodes = VsApplication.ActivePage.Drop(mstObj, dblX, dblY);
                //Set shape text
                shpObjNodes.Text = a[i, 1];
            }
        }
    }
}
 

抱歉!评论已关闭.