Visio AddAdvice 引发异常
我试图在将元素添加到图表中时处理事件,但是 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;
}
}
}
I'm trying to handle an event when elements are added into a diagram, however AddAdvice() throws an unhandled COM exception:
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定您在 AddAdvise 中看到的异常,但为什么不绕过 AddAdvise 并使用 Visio 互操作层提供的托管事件包装器呢?是否有理由使用 AddAdvise 而不是 Visio 主互操作程序集?
您可以直接在控件本身上直接添加 ShapeAdded 事件的处理程序吗?或者,如果不在控件上,则肯定在控件内包含的 Visio.Document 上。
另请参阅此论坛帖子中的示例代码:
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c80df85f-4e97-4f4c-8563-52cb40786b13/
另一个 stackoverflow 问题的答案:
C# - Visio 中是否有 OnShapeMoved 或 OnShapeDeleted 事件?
Not sure about the exception you're seeing with AddAdvise, but why not bypass AddAdvise and use the managed event wrappers provided by Visio's interop layer? Is there a reason to use AddAdvise instead of the Visio Primary Interop Assembly?
Can you simply add a handler for the ShapeAdded event directly on the control itself? Or, if not on the control, then certainly on the Visio.Document contained inside the control.
See also the sample code in this forum post:
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/c80df85f-4e97-4f4c-8563-52cb40786b13/
And the answer to this other stackoverflow question:
C# - Is there any OnShapeMoved or OnShapeDeleted event in Visio?
添加以下内容:
使用 System.Runtime.InteropServices;
在你有这个的地方:
把这个:
Add this:
using System.Runtime.InteropServices;
And where you have this:
Put this: