我正在尝试处理元素添加到图表中的事件,但AddAdvice()会抛出未处理的COM异常:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Visio = Microsoft.Office.Interop.Visio;
namespace VisioAddAdviceWinForms
{
public partial class Form1 : Form
{
private EventSink eventSink = null;
public Form1()
{
InitializeComponent();
this.eventSink = new EventSink();
unchecked
{
axDrawingControl1.Window.EventList.AddAdvise(((short)Visio.VisEventCodes.visEvtAdd + (short)Visio.VisEventCodes.visEvtShape), this.eventSink, "", "");
}
}
}
public class EventSink : Visio.IVisEventProc
{
object Visio.IVisEventProc.VisEventProc(
short eventCode,
object source,
int eventID,
int eventSeqNum,
object subject,
object moreInfo)
{
Visio.IVApplication app = null;
Visio.IVDocument doc = null;
Visio.IVShape shape;
try
{
if (source is Visio.IVApplication)
{
app = (Visio.Application)source;
}
else if (source is Visio.IVDocument)
{
doc = (Visio.Document)source;
}
switch (eventCode)
{
case unchecked((short)Visio.VisEventCodes.visEvtAdd) +
(short)Visio.VisEventCodes.visEvtShape:
shape = (Visio.Shape)subject;
MessageBox.Show("added");
break;
case (short)Visio.VisEventCodes.visEvtApp +
(short)Visio.VisEventCodes.visEvtNonePending:
MessageBox.Show("pending");
break;
case (short)Visio.VisEventCodes.visEvtDel + (short)
Visio.VisEventCodes.visEvtShape:
shape = (Visio.Shape)subject;
MessageBox.Show("deleted");
break;
default:
break;
}
}
catch (Exception err)
{
MessageBox.Show("Exception in IVisEventProc.VisEventProc: "
+ err.Message);
}
return null;
}
}
}
答案 0 :(得分:0)
不确定您在使用AddAdvise时遇到的异常,但为什么不绕过AddAdvise并使用Visio的互操作层提供的托管事件包装器?是否有理由使用AddAdvise而不是Visio Primary Interop Assembly?
您可以直接在控件本身上添加ShapeAdded事件的处理程序吗?或者,如果不在控件上,则肯定在控件内包含的Visio.Document上。
另请参阅此论坛帖子中的示例代码:
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c80df85f-4e97-4f4c-8563-52cb40786b13/
这个其他stackoverflow问题的答案:
C# - Is there any OnShapeMoved or OnShapeDeleted event in Visio?
答案 1 :(得分:0)
添加:
使用System.Runtime.InteropServices;
你有这个:
public class EventSink : Visio.IVisEventProc{
把这个:
[ComVisible(true)]
public class EventSink : Microsoft.Office.Interop.Visio.IVisEventProc{