Visio AddAdvice 引发异常

发布于 2024-11-29 00:33:53 字数 2634 浏览 2 评论 0原文

我试图在将元素添加到图表中时处理事件,但是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不如归去 2024-12-06 00:33:53

不确定您在 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?

再可℃爱ぅ一点好了 2024-12-06 00:33:53

添加以下内容:

使用 System.Runtime.InteropServices;

在你有这个的地方:

public class EventSink : Visio.IVisEventProc{  

把这个:

[ComVisible(true)]
public class EventSink : Microsoft.Office.Interop.Visio.IVisEventProc{

Add this:

using System.Runtime.InteropServices;

And where you have this:

public class EventSink : Visio.IVisEventProc{  

Put this:

[ComVisible(true)]
public class EventSink : Microsoft.Office.Interop.Visio.IVisEventProc{
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文