C# - Visio中是否有任何OnShapeMoved或OnShapeDeleted事件?

时间:2011-05-26 15:45:02

标签: events visio

我认为标题或问题很清楚。我看到了关于EventSink的一些信息,但我发现很难使用它。任何提示?

2 个答案:

答案 0 :(得分:3)

Visio Primary Interop Assembly将这些事件公开为C#事件,因此您只需将事件与委托挂钩即可。

见这个简单的例子:

namespace VisioEventsExample
{
    using System;
    using Microsoft.Office.Interop.Visio;

    class Program
    {
        public static void Main(string[] args)
        {
            Application app = new Application();
            Document doc = app.Documents.Add("");
            Page page = doc.Pages[1];

            // Setup event handles for the events you are intrested in.

            // Shape deleted is easy.
            page.BeforeShapeDelete += 
                new EPage_BeforeShapeDeleteEventHandler(onBeforeShapeDelete);

            // To find out if a shape has moved hook the cell changed event 
            // and then check to see if PinX or PinY changed.
            page.CellChanged += 
                new EPage_CellChangedEventHandler(onCellChanged);

            // In C# 4 for you can simply do this:
            //            
            //   page.BeforeShapeDelete += onBeforeShapeDelete;
            //   page.CellChanged += onCellChanged;

            // Now wait for the events.
            Console.WriteLine("Wait for events. Press any key to stop.");
            Console.ReadKey();
        }

        // This will be called when a shape sheet cell for a
        // shape on the page is changed. To know if the shape
        // was moved see of the pin was changed. This will 
        // fire twice if the shape is moved horizontally and 
        // vertically.
        private static void onCellChanged(Cell cell)
        {
            if (cell.Name == "PinX" || cell.Name == "PinY")
            {
                Console.WriteLine(
                    string.Format("Shape {0} moved", cell.Shape.Name));
            }            
        }

        // This will be called when a shape is deleted from the page.
        private static void onBeforeShapeDelete(Shape shape)
        {
            Console.WriteLine(string.Format("Shape deleted {0}", shape.Name));
        }
    }
}

如果您尚未下载Visio SDK,则应该这样做。 SDK的最新版本包含许多有用的示例,包括一个名为“Shape Add \ Delete Event”的示例。如果您有2010版本,可以通过转到“开始”菜单\“程序”\“Microsoft Office 2010开发人员资源\ Microsoft Visio 2010 SDK \ Microsoft Visio代码示例库”来浏览示例。

答案 1 :(得分:0)

我认为您必须实施EvenSink才能访问“ShapesDeleted”,即

 (short)Microsoft.Office.Interop.Visio.VisEventCodes.visEvtCodeShapeDelete

如果您正在寻找事件“BeforeShapeDelete”而不是“之后”ShapeDelete,上面的代码将帮助您;)