/*********************** * 说明:visio 事件处理类 * 创建人:姜棚 * 创建日期: * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ***********************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Visio; using System.Runtime.InteropServices; namespace Lskj.Visio { [ComVisible(true)] public sealed class EventSink : IVisEventProc { private int alertResponse; public Document eventDocument; //Visio文档对象 private Application eventApplication; //Visio应用程序对象 public delegate void VisioEventHandler(object sender, EventArgs e); public event VisioEventHandler OnTextEdit; //文本编辑事件 private const short visEvtAdd = -32768; public EventSink() { } [CLSCompliant(false)] public void AddAdvise(Application callingApplication, Document callingDocument) { // 保存应用程序对象,方便设置事件处理 eventApplication = callingApplication; // 缓存AlertResponse 对象的值 alertResponse = callingApplication.AlertResponse; // 保存文档对象,方便设置事件处理 eventDocument = callingDocument; setAddAdvise(); return; } /// /// 添加事件参数 /// /// /// private void addEventDescription( short eventCode, string description) { string key = Convert.ToString(eventCode, System.Globalization.CultureInfo.InvariantCulture); eventDescriptions.Add(key, description); } private System.Collections.Specialized.StringDictionary eventDescriptions; /// /// 每次事件调用函数 /// /// /// /// /// /// /// /// public object VisEventProc(short eventCode, object source, int eventId, int eventSequenceNumber, object subject, object moreInfo) { Application eventProcApplication = null; Document eventProcDocument = null; Shape eventShape = null; Selection subjectSelection = null; object result = null; // 获得传递过来的应用程序对象或者文档对象 if (source is IVApplication) { eventProcApplication = (Microsoft.Office.Interop.Visio.Application)source; } else if (source is IVDocument) { eventProcDocument = (Document)source; } // 检查每个事件代码是否被处理了。事件代码是对象和事件动作的联合对象。 // 仅当事件在SetAddAdvise方法里面增加后,事件会被发送到这里进行处理。 switch (eventCode) { case (short)Microsoft.Office.Interop.Visio.VisEventCodes. visEvtDoc + (short)Microsoft.Office.Interop.Visio. VisEventCodes.visEvtDel: break; case (short)Microsoft.Office.Interop.Visio.VisEventCodes. visEvtShape + (short)Microsoft.Office.Interop.Visio. VisEventCodes.visEvtDel: break; // 形状Text 改变事件 case (short)Microsoft.Office.Interop.Visio.VisEventCodes.visEvtCodeShapeBeforeTextEdit: eventShape = (Shape)subject; handleShapeExitTextEdit(eventShape); break; case (short)Microsoft.Office.Interop.Visio.VisEventCodes.visEvtCodeShapeExitTextEdit: eventShape = (Shape)subject; handleShapeExitTextEdit(eventShape); break; default: break; } return result; } /// /// 查看是否包含指定的事件 /// private bool IsContainEvent(EventList eventList, string eventTargetArgs) { foreach (Event oneEvent in eventList) { if (oneEvent.TargetArgs == eventTargetArgs) { return true; } } return false; } /// /// 要添加事件就往这里加 /// private void setAddAdvise() { const string sink = ""; Event newEvent = null; EventList applicationEvents = eventApplication.EventList; EventList documentEvents = eventDocument.EventList; // Add the BeforeShapeDelete event. //AddAdvise(生成通知的事件,对对象上接收事件通知的 COM 接口的引用,可传参数,可传参数); documentEvents.AddAdvise(((short)Microsoft.Office.Interop.Visio. VisEventCodes.visEvtDel + (short)Microsoft.Office.Interop. Visio.VisEventCodes.visEvtShape), this, "", ""); // Add the PageAdded event. documentEvents.AddAdvise((short)Microsoft.Office.Interop.Visio. VisEventCodes.visEvtPage + visEvtAdd, this, "", ""); // Add the ShapeAdded event. documentEvents.AddAdvise((short)Microsoft.Office.Interop.Visio. VisEventCodes.visEvtShape + visEvtAdd, this, "", ""); applicationEvents.AddAdvise((short)Microsoft.Office.Interop.Visio.VisEventCodes.visEvtCodeShapeExitTextEdit, this, "", ""); } /// /// 该方法在形状添加后触发调用。 /// 形状对象已经存在,因此可以在后续使用进行引用处理。 /// 形状添加的实际处理过程,是在所有Visio事件处理完毕后进行。 /// /// 待添加的形状对象 /// /// 形状文本退出修改的事件处理 /// /// 形状对象 private void handleShapeExitTextEdit(Shape shape) { if (OnTextEdit != null) { OnTextEdit(shape, new EventArgs()); } } } }