예제 #1
0
        protected internal void SubscribeToEvents(HasControlInputCmdletBase cmdlet,
                                                  AutomationElement inputObject,
                                                  AutomationEvent eventType,
                                                  AutomationProperty prop)
        {
            AutomationEventHandler uiaEventHandler;
            AutomationPropertyChangedEventHandler uiaPropertyChangedEventHandler;
            StructureChangedEventHandler uiaStructureChangedEventHandler;
            AutomationFocusChangedEventHandler uiaFocusChangedEventHandler;

            // 20130109
            if (null == CurrentData.Events) {
                CurrentData.InitializeEventCollection();
            }

            try {

                CacheRequest cacheRequest = new CacheRequest();
                cacheRequest.AutomationElementMode = AutomationElementMode.Full; //.None;
                cacheRequest.TreeFilter = Automation.RawViewCondition;
                cacheRequest.Add(AutomationElement.NameProperty);
                cacheRequest.Add(AutomationElement.AutomationIdProperty);
                cacheRequest.Add(AutomationElement.ClassNameProperty);
                cacheRequest.Add(AutomationElement.ControlTypeProperty);
                //cacheRequest.Add(AutomationElement.ProcessIdProperty);
                // cache patterns?

                // cacheRequest.Activate();
                cacheRequest.Push();

                switch (eventType.ProgrammaticName) {
                    case "InvokePatternIdentifiers.InvokedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the InvokedEvent handler");
                        Automation.AddAutomationEventHandler(
                            InvokePattern.InvokedEvent,
                            inputObject,
                            TreeScope.Element, // TreeScope.Subtree, // TreeScope.Element,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "TextPatternIdentifiers.TextChangedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the TextChangedEvent handler");
                        Automation.AddAutomationEventHandler(
                            TextPattern.TextChangedEvent,
                            inputObject,
                            TreeScope.Element,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "TextPatternIdentifiers.TextSelectionChangedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the TextSelectionChangedEvent handler");
                        Automation.AddAutomationEventHandler(
                            TextPattern.TextSelectionChangedEvent,
                            inputObject,
                            TreeScope.Element,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "WindowPatternIdentifiers.WindowOpenedProperty":
                        this.WriteVerbose(cmdlet, "subscribing to the WindowOpenedEvent handler");
                        Automation.AddAutomationEventHandler(
                            WindowPattern.WindowOpenedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.AutomationPropertyChangedEvent":
                        if (prop != null) {
                            this.WriteVerbose(cmdlet, "subscribing to the AutomationPropertyChangedEvent handler");
                            Automation.AddAutomationPropertyChangedEventHandler(
                                inputObject,
                                TreeScope.Subtree,
                                uiaPropertyChangedEventHandler =
                                    // new AutomationPropertyChangedEventHandler(OnUIAutomationPropertyChangedEvent),
                                    //new AutomationPropertyChangedEventHandler(handler),
                                    //new AutomationPropertyChangedEventHandler(((EventCmdletBase)cmdlet).AutomationPropertyChangedEventHandler),
                                    new AutomationPropertyChangedEventHandler(cmdlet.AutomationPropertyChangedEventHandler),
                                prop);
                            UIAHelper.WriteEventToCollection(cmdlet, uiaPropertyChangedEventHandler);
                            // 20130327
                            //this.WriteObject(cmdlet, uiaPropertyChangedEventHandler);
                            if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaPropertyChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        }
                        break;
                    case "AutomationElementIdentifiers.StructureChangedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the StructureChangedEvent handler");
                        Automation.AddStructureChangedEventHandler(
                            inputObject,
                            TreeScope.Subtree,
                            uiaStructureChangedEventHandler =
                            // new StructureChangedEventHandler(OnUIStructureChangedEvent));
                            //new StructureChangedEventHandler(handler));
                            //new StructureChangedEventHandler(((EventCmdletBase)cmdlet).StructureChangedEventHandler));
                            new StructureChangedEventHandler(cmdlet.StructureChangedEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaStructureChangedEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaStructureChangedEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaStructureChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "WindowPatternIdentifiers.WindowClosedProperty":
                        this.WriteVerbose(cmdlet, "subscribing to the WindowClosedEvent handler");
                        Automation.AddAutomationEventHandler(
                            WindowPattern.WindowClosedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.MenuClosedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the MenuClosedEvent handler");
                        Automation.AddAutomationEventHandler(
                            AutomationElement.MenuClosedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.MenuOpenedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the MenuOpenedEvent handler");
                        Automation.AddAutomationEventHandler(
                            AutomationElement.MenuOpenedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.ToolTipClosedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the ToolTipClosedEvent handler");
                        Automation.AddAutomationEventHandler(
                            AutomationElement.ToolTipClosedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.ToolTipOpenedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the ToolTipOpenedEvent handler");
                        Automation.AddAutomationEventHandler(
                            AutomationElement.ToolTipOpenedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.AutomationFocusChangedEvent":
                        WriteVerbose(cmdlet, "subscribing to the AutomationFocusChangedEvent handler");
                        Automation.AddAutomationFocusChangedEventHandler(
                            //AutomationElement.AutomationFocusChangedEvent,
                            //inputObject,
                            //System.Windows.Automation.AutomationElement.RootElement,
                            //TreeScope.Subtree,
                            uiaFocusChangedEventHandler = new AutomationFocusChangedEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaFocusChangedEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaFocusChangedEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaFocusChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    default:
                        this.WriteVerbose(cmdlet,
                                     "the following event has not been subscribed to: " +
                                     eventType.ProgrammaticName);
                        break;
                }
                this.WriteVerbose(cmdlet, "on the object " + inputObject.Current.Name);
                cacheRequest.Pop();

            }
            catch (Exception e) {
            // try {
            // ErrorRecord err = new ErrorRecord(
            // e,
            // "RegisteringEvent",
            // ErrorCategory.OperationStopped,
            // inputObject);
            // err.ErrorDetails =
            // new ErrorDetails("Unable to register event handler " +
            //  // handler.ToString());
            // eventType.ProgrammaticName +
            // " for " +
            // inputObject.Current.Name);
            //  // this.OnSuccessAction.ToString());
            // WriteError(this, err, false);
            // }
            // catch {
            // ErrorRecord err = new ErrorRecord(
            // e,
            // "RegisteringEvent",
            // ErrorCategory.OperationStopped,
            // inputObject);
            // err.ErrorDetails =
            // new ErrorDetails("Unable to register event handler " +
            // eventType.ProgrammaticName);;
            // WriteError(this, err, false);
            // }

                WriteVerbose(cmdlet,
                              "Unable to register event handler " +
                              eventType.ProgrammaticName +
                              " for " +
                              inputObject.Current.Name);
                 WriteVerbose(cmdlet,
                              e.Message);
            }
        }
 protected internal void SubscribeToEvents(HasControlInputCmdletBase cmdlet,
                                           IUiElement inputObject,
                                           classic.AutomationEvent eventType,
                                           classic.AutomationProperty[] properties)
 {
     if (null == CurrentData.Events) {
         CurrentData.InitializeEventCollection();
     }
     
     try {
         var cacheRequest = new classic.CacheRequest
         // CacheRequest cacheRequest = new CacheRequest
         {
             AutomationElementMode = classic.AutomationElementMode.Full,
             // 20140130
             // TreeFilter = Automation.RawViewCondition
             TreeFilter = UiaAutomation.RawViewCondition
         };
         cacheRequest.Add(classic.AutomationElement.NameProperty);
         cacheRequest.Add(classic.AutomationElement.AutomationIdProperty);
         cacheRequest.Add(classic.AutomationElement.ClassNameProperty);
         cacheRequest.Add(classic.AutomationElement.ControlTypeProperty);
         //cacheRequest.Add(AutomationElement.ProcessIdProperty);
         // cache patterns?
         
         // cacheRequest.Activate();
         cacheRequest.Push();
         
         classic.AutomationEventHandler uiaEventHandler;
         switch (eventType.ProgrammaticName) {
             case "InvokePatternIdentifiers.InvokedEvent":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.InvokePattern.InvokedEvent,
                     inputObject,
                     classic.TreeScope.Element,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "TextPatternIdentifiers.TextChangedEvent":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.TextPattern.TextChangedEvent,
                     inputObject,
                     classic.TreeScope.Element,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "TextPatternIdentifiers.TextSelectionChangedEvent":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.TextPattern.TextSelectionChangedEvent,
                     inputObject,
                     classic.TreeScope.Element,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "WindowPatternIdentifiers.WindowOpenedProperty":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.WindowPattern.WindowOpenedEvent,
                     inputObject,
                     classic.TreeScope.Subtree,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "AutomationElementIdentifiers.AutomationPropertyChangedEvent":
                 if (properties != null) {
                     classic.AutomationPropertyChangedEventHandler uiaPropertyChangedEventHandler;
                     UiaAutomation.AddAutomationPropertyChangedEventHandler(
                         inputObject,
                         classic.TreeScope.Subtree,
                         uiaPropertyChangedEventHandler = 
                             new classic.AutomationPropertyChangedEventHandler(cmdlet.AutomationPropertyChangedEventHandler),
                         properties);
                     UiaHelper.WriteEventToCollection(cmdlet, uiaPropertyChangedEventHandler);
                     if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaPropertyChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 }
                 break;
             case "AutomationElementIdentifiers.StructureChangedEvent":
                 classic.StructureChangedEventHandler uiaStructureChangedEventHandler;
                 UiaAutomation.AddStructureChangedEventHandler(
                     inputObject,
                     classic.TreeScope.Subtree,
                     uiaStructureChangedEventHandler = 
                     new classic.StructureChangedEventHandler(cmdlet.StructureChangedEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaStructureChangedEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaStructureChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "WindowPatternIdentifiers.WindowClosedProperty":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.WindowPattern.WindowClosedEvent,
                     inputObject,
                     classic.TreeScope.Subtree,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "AutomationElementIdentifiers.MenuClosedEvent":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.AutomationElement.MenuClosedEvent,
                     inputObject,
                     classic.TreeScope.Subtree,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "AutomationElementIdentifiers.MenuOpenedEvent":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.AutomationElement.MenuOpenedEvent,
                     inputObject,
                     classic.TreeScope.Subtree,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "AutomationElementIdentifiers.ToolTipClosedEvent":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.AutomationElement.ToolTipClosedEvent,
                     inputObject,
                     classic.TreeScope.Subtree,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "AutomationElementIdentifiers.ToolTipOpenedEvent":
                 UiaAutomation.AddAutomationEventHandler(
                     classic.AutomationElement.ToolTipOpenedEvent,
                     inputObject,
                     classic.TreeScope.Subtree,
                     uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             case "AutomationElementIdentifiers.AutomationFocusChangedEvent":
                 classic.AutomationFocusChangedEventHandler uiaFocusChangedEventHandler;
                 UiaAutomation.AddAutomationFocusChangedEventHandler(
                     uiaFocusChangedEventHandler = new classic.AutomationFocusChangedEventHandler(cmdlet.AutomationEventHandler));
                 UiaHelper.WriteEventToCollection(cmdlet, uiaFocusChangedEventHandler);
                 if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaFocusChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                 break;
             default:
                 WriteVerbose(cmdlet, 
                              "the following event has not been subscribed to: " + 
                              eventType.ProgrammaticName);
                 break;
         }
         cacheRequest.Pop();
         
     } 
     catch (Exception e) {
         
         WriteVerbose(cmdlet,
                       "Unable to register event handler " +
                       eventType.ProgrammaticName +
                       " for " +
             // 20140312
                       // inputObject.Current.Name);
             inputObject.GetCurrent().Name);
          WriteVerbose(cmdlet,
                       e.Message);
     }
 }