开发者

Visio AddAdvice Throws an Exception

开发者 https://www.devze.com 2023-03-27 05:46 出处:网络
I\'m trying to handle an event when elements are added into a diagr开发者_如何转开发am, however AddAdvice() throws an unhandled COM exception:

I'm trying to handle an event when elements are added into a diagr开发者_如何转开发am, 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;

        }
    }
}


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?


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{
0

精彩评论

暂无评论...
验证码 换一张
取 消