将Visio图中的图形转移到Winform中

仅限使用矩形图案

效果:

在这里插入图片描述
Visio图说明:单位mm(毫米),坐标原点位于左下角

将Visio文件拖动到主窗体后自动加载

代码:

using Microsoft.Office.Interop.Visio;
using Application = Microsoft.Office.Interop.Visio.Application;
using Color = System.Drawing.Color;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Application visioApp;
        private Document visioDocument;
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            // 检查拖放的数据是否包含文件
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy; // 允许拖放
            }
            else
            {
                e.Effect = DragDropEffects.None; // 不允许拖放
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            // 获取拖放的文件路径
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            if (files.Length > 0)
            {
                string visioFilePath = files[0];

                // 加载 Visio 文件
                List<VisioShapeInfo> list = GetShapesInfo(visioFilePath);

                load(list);
                // 在这里可以进行其他处理,例如显示 Visio 图形信息等
            }
        }

        public List<VisioShapeInfo> GetShapesInfo(string visioFilePath)
        {
            List<VisioShapeInfo> shapesInfo = new List<VisioShapeInfo>();

            // 创建 Visio 应用程序
            Application visioApp = new Application();

            // 打开 Visio 文档
            Document document = visioApp.Documents.Open(visioFilePath);

            // 获取第一个页面
            Page page = document.Pages[1];

            // 获取页面的大小
            double pageWidth = page.PageSheet.get_Cells("PageWidth").Result[VisUnitCodes.visMillimeters];
            double pageHeight = page.PageSheet.get_Cells("PageHeight").Result[VisUnitCodes.visMillimeters];

            panel1.Width = Convert.ToInt32((pageWidth * 96 / 25.4)); //从毫米换算成像素
            panel1.Height = Convert.ToInt32((pageHeight * 96 / 25.4));

            panel1.BorderStyle = BorderStyle.FixedSingle;

            // 遍历页面中的所有形状
            foreach (Shape shape in page.Shapes)
            {
                // 获取Shape的文本
                string shapeText = shape.Text;
                double shapeX = shape.Cells["PinX"].Result[VisUnitCodes.visMillimeters];
                double shapeY = shape.Cells["PinY"].Result[VisUnitCodes.visMillimeters];
                double shapeWidth = shape.Cells["Width"].Result[VisUnitCodes.visMillimeters];
                double shapeHeight = shape.Cells["Height"].Result[VisUnitCodes.visMillimeters];


                // 将形状信息添加到列表中
                shapesInfo.Add(new VisioShapeInfo
                {
                    X = (float)(shapeX * 96 / 25.4),
                    Y = (float)(pageHeight * 96 / 25.4) - (float)(shapeY * 96 / 25.4),    //窗体坐标原点为左上角,Visio图文件坐标点为左下角,所以反转一下
                    Width = (float)(shapeWidth * 96 / 25.4),
                    Height = (float)(shapeHeight * 96 / 25.4),
                    Text = shapeText
                });
            }

            // 关闭 Visio 应用程序
            visioApp.Quit();

            return shapesInfo;
        }

        private void load(List<VisioShapeInfo> shapesInfo)
        {
            // 遍历形状信息列表,为每个形状创建 Label 控件并添加到 Panel 中
            foreach (var shapeInfo in shapesInfo)
            {
                Label label = new Label();
                label.AutoSize = false;
                label.BorderStyle = BorderStyle.FixedSingle;
                label.BackColor = Color.Red;

                // 设置 Label 的文本、大小和位置
                label.Text = shapeInfo.Text;
                label.Size = new Size((int)shapeInfo.Width, (int)shapeInfo.Height);
                label.Location = new Point((int)shapeInfo.X, (int)shapeInfo.Y);

                // 文本居中显示
                label.TextAlign = ContentAlignment.MiddleCenter;

                // 添加 Label 控件到 Panel 中
                panel1.Controls.Add(label);
            }
        }
    }

    public class VisioShapeInfo
    {
        public double X { get; set; }
        public double Y { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public string Text { get; set; }
    }
}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百度CV程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值