C#进行Visio开发的事件处理

Visio的二次开发过程中,必定要处理很多Visio事件,事件基本上分为三类:Document事件、Applicaiton事件、AxDrawingControl事件。Document事件是每个文档创建或者打开都需要重新绑定一次的,但是Application事件不变,即不需要重新绑定,AxDrawingControl是ActiveX控件的事件响应。
在C#进行Visio的开发中,通过继承接口IVisEventProc,可以创建事件的侦听处理类,如下所示:
    [ComVisible(true)]
    
public sealed class EventSink2 : IVisEventProc
    
{

..

        
object IVisEventProc.VisEventProc(short eventCode, object source, int eventId,
            
int eventSequenceNumber, object subject, object moreInfo)
        
{
           
        }

     }

事件有很多类,有Application级别的,有Document级别的,有Page级别的,有Shape级别的,有Windows级别的,有Cell级别的..............,下面截取一部分事件代码看看

                    // Document event codes
                    case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtDoc 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtDel:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtCodeBefDocSave:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtCodeBefDocSaveAs:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtCodeDocDesign:
                    // Page event codes
                    case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtPage 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtDel:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtPage 
+ visEvtAdd:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtPage 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtMod:
                    // Master event codes
                    case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtMaster 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtDel:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtMaster 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtMod:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtCodeCancelMasterDel:
                   // Shape event codes
                    case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtShape 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtDel:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtCodeShapeBeforeTextEdit:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtShape 
+ visEvtAdd:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtShape 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtMod:
                    // Cell event codes
                    case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtCell 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtMod:
                    
case (short)Microsoft.Office.Interop.Visio.VisEventCodes.
                        visEvtFormula 
+ (short)Microsoft.Office.Interop.Visio.
                        VisEventCodes.visEvtMod:
还有很多很多..

将事件和我们的处理函数联系起来,还需要对事件进行侦听,即AddAdvise,下面列出一段如何侦听事件的代码供参考
            const string sink = "";
            Event newEvent 
= null;

            EventList applicationEvents 
= eventApplication.EventList;
            EventList documentEvents 
= eventDocument.EventList;

            newEvent 
= documentEvents.AddAdvise(
                (
unchecked((short)VisEventCodes.visEvtAdd) + (short)VisEventCodes.visEvtShape),
                (IVisEventProc)
this, sink, "ShapeAdd");

            newEvent 
= documentEvents.AddAdvise(
                (
short)VisEventCodes.visEvtDel + (short)VisEventCodes.visEvtShape,
                (IVisEventProc)
this, sink, "ShapeDelete");

              newEvent 
= documentEvents.AddAdvise(
                (
short)VisEventCodes.visEvtMod + (short)VisEventCodes.visEvtCell,
                (IVisEventProc)
this, sink, "CellChanged");

             newEvent 
= documentEvents.AddAdvise(
                (
short)VisEventCodes.visEvtCodeShapeExitTextEdit,
                (IVisEventProc)
this, sink, "ShapeExitedTextEdit");

             newEvent 
= documentEvents.AddAdvise(
                (
short)VisEventCodes.visEvtCodeQueryCancelSelDel,
                (IVisEventProc)
this, sink, "QueryCancelSelectionDelete");

具体的使用,最好下载Visio2007 SDK进行学习,自己编写一些例子对事件进行处理

posted on 2008-02-22 23:25 伍华聪 阅读(...) 评论(...) 编辑 收藏

导航

公告