/// <summary>
        /// Start or Stop to log the events' message
        /// </summary>
        /// <param name="mode">true represents for logging events started,false is to stop log events</param>
        private void StartStop(bool mode)
        {
            isWorking = mode;
            if (isWorking)
            {
                customTaskPanel.Clear();
                commandBarButtonStart.Caption = StopJob;

                // Get previous Selected Object and Type Name
                selectedTypeNameLastTime = GetSelectedTypeName();

                // when User restarts to log events,
                // Must Initialize previous Selected shape and previous Existing shapes again.
                FillCollections();
                shapesExistingLastTime = shapesExistingNow;
                shapeSelectedLastTime  = shapeSelectedNow;

                // Initialize previous Selected Object and Type Name again
                selectedTypeNameLastTime = selecteTypeNameNow;
            }
            else
            {
                commandBarButtonStart.Caption = StartJob;

                // Dispose object
                shapeSelectedLastTime    = null;
                shapeSelectedNow         = null;
                shapesExistingLastTime   = null;
                shapesExistingNow        = null;
                selectedTypeNameLastTime = null;
                selecteTypeNameNow       = null;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Get Created Shape Collection or Removed Shape Collection
        /// </summary>
        /// <param name="shapesToCheck">Shapes Collections to be Checked</param>
        /// <returns>Return ShapesCreated Collection or ShapesRemoved</returns>
        public MyShapes GetShapesMissingIn(MyShapes shapesToCheck)
        {
            MyShapes newShapes = new MyShapes();

            foreach (MyShape shape in myShapes)
            {
                if (shapesToCheck == null || !shapesToCheck.Contains(shape.ID))
                {
                    newShapes.Add(shape);
                }
            }
            return(newShapes);
        }
        /// <summary>
        /// Analyze Existing Shapes
        /// </summary>
        private void ProcessExistingShapes()
        {
            MyShapes shapesCreated = shapesExistingNow.GetShapesMissingIn(shapesExistingLastTime);
            MyShapes shapesRemoved = shapesExistingLastTime.GetShapesMissingIn(shapesExistingNow);

            // ShapesRemoved Event Occurs If the count of the Removed Shapes is not zero.
            // Also, the number of Removed Shapes will be shown in Task Panel.
            if (shapesRemoved.Count != 0)
            {
                ShapesRemoved(shapesRemoved);
            }
            if (shapesCreated.Count != 0)
            {
                ShapesCreated(shapesCreated);
            }
        }
        // Initialize Control
        private void InitializeCustomComponents()
        {
            // Add a custom commandbar button and set the name is "Start checking shapes"
            commandBarStart = this.Application.CommandBars.ActiveMenuBar;

            // Add the commandbar button into the commandbar
            commandBarButtonStart         = (Office.CommandBarButton)commandBarStart.Controls.Add(Office.MsoControlType.msoControlButton, Before: 1, Temporary: true);
            commandBarButtonStart.Style   = Office.MsoButtonStyle.msoButtonCaption;
            commandBarButtonStart.Caption = StartJob;
            commandBarButtonStart.FaceId  = 60;
            commandBarButtonStart.Visible = true;

            // Register click event of commandbarbutton
            commandBarButtonStart.Click += new Office._CommandBarButtonEvents_ClickEventHandler(commandBarButtonStart_Click);

            // Use CommandBars.OnUpdate event to detect shapes' event
            commandBars           = this.Application.CommandBars;
            commandBars.OnUpdate += new Office._CommandBarsEvents_OnUpdateEventHandler(commandBars_OnUpdate);

            // Initialize Task Panel
            // set the Position and width of task panel
            customTaskPanel = new CustomTaskPanel();
            Microsoft.Office.Tools.CustomTaskPane mycustomerTaskPane = this.CustomTaskPanes.Add(customTaskPanel, "Custom Task Panel Tracking Event");
            mycustomerTaskPane.Visible      = true;
            mycustomerTaskPane.Width        = 430;
            mycustomerTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;

            // Initialize Shapes in active worksheet
            Excel.Worksheet shapeworksheet = this.Application.ActiveSheet;
            Excel.Shape     shapeRect;
            Excel.Shape     shapeCircle;
            shapeRect   = shapeworksheet.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeRectangle, 60, 80, 80, 30);
            shapeCircle = shapeworksheet.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeOval, 200, 30, 50, 50);

            rectangleShape = new MyShape(shapeRect);
            circleShape    = new MyShape(shapeCircle);

            // Initialize Shapes and Selected shape before the shapes change
            shapeSelectedLastTime  = GetShapeSelected();
            shapesExistingLastTime = new MyShapes(shapeworksheet);
        }
        /// <summary>
        /// Get Selected shape and Existing shapes
        /// </summary>
        private void FillCollections()
        {
            if (selecteTypeNameNow == null || selectedTypeNameLastTime == null)
            {
                customTaskPanel.AddMessage("The type name of the selected object is " + selectedTypeNameLastTime);
            }
            else
            {
                if (selecteTypeNameNow != selectedTypeNameLastTime)
                {
                    customTaskPanel.AddMessage("The type name of the selected object is " + selecteTypeNameNow);
                }
            }

            // Get the current selected shape and current existing shapes
            shapeSelectedNow  = GetShapeSelected();
            shapesExistingNow = new MyShapes(Application.ActiveSheet);

            // Get the current Selected Object and current Type Name
            selecteTypeNameNow = GetSelectedTypeName();
        }
        /// <summary>
        /// CommandBars Update function
        /// </summary>
        private void commandBars_OnUpdate()
        {
            if (!isWorking)
            {
                return;
            }

            selecteTypeNameNow = GetSelectedTypeName();

            // Initialize current Selected shape and  current Existing shapes
            FillCollections();

            // detect whether the custom events occur
            ProcessExistingShapes();
            ProcessSelectedShape();

            // Initialize previous Selected shape and previous Existing shapes again
            shapesExistingLastTime = shapesExistingNow;
            shapeSelectedLastTime  = shapeSelectedNow;

            // Initialize previous Selected Object and Type Name again
            selectedTypeNameLastTime = selecteTypeNameNow;
        }
 /// <summary>
 /// Occurs when a shape is Created
 /// </summary>
 /// <param name="myshape">The Created shape</param>
 private void ShapesCreated(MyShapes shapesCreated)
 {
     customTaskPanel.AddMessage("ShapeCreated Event Occurs." + shapesCreated.Count.ToString() + " shape(s) are added to the Sheet.Shapes collection.");
 }
 /// <summary>
 /// Occurs when a shape(s) is removed from the Shapes collection of the current sheet
 /// </summary>
 /// <param name="shapesRemoved">The removed shapes</param>
 private void ShapesRemoved(MyShapes shapesRemoved)
 {
     customTaskPanel.AddMessage("ShapesRemoved Event Occurs." + shapesRemoved.Count.ToString() + " shape(s) are removed from Sheet.Shapes.");
 }