Files
lserp_cs_6.0/插件库/Lskj.Visio/EventSink.cs
cyf ab56a9bcf7 基线 SVN r240
SVN-Revision: r240
2025-02-06 06:46:06 +00:00

186 lines
6.7 KiB
C#

/***********************
* 说明: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;
}
/// <summary>
/// 添加事件参数
/// </summary>
/// <param name="eventCode"></param>
/// <param name="description"></param>
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;
/// <summary>
/// 每次事件调用函数
/// </summary>
/// <param name="eventCode"></param>
/// <param name="source"></param>
/// <param name="eventId"></param>
/// <param name="eventSequenceNumber"></param>
/// <param name="subject"></param>
/// <param name="moreInfo"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 查看是否包含指定的事件
/// </summary>
private bool IsContainEvent(EventList eventList, string eventTargetArgs)
{
foreach (Event oneEvent in eventList)
{
if (oneEvent.TargetArgs == eventTargetArgs)
{
return true;
}
}
return false;
}
/// <summary>
/// 要添加事件就往这里加
/// </summary>
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, "", "");
}
/// <summary>
/// 该方法在形状添加后触发调用。
/// 形状对象已经存在,因此可以在后续使用进行引用处理。
/// 形状添加的实际处理过程,是在所有Visio事件处理完毕后进行。
/// </summary>
/// <param name="addedShape">待添加的形状对象</param>
/// <summary>
/// 形状文本退出修改的事件处理
/// </summary>
/// <param name="shape">形状对象</param>
private void handleShapeExitTextEdit(Shape shape)
{
if (OnTextEdit != null)
{
OnTextEdit(shape, new EventArgs());
}
}
}
}