Description of HasControlInputCmdletBase.
Inheritance: HasScriptBlockCmdletBase
Ejemplo n.º 1
0
 protected internal void RunOnSleepScriptBlocks(HasControlInputCmdletBase cmdlet, object[] parameters)
 {
     if (cmdlet is HasTimeoutCmdletBase) {
         runTwoScriptBlockCollections(
             Preferences.OnSleepAction,
             ((HasTimeoutCmdletBase)cmdlet).OnSleepAction,
             cmdlet,
             parameters);
     }
 }
Ejemplo n.º 2
0
 internal static IUiElement InvokeContextMenu(this IUiElement inputObject, HasControlInputCmdletBase cmdlet, int x, int y)
 {
     IUiElement resultElement = null;
     try {
         
         if (!cmdlet.ClickControl(
                 cmdlet,
                 inputObject,
                 new ClickSettings() {
                     RightClick = true,
                     RelativeX = (x < 0 ? Preferences.ClickOnControlByCoordX : x),
                     RelativeY = (y < 0 ? Preferences.ClickOnControlByCoordY : y)
                 })) {
         }
     }
     catch (Exception) {
         throw new Exception("failed to click on the control");
     }
     
     // 20140116
     // what are these x and y?
     // int x = Cursor.Position.X;
     // int y = Cursor.Position.Y;
     
     var contextMenuSearcher =
         AutomationFactory.GetSearcherImpl<ContextMenuSearcher>();
     
     var contextMenuSearcherData =
         new ContextMenuSearcherData {
         InputObject = inputObject,
         ProcessId = inputObject.GetCurrent().ProcessId
     };
     
     var elementsMenuRoot =
         contextMenuSearcher.GetElements(
             contextMenuSearcherData,
             Preferences.Timeout);
     
     resultElement =
         elementsMenuRoot.First(element => null != element);
     
     return resultElement;
 }
Ejemplo n.º 3
0
 protected internal void SleepAndRunScriptBlocks(HasControlInputCmdletBase cmdlet)
 {
     RunOnSleepScriptBlocks(cmdlet, null);
     Thread.Sleep(Preferences.OnSleepDelay);
 }
Ejemplo n.º 4
0
        protected internal void RunEventScriptBlocks(HasControlInputCmdletBase cmdlet)
        {
            var blocks = new List<ScriptBlock>();
            
//            WriteVerbose(cmdlet,
//                              blocks.Count +
//                              " events to fire");
            if (cmdlet.EventAction != null && cmdlet.EventAction.Length > 0) {
                foreach (ScriptBlock sb in cmdlet.EventAction) {
                    blocks.Add(sb);
//                    WriteVerbose(cmdlet,
//                                      "the scriptblock: " +
//                                      sb +
//                                      " is ready to be fired");
                }
            }
            
            try {
                runScriptBlocks(blocks, cmdlet, true, null);
            }
            catch (Exception eScriptBlocks) {
                
                cmdlet.WriteError(
                    cmdlet,
                    eScriptBlocks.Message,
                    "ScriptblocksFailed",
                    ErrorCategory.InvalidResult,
                    true);
            }
            // runEventScriptBlocks(blocks, cmdlet); //, true);
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
0
        protected bool ClickControl(HasControlInputCmdletBase cmdlet,
                                    AutomationElement element,
                                    bool RightClick,
                                    bool MidClick,
                                    bool Alt,
                                    bool Shift,
                                    bool Ctrl,
                                    bool inSequence,
                                    bool DoubleClick,
                                    int RelativeX,
                                    int RelativeY)
        {
            bool result = false;

            // 20121012
            if (-1000000 == RelativeX) {
                RelativeX = Preferences.ClickOnControlByCoordX;
            }
            if (-1000000 == RelativeY) {
                RelativeY = Preferences.ClickOnControlByCoordY;
            }

            // 20120823
            //foreach (AutomationElement element in elements){

            AutomationElement whereToClick =
                element;
            WriteVerbose(cmdlet,
                         "where the click will be performed: " +
                         element.Current.Name);
            AutomationElement whereTheHandle =
                whereToClick;

            if (whereToClick.Current.NativeWindowHandle == 0) {

                WriteVerbose(cmdlet, "The handle of this control equals to zero");
                WriteVerbose(cmdlet, "trying to use one of its ancestors");

                whereTheHandle =
                    UIAHelper.GetAncestorWithHandle(whereToClick);
                if (whereTheHandle.Current.NativeWindowHandle == 0) {
                    ErrorRecord err =
                        new ErrorRecord(new Exception("The handle of this control equals to zero"),
                                        "ZeroHandle",
                                        ErrorCategory.InvalidArgument,
                                        whereTheHandle);
                    err.ErrorDetails =
                        new ErrorDetails("This control does not have a handle");
                    WriteError(cmdlet, err, true);
                    // return;
                    // 20120209 result = false;
                    // 20120209 return result;
                } else {
                    WriteVerbose(cmdlet,
                                 "the control with a handle is " +
                                 whereTheHandle.Current.Name);
                    WriteVerbose(cmdlet,
                                 "its handle is " +
                                 whereTheHandle.Current.NativeWindowHandle.ToString());
                    WriteVerbose(cmdlet,
                                 "its rectangle is " +
                                 whereTheHandle.Current.BoundingRectangle.ToString());
                }
            }

            WriteVerbose(cmdlet,
                         "the element the click will be performed on has rectangle: " +
                         whereToClick.Current.BoundingRectangle.ToString());

            int x = 0;
            int y = 0;
            // these x and y are window-related coordinates
            if (RelativeX != 0 && RelativeY != 0) {
                //int relativeX = x - (int)whereTheHandle.Current.BoundingRectangle.X;
                x = RelativeX + (int)whereToClick.Current.BoundingRectangle.X;
                //int relativeY = y - (int)whereTheHandle.Current.BoundingRectangle.Y;
                y = RelativeY + (int)whereToClick.Current.BoundingRectangle.Y;
            } else {
                // these x and y are for the SetCursorPos call
                // they are screen coordinates
                x = (int)whereToClick.Current.BoundingRectangle.X +
                    ((int)whereToClick.Current.BoundingRectangle.Width / 2); // + 3;
                y = (int)whereToClick.Current.BoundingRectangle.Y +
                    ((int)whereToClick.Current.BoundingRectangle.Height / 2); // + 3;
            }
            // if the -X and -Y paramters are supplied (only for SetCursorPos)
            // if (RelativeX != 0 && RelativeY != 0) {
            // x = RelativeX;
            // y = RelativeY;
            // WriteVerbose(cmdlet, "coordinates are taken from the input parameters");
            // }
            // WriteVerbose(cmdlet, "X = " + x.ToString());
            // WriteVerbose(cmdlet, "Y = " + y.ToString());

            // PostMessage's (click) second parameter
            uint uDown = 0;
            uint uUp = 0;

            // these relative coordinates for SendMessage/PostMessage
            int relativeX = x - (int)whereTheHandle.Current.BoundingRectangle.X;
            int relativeY = y - (int)whereTheHandle.Current.BoundingRectangle.Y;

            //  // these x and y are window-related coordinates
            // if (RelativeX != 0 && RelativeY != 0) {
            //  //int relativeX = x - (int)whereTheHandle.Current.BoundingRectangle.X;
            // x = RelativeX + (int)whereToClick.Current.BoundingRectangle.X;
            //  //int relativeY = y - (int)whereTheHandle.Current.BoundingRectangle.Y;
            // y = RelativeY + (int)whereToClick.Current.BoundingRectangle.Y;
            // }
            WriteVerbose(cmdlet, "relative X (the base is the control with the handle) = " + relativeX.ToString());
            WriteVerbose(cmdlet, "relative Y (the base is the control with the handle) = " + relativeY.ToString());

            // PostMessage's (click) third and fourth paramters (the third'll be reasigned later)
            System.IntPtr wParamDown = IntPtr.Zero;
            System.IntPtr wParamUp = IntPtr.Zero;
            System.IntPtr lParam =
                new IntPtr(((new IntPtr(relativeX)).ToInt32() & 0xFFFF) +
                           (((new IntPtr(relativeY)).ToInt32() & 0xFFFF) << 16));

            // PostMessage's (keydown/keyup) fourth parameter
            uint uCtrlDown = 0x401D;
            uint uCtrlUp = 0xC01D;
            uint uShiftDown = 0x402A;
            uint uShiftUp = 0xC02A;
            System.IntPtr lParamKeyDown = IntPtr.Zero;
            System.IntPtr lParamKeyUp = IntPtr.Zero;

            // if (Ctrl) {
            // unsafe{
            // KEYBDINPUT kb;//={ 0 };
            // INPUT Input = new INPUT(); //={ 0 };
            //  // if (bExtended)
            //  // kb.dwFlags = KEYEVENT_EXTENDEDKEY;
            // kb.wVk = (ushort)VK_CONTROL;
            // Input.Type = INPUT_KEYBOARD;
            //
            //  // Input.Data = kb;
            // SendInput(1, Input, sizeof(INPUT));
            // }
            // }

            if (Ctrl) {
                lParamKeyDown =
                    new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
                               (((new IntPtr(uCtrlDown)).ToInt32() & 0xFFFF) << 16));
                lParamKeyUp =
                    new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
                               (((new IntPtr(uCtrlUp)).ToInt32() & 0xFFFF) << 16));
                WriteVerbose(this, "control parameters for KeyDown/KeyUp have been prepared");
            }
            if (Shift) {
                lParamKeyDown =
                    new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
                               (((new IntPtr(uShiftDown)).ToInt32() & 0xFFFF) << 16));
                lParamKeyUp =
                    new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
                               (((new IntPtr(uShiftUp)).ToInt32() & 0xFFFF) << 16));
                WriteVerbose(this, "shift parameters for KeyDown/KeyUp have been prepared");
            }
            // PostMessage's (activate) third parameter
            uint ulAct = 0;
            uint uhAct = 0;

            uint mask = 0;
            if (Ctrl) {
                mask |= NativeMethods.MK_CONTROL;
                WriteVerbose(this, "control parameters for ButtonDown/ButtonUp have been prepared");
            }
            if (Shift) {
                mask |= NativeMethods.MK_SHIFT;
                WriteVerbose(this, "shift parameters for ButtonDown/ButtonUp have been prepared");
            }

            if (RightClick && !DoubleClick) {
                WriteVerbose(cmdlet, "right click");
                uhAct = uDown = NativeMethods.WM_RBUTTONDOWN;
                uUp = NativeMethods.WM_RBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_RBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_RBUTTON;
            } else if (RightClick && DoubleClick) {
                WriteVerbose(cmdlet, "right double click");
                uhAct = uDown = NativeMethods.WM_RBUTTONDBLCLK;
                uUp = NativeMethods.WM_RBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_RBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_RBUTTON;
            } else if (MidClick && !DoubleClick) {
                WriteVerbose(cmdlet, "middle button click");
                uhAct = uDown = NativeMethods.WM_MBUTTONDOWN;
                uUp = NativeMethods.WM_MBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_MBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_MBUTTON;
            } else if (MidClick && DoubleClick) {
                WriteVerbose(cmdlet, "middle button double click");
                uhAct = uDown = NativeMethods.WM_MBUTTONDBLCLK;
                uUp = NativeMethods.WM_MBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_MBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_MBUTTON;
            } else if (DoubleClick) {
                WriteVerbose(cmdlet, "left double click");
                uhAct = uDown = NativeMethods.WM_LBUTTONDBLCLK;
                uUp = NativeMethods.WM_LBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_LBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_LBUTTON;
            } else {
                WriteVerbose(cmdlet, "left click");
                uhAct = uDown = NativeMethods.WM_LBUTTONDOWN;
                uUp = NativeMethods.WM_LBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_LBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_LBUTTON;
            }

            System.IntPtr handle =
                    new System.IntPtr(whereTheHandle.Current.NativeWindowHandle);
            WriteVerbose(cmdlet,
                         "the handle of the element the click will be performed on is " +
                         handle.ToString());

            // SetCursorPos((int)whereToClick.Current.BoundingRectangle.X,
            // (int)whereToClick.Current.BoundingRectangle.Y);
            WriteVerbose(cmdlet, "X = " + x.ToString());
            WriteVerbose(cmdlet, "Y = " + y.ToString());

            //  // IntPtr hDesk = OpenInputDesktop(0, true, 0);
            //  // IntPtr hDesk = OpenInputDesktop(DF_ALLOWOTHERACCOUNTHOOK, true, GENERIC_ALL);
            //  // IntPtr hDesk = OpenInputDesktop(0, true, 0x0000037f);
            //
            // IntPtr hOrigThread = GetThreadDesktop(GetCurrentThreadId());
            // IntPtr hOrigDesktop = OpenInputDesktop(0, false, DESKTOP_SWITCHDESKTOP);
            // WriteVerbose(this,
            // "the handle to the input desktop is " +
            // hOrigDesktop.ToString());
            //  // MessageBox.Show(x.ToString());
            // bool Success = SetThreadDesktop(hOrigDesktop);
            // WriteVerbose(this,
            // "the input desktop has been set (SetThreadDesktop): " +
            // Success.ToString());
            // Success = SwitchDesktop(hOrigDesktop);
            //  // MessageBox.Show(Success.ToString());
            // WriteVerbose(this,
            // "the input desktop has been set (SwitchDesktop): " +
            // Success.ToString());

            //  // Save original desktop
            // hOriginalThread = GetThreadDesktop(GetCurrentThreadId());
            // hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP);
            //
            //  // Create a new Desktop and switch to it
            // hNewDesktop = CreateDesktop("NewDesktopName", NULL, NULL, 0, GENERIC_ALL, NULL);
            // SetThreadDesktop(hNewDesktop);
            // SwitchDesktop(hNewDesktop);
            //
            //  // Execute thread/process in the new desktop
            // StartThread();
            // StartProcess();
            //
            //  // Restore original desktop
            // SwitchDesktop(hOriginalInput);
            // SetThreadDesktop(hOriginalThread);
            //
            //  // Close the Desktop
            // CloseDesktop(hNewDesktop);

            try {
                whereTheHandle.SetFocus();
            }
            catch { }

            bool setCursorPosResult =
                NativeMethods.SetCursorPos(x, y);
            WriteVerbose(cmdlet, "SetCursorPos result = " + setCursorPosResult.ToString());

            System.Threading.Thread.Sleep(Preferences.OnClickDelay);

            // trying to heal context menu clicks
            System.Diagnostics.Process windowProcess =
                System.Diagnostics.Process.GetProcessById(
                    whereTheHandle.Current.ProcessId);
            if (windowProcess != null) {
                IntPtr mainWindow =
                    windowProcess.MainWindowHandle;
                if (mainWindow != IntPtr.Zero) {
            // System.IntPtr lParam2 =
            // new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
            // (((new IntPtr(0x0201)).ToInt32() & 0xFFFF) << 16));
                    System.IntPtr lParam2 =
                        new IntPtr(((new IntPtr(ulAct)).ToInt32() & 0xFFFF) +
                                   (((new IntPtr(uhAct)).ToInt32() & 0xFFFF) << 16));
                    bool res0 =
                        // 20130118
                        //NativeMethods.SendMessage1(handle, NativeMethods.WM_MOUSEACTIVATE,
                        // 20130312
                        NativeMethods.PostMessage1(handle, NativeMethods.WM_MOUSEACTIVATE,
                                     mainWindow, lParam2);
                    WriteVerbose(this, "WM_MOUSEACTIVATE is sent");
                }
            }

            // if (Ctrl) {
            // PostMessage1(handle, WM_KEYDOWN, new IntPtr(VK_CONTROL), lParamKeyDown);
            // }
            // if (Shift) {
            // PostMessage1(handle, WM_KEYDOWN, new IntPtr(VK_SHIFT), lParamKeyDown);
            // }

            if (Ctrl) {
            // PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_CONTROL), lParamKeyDown);
            // PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_CONTROL), lParamKeyDown);
            // WriteVerbose(this, "WM_SYSKEYDOWN VK_CONTROL");
                // press the control key
                // keybd_event((byte)VK_LCONTROL, 0x45, 0, 0);
                NativeMethods.keybd_event((byte)NativeMethods.VK_LCONTROL, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | 0, 0);
                WriteVerbose(this, " the control button has been pressed");
            }
            if (Shift) {
            // PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_SHIFT), lParamKeyDown);
            // PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_SHIFT), lParamKeyDown);
            // WriteVerbose(this, "WM_SYSKEYDOWN VK_SHIFT");
                // press the shift key
                // keybd_event((byte)VK_LSHIFT, 0x45, 0, 0);
                NativeMethods.keybd_event((byte)NativeMethods.VK_LSHIFT, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | 0, 0);
                WriteVerbose(this, " the shift button has been pressed");
            }

            // // 20120620 for Home Tab
            bool res1 = NativeMethods.PostMessage1(handle, uDown, wParamDown, lParam);
            // 20130312
            //bool res1 = NativeMethods.SendMessage1(handle, uDown, wParamDown, lParam);

            // 20130312
            // MouseMove
            if (RightClick || DoubleClick) {
                bool resMM = NativeMethods.PostMessage1(handle, NativeMethods.WM_MOUSEMOVE, wParamDown, lParam);
            }

            // if (Ctrl) {
            //// PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_CONTROL), lParamKeyDown);
            //// PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_CONTROL), lParamKeyDown);
            //// WriteVerbose(this, "WM_SYSKEYDOWN VK_CONTROL");
            //  // press the control key
            // keybd_event((byte)VK_CONTROL, 0x45, 0, 0);
            // }
            // if (Shift) {
            //// PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_SHIFT), lParamKeyDown);
            //// PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_SHIFT), lParamKeyDown);
            //// WriteVerbose(this, "WM_SYSKEYDOWN VK_SHIFT");
            //  // press the shift key
            // keybd_event((byte)VK_SHIFT, 0x45, 0, 0);
            // }

            // bool res2 = PostMessage1(handle, uUp, IntPtr.Zero, lParam);
            // bool res2 = PostMessage1(handle, uUp, wParam, lParam);
            // System.Threading.Thread.Sleep(50);

            // // 20120620 for Home Tab
            bool res2 = NativeMethods.PostMessage1(handle, uUp, wParamUp, lParam);
            // 20130312
            //bool res2 = NativeMethods.SendMessage1(handle, uUp, wParamUp, lParam);

            // if (Ctrl) {
            //// PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_CONTROL), lParamKeyDown);
            //// PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_CONTROL), lParamKeyDown);
            //// WriteVerbose(this, "WM_SYSKEYDOWN VK_CONTROL");
            // }
            // if (Shift) {
            //// PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_SHIFT), lParamKeyDown);
            //// PostMessage1(handle, WM_SYSKEYDOWN, new IntPtr(VK_SHIFT), lParamKeyDown);
            //// WriteVerbose(this, "WM_SYSKEYDOWN VK_SHIFT");
            // }

            if (!inSequence) {
                if (Ctrl) {
            // PostMessage1(handle, WM_SYSKEYUP, new IntPtr(VK_CONTROL), lParamKeyUp);
            // WriteVerbose(this, "WM_SYSKEYUP VK_CONTROL");
                    // ::ZeroMemory
                    // release the control key
                    // keybd_event((byte)VK_LCONTROL, 0x45, KEYEVENTF_KEYUP, 0);
                    NativeMethods.keybd_event((byte)NativeMethods.VK_LCONTROL, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | NativeMethods.KEYEVENTF_KEYUP, 0);
                    WriteVerbose(this, " the control button has been released");
                }
                if (Shift) {
            // PostMessage1(handle, WM_SYSKEYUP, new IntPtr(VK_SHIFT), lParamKeyUp);
            // WriteVerbose(this, "WM_SYSKEYUP VK_SHIFT");
                    // release the shift key
                    // keybd_event((byte)VK_LSHIFT, 0x45, KEYEVENTF_KEYUP, 0);
                    NativeMethods.keybd_event((byte)NativeMethods.VK_LSHIFT, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | NativeMethods.KEYEVENTF_KEYUP, 0);
                    WriteVerbose(this, " the shift button has been released");
                }
            }
            // bool res1 = SendMessage1(handle, uDown, wParam, lParam);
            // bool res2 = SendMessage1(handle, uUp, IntPtr.Zero, lParam);

            WriteVerbose(cmdlet,
                         // // 20120620 for Home Tab
                         // 20130312
                         "PostMessage " + uDown.ToString() +
                         //"SendMessage " + uDown.ToString() +
                         " result = " + res1.ToString());
            WriteVerbose(cmdlet,
                         // // 20120620 for Home Tab
                         // 20130312
                         "PostMessage " + uUp.ToString() +
                         //"SendMessage " + uUp.ToString() +
                         " result = " + res2.ToString());
            // if (!res1 && !res2) {
            if (res1 && res2) {
                result = true;
            } else {
                result = false;
            }

            //} // 20120823

            return result;
        }
Ejemplo n.º 7
0
        //        protected override void BeginProcessing()
        //        {
        //            WriteVerbose(this, "BeginProcessing()");
        //        }
        protected bool GetColorProbe(HasControlInputCmdletBase cmdlet,
                                     AutomationElement element)
        {
            bool result = false;

            //NativeMethods.getpi

            return result;
        }
Ejemplo n.º 8
0
        internal bool CheckControl(HasControlInputCmdletBase cmdlet)
        {
            bool result = false;
            // string cmdletName = cmdlet.GetType().Name.Replace("UIA", "-UIA");
            // 20120228 string cmdletName = CmdletName(cmdlet);

            // 20120823
            foreach (AutomationElement inputObject in cmdlet.InputObject) {

            // 20120823
            //if (cmdlet.InputObject == null)
            if (null == inputObject) {

            // 20120823
            //{
                //WriteVerbose(cmdletName + ": Control is null");
                this.WriteVerbose(cmdlet, "[checking the control] Control is null");
                if (this.PassThru) {
                    //WriteObject(this, null);
                    // 20120823
                    //WriteObject(this, this.InputObject);
                    this.WriteObject(this, inputObject);
                } else {
                    result = false;
                    this.WriteObject(this, result);
                }
                // 20120830
                //return false;
                result = false;
                return result;
            }
            System.Windows.Automation.AutomationElement _control = null;
            try {

                _control =
                    // 20120823
                    //(System.Windows.Automation.AutomationElement)(cmdlet.InputObject);
                    (AutomationElement)inputObject;

                this.WriteVerbose(cmdlet,
                             //"[checking the control] the given control is of the " +
                             // 20130108
                             "[checking the control] the input control is of the " +
                             // 20120823
                             //cmdlet.InputObject.Current.ControlType.ProgrammaticName +
                             inputObject.Current.ControlType.ProgrammaticName +
                             " type");

                //WriteVerbose(cmdlet,
                //             "[checking the control] the given control is of the " +
                //             _control.Current.ControlType.ProgrammaticName +
                //             " type");
                cmdlet._window = _control;

                result = true;
                // WriteObject(result);
                // there's no need to output the True value
                // since the output will be what we want
                // (some part of AutomationElement, as an example)
            } catch (Exception eControlTypeException) {

                this.WriteDebug(cmdlet, "[checking the control] Control is not an AutomationElement");
                this.WriteDebug(cmdlet, "[checking the control] " + eControlTypeException.Message);
                if (this.PassThru) {

                    // result = null;
                    //WriteObject(this, null);
                    WriteObject(this, _control);
                } else {

                    result = false;
                    this.WriteObject(this, result);
                }
                // result = false;
                // WriteObject(result);
                // return result;
                // 20120830
                //return false;
                result = false;
                return result;
            }

            } // 20120823

            return result;
        }
Ejemplo n.º 9
0
 // 20120312 0.6.11
 //protected void SleepAndRunScriptBlocks(HasTimeoutCmdletBase cmdlet)
 protected void SleepAndRunScriptBlocks(HasControlInputCmdletBase cmdlet)
 {
     this.RunOnSleepScriptBlocks(cmdlet);
     System.Threading.Thread.Sleep(Preferences.OnSleepDelay);
     // RunScriptBlocks(cmdlet);
 }
Ejemplo n.º 10
0
 // 20120209 protected void RunEventScriptBlocks(EventCmdletBase cmdlet)
 //protected internal void RunEventScriptBlocks(EventCmdletBase cmdlet)
 protected internal void RunEventScriptBlocks(HasControlInputCmdletBase cmdlet)
 {
     System.Collections.Generic.List<ScriptBlock >  blocks =
         new System.Collections.Generic.List<ScriptBlock > ();
     this.WriteVerbose(cmdlet,
                       blocks.Count.ToString() +
                       " events to fire");
     if (cmdlet.EventAction != null &&
         cmdlet.EventAction.Length > 0) {
         foreach (ScriptBlock sb in cmdlet.EventAction) {
             blocks.Add(sb);
             this.WriteVerbose(cmdlet,
                               "the scriptblock: " +
                               sb.ToString() +
                               " is ready to be fired");
         }
     }
     runScriptBlocks(blocks, cmdlet, true);
     // runEventScriptBlocks(blocks, cmdlet); //, true);
 }
Ejemplo n.º 11
0
 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);
     }
 }
Ejemplo n.º 12
0
        internal bool CheckAndPrepareInput(HasControlInputCmdletBase cmdlet)
        {
            bool result = false;
            
            if (null == cmdlet.InputObject) {
                
                WriteVerbose(cmdlet, "[checking the input] Control(s) are null");
                
                cmdlet.WriteError(
                    cmdlet,
                    "The pipeline input is null",
                    "InputIsNull",
                    ErrorCategory.InvalidArgument,
                    true);
                
            }
            
            foreach (var inputObject in cmdlet.InputObject) {
                
                if (null == inputObject) {
                    
                    WriteVerbose(cmdlet, "[checking the input] Control is null");
                    if (PassThru) {
                        
                        WriteObject(this, inputObject);
                    } else {
                        
                        result = false;
                        WriteObject(this, result);
                    }
                    
                    result = false;
                    
                    cmdlet.WriteError(
                        cmdlet,
                        "A part of the pipeline input is null",
                        "PartOfInputIsNull",
                        ErrorCategory.InvalidArgument,
                        false);
                    
                }
                
                // 20131109
                //System.Windows.Automation.AutomationElement _control = null;
                //IUiElement _controlAdapter = null;
                var _controlAdapter = inputObject;
                
                try {
                    
                    // 20131109
                    //_control = 
                    //    (AutomationElement)inputObject;
                    
                    // 20131109
//                    this.WriteVerbose(
//                        cmdlet,
//                        "[checking the input] the input control is of the " +
//                        inputObject.Current.ControlType.ProgrammaticName +
//                        " type");
                    
                    // 20131109
                    //cmdlet.currentWindow = _control;
                    //if (inputObject is IUiElement) {
                    if (null != (inputObject as IUiElement))
                    {
                    /*
                    if (inputObject is IUiElement) {
                    */
                        cmdlet.CurrentInputElement = (IUiElement)_controlAdapter;
                    }
//                    if (inputObject is AutomationElement) {
//                        cmdlet.currentWindow = new UiElement((AutomationElement)inputObject);
//                    }
                    
                    result = true;
                    
                    // there's no need to output the True value
                    // since the output will be what we want 
                    // (some part of AutomationElement, as an example)
                } catch (Exception) {
                    
                    if (PassThru) {
                        
                        // 20131109
                        //WriteObject(this, _control);
                        WriteObject(this, _controlAdapter);
                        
                    } else {
                        
                        result = false;
                        WriteObject(this, result);
                        
                    }
                    result = false;
                    return result;
                }
            
            } // 20120823
            
            return result;
        }
Ejemplo n.º 13
0
        protected internal bool ClickControl(HasControlInputCmdletBase cmdlet,
                                    IUiElement element,
                                    ClickSettings settings)
        {
            bool result = false;
            
            if (-1000000 == settings.RelativeX) {
                settings.RelativeX = Preferences.ClickOnControlByCoordX;
            }
            if (-1000000 == settings.RelativeY) {
                settings.RelativeY = Preferences.ClickOnControlByCoordY;
            }
            
            IUiElement whereToClick = 
                element;
            IUiElement whereTheHandle = 
                whereToClick;
            
            // 20140312
            // if (whereToClick.Current.NativeWindowHandle == 0) {
            if (whereToClick.GetCurrent().NativeWindowHandle == 0) {
                
                whereTheHandle =
                    whereToClick.GetAncestorWithHandle();
                // 20140312
                // if (whereTheHandle.Current.NativeWindowHandle == 0) {
                if (whereTheHandle.GetCurrent().NativeWindowHandle == 0) {
                    
                    WriteError(
                        this,
                        "The handle of this control equals to zero",
                        "ZeroHandle",
                        ErrorCategory.InvalidArgument,
                        true);

                    // TODO: WriteError(...)
                } else {
//                    WriteVerbose(cmdlet, 
//                                 "the control with a handle is " + 
//                                 whereTheHandle.Current.Name);
//                    WriteVerbose(cmdlet, 
//                                 "its handle is " + 
//                                 whereTheHandle.Current.NativeWindowHandle.ToString());
//                    WriteVerbose(cmdlet, 
//                                 "its rectangle is " + 
//                                 whereTheHandle.Current.BoundingRectangle.ToString());
                }
            }
            
            int x = 0;
            int y = 0;
            // these x and y are window-related coordinates
            if (settings.RelativeX != 0 && settings.RelativeY != 0) {
                // 20140312
//                x = settings.RelativeX + (int)whereToClick.Current.BoundingRectangle.X;
//                y = settings.RelativeY + (int)whereToClick.Current.BoundingRectangle.Y;
                x = settings.RelativeX + (int)whereToClick.GetCurrent().BoundingRectangle.X;
                y = settings.RelativeY + (int)whereToClick.GetCurrent().BoundingRectangle.Y;
            } else {
                // these x and y are for the SetCursorPos call
                // they are screen coordinates
                // 20140312
//                x = (int)whereToClick.Current.BoundingRectangle.X + 
//                    ((int)whereToClick.Current.BoundingRectangle.Width / 2); // + 3;
//                y = (int)whereToClick.Current.BoundingRectangle.Y + 
//                    ((int)whereToClick.Current.BoundingRectangle.Height / 2); // + 3;
                x = (int)whereToClick.GetCurrent().BoundingRectangle.X + 
                    ((int)whereToClick.GetCurrent().BoundingRectangle.Width / 2); // + 3;
                y = (int)whereToClick.GetCurrent().BoundingRectangle.Y + 
                    ((int)whereToClick.GetCurrent().BoundingRectangle.Height / 2); // + 3;
            }
            // if the -X and -Y paramters are supplied (only for SetCursorPos)
            
            // PostMessage's (click) second parameter
            uint uDown = 0;
            uint uUp = 0;
            
            // these relative coordinates for SendMessage/PostMessage
            // 20140312
//            settings.RelativeX = x - (int)whereTheHandle.Current.BoundingRectangle.X;
//            settings.RelativeY = y - (int)whereTheHandle.Current.BoundingRectangle.Y;
            settings.RelativeX = x - (int)whereTheHandle.GetCurrent().BoundingRectangle.X;
            settings.RelativeY = y - (int)whereTheHandle.GetCurrent().BoundingRectangle.Y;
            
            // PostMessage's (click) third and fourth paramters (the third'll be reasigned later)
            IntPtr wParamDown = IntPtr.Zero;
            IntPtr wParamUp = IntPtr.Zero;
            var lParam =
                new IntPtr(((new IntPtr(settings.RelativeX)).ToInt32() & 0xFFFF) +
                           (((new IntPtr(settings.RelativeY)).ToInt32() & 0xFFFF) << 16));
            
            /*
            IntPtr lParam = 
                new IntPtr(((new IntPtr(settings.RelativeX)).ToInt32() & 0xFFFF) +
                           (((new IntPtr(settings.RelativeY)).ToInt32() & 0xFFFF) << 16));
            */
            
            // PostMessage's (keydown/keyup) fourth parameter
            const uint uCtrlDown = 0x401D;
            const uint uCtrlUp = 0xC01D;
            const uint uShiftDown = 0x402A;
            const uint uShiftUp = 0xC02A;
            IntPtr lParamKeyDown = IntPtr.Zero;
            IntPtr lParamKeyUp = IntPtr.Zero;
            
            if (settings.Ctrl) {
                lParamKeyDown = 
                    new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
                               (((new IntPtr(uCtrlDown)).ToInt32() & 0xFFFF) << 16));
                lParamKeyUp = 
                    new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
                               (((new IntPtr(uCtrlUp)).ToInt32() & 0xFFFF) << 16));
            }
            if (settings.Shift) {
                lParamKeyDown = 
                    new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
                               (((new IntPtr(uShiftDown)).ToInt32() & 0xFFFF) << 16));
                lParamKeyUp = 
                    new IntPtr(((new IntPtr(0x0001)).ToInt32() & 0xFFFF) +
                               (((new IntPtr(uShiftUp)).ToInt32() & 0xFFFF) << 16));
            }
            // PostMessage's (activate) third parameter
            uint ulAct = 0;
            uint uhAct = 0;
            
            uint mask = 0;
            if (settings.Ctrl) {
                mask |= NativeMethods.MK_CONTROL;
            }
            if (settings.Shift) {
                mask |= NativeMethods.MK_SHIFT;
            }
            
            if (settings.RightClick && !settings.DoubleClick) {
                uhAct = uDown = NativeMethods.WM_RBUTTONDOWN;
                uUp = NativeMethods.WM_RBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_RBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_RBUTTON;
            } else if (settings.RightClick && settings.DoubleClick) {
                uhAct = uDown = NativeMethods.WM_RBUTTONDBLCLK;
                uUp = NativeMethods.WM_RBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_RBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_RBUTTON;
            } else if (settings.MidClick && !settings.DoubleClick) {
                uhAct = uDown = NativeMethods.WM_MBUTTONDOWN;
                uUp = NativeMethods.WM_MBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_MBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_MBUTTON;
            } else if (settings.MidClick && settings.DoubleClick) {
                uhAct = uDown = NativeMethods.WM_MBUTTONDBLCLK;
                uUp = NativeMethods.WM_MBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_MBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_MBUTTON;
            } else if (settings.DoubleClick) {
                uhAct = uDown = NativeMethods.WM_LBUTTONDBLCLK;
                uUp = NativeMethods.WM_LBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_LBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_LBUTTON;
            } else {
                uhAct = uDown = NativeMethods.WM_LBUTTONDOWN;
                uUp = NativeMethods.WM_LBUTTONUP;
                wParamDown = new IntPtr(NativeMethods.MK_LBUTTON | mask);
                wParamUp = new IntPtr(mask);
                ulAct = NativeMethods.MK_LBUTTON;
            }
            
            // 20140312
            // var handle = new IntPtr(whereTheHandle.Current.NativeWindowHandle);
            var handle = new IntPtr(whereTheHandle.GetCurrent().NativeWindowHandle);
            /*
            IntPtr handle =
                    new IntPtr(whereTheHandle.Current.NativeWindowHandle);
            */
            
            try {
                whereTheHandle.SetFocus();
            } 
            catch { }
            
            bool setCursorPosResult = 
                NativeMethods.SetCursorPos(x, y);
//            WriteVerbose(cmdlet, "SetCursorPos result = " + setCursorPosResult.ToString());
            
            Thread.Sleep(Preferences.OnClickDelay);
            
            // trying to heal context menu clicks
            Process windowProcess = 
                Process.GetProcessById(
                    // 20140312
                    // whereTheHandle.Current.ProcessId);
                    whereTheHandle.GetCurrent().ProcessId);
            if (windowProcess != null) {
                IntPtr mainWindow = 
                    windowProcess.MainWindowHandle;
                if (mainWindow != IntPtr.Zero) {
                    
                    var lParam2 = 
                        new IntPtr(((new IntPtr(ulAct)).ToInt32() & 0xFFFF) +
                                   (((new IntPtr(uhAct)).ToInt32() & 0xFFFF) << 16));
                    /*
                    IntPtr lParam2 = 
                        new IntPtr(((new IntPtr(ulAct)).ToInt32() & 0xFFFF) +
                                   (((new IntPtr(uhAct)).ToInt32() & 0xFFFF) << 16));
                    */
                    bool res0 = 
                        NativeMethods.PostMessage1(handle, NativeMethods.WM_MOUSEACTIVATE, 
                                     mainWindow, lParam2);
//                    WriteVerbose(this, "WM_MOUSEACTIVATE is sent");
                }
            }
            
            if (settings.Ctrl) {
                // press the control key
                NativeMethods.keybd_event((byte)NativeMethods.VK_LCONTROL, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | 0, 0);
//                WriteVerbose(this, " the Control button has been pressed");
            }
            if (settings.Shift) {
                // press the settings.Shift key
                NativeMethods.keybd_event((byte)NativeMethods.VK_LSHIFT, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | 0, 0);
//                WriteVerbose(this, " the Shift button has been pressed");
            }
            
            // // 20120620 for Home Tab
            bool res1 = NativeMethods.PostMessage1(handle, uDown, wParamDown, lParam);
            
            int interval = settings.DoubleClickInterval / 2;
            if (settings.DoubleClick) {
                Thread.Sleep(interval);
            }
            
            // MouseMove
            if (settings.RightClick || settings.DoubleClick) {
                bool resMM = NativeMethods.PostMessage1(handle, NativeMethods.WM_MOUSEMOVE, wParamDown, lParam);
            }
            
            // 20131125
            if (settings.DoubleClick) {
                Thread.Sleep(interval);
            }
            
            // // 20120620 for Home Tab
            bool res2 = NativeMethods.PostMessage1(handle, uUp, wParamUp, lParam);
            
            if (!settings.inSequence) {
                if (settings.Ctrl) {
                    // release the control key
                    NativeMethods.keybd_event((byte)NativeMethods.VK_LCONTROL, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | NativeMethods.KEYEVENTF_KEYUP, 0);
//                    WriteVerbose(this, " the Control button has been released");
                }
                if (settings.Shift) {
                    // release the settings.Shift key
                    NativeMethods.keybd_event((byte)NativeMethods.VK_LSHIFT, 0x45, NativeMethods.KEYEVENTF_EXTENDEDKEY | NativeMethods.KEYEVENTF_KEYUP, 0);
//                    WriteVerbose(this, " the Shift button has been released");
                }
            }
            
//            WriteVerbose(cmdlet,
//                         // // 20120620 for Home Tab
//                         // 20130312
//                         "PostMessage " + uDown.ToString() + 
//                         //"SendMessage " + uDown.ToString() + 
//                         " result = " + res1.ToString());
//            WriteVerbose(cmdlet, 
//                         // // 20120620 for Home Tab
//                         // 20130312
//                         "PostMessage " + uUp.ToString() +
//                         //"SendMessage " + uUp.ToString() + 
//                         " result = " + res2.ToString());
            // if (!res1 && !res2) {
            if (res1 && res2) {
                result = true;
            } else {
                result = false;
            }
            
            return result;
        }
Ejemplo n.º 14
0
        // 20120209 protected void RunEventScriptBlocks(EventCmdletBase cmdlet)
        //protected internal void RunEventScriptBlocks(EventCmdletBase cmdlet)
        protected internal void RunEventScriptBlocks(HasControlInputCmdletBase cmdlet)
        {
            System.Collections.Generic.List<ScriptBlock >  blocks =
                new System.Collections.Generic.List<ScriptBlock > ();
            this.WriteVerbose(cmdlet,
                              blocks.Count.ToString() +
                              " events to fire");
            if (cmdlet.EventAction != null &&
                cmdlet.EventAction.Length > 0) {
                foreach (ScriptBlock sb in cmdlet.EventAction) {
                    blocks.Add(sb);
                    this.WriteVerbose(cmdlet,
                                      "the scriptblock: " +
                                      sb.ToString() +
                                      " is ready to be fired");
                }
            }
            // 20130318
            //runScriptBlocks(blocks, cmdlet, true);
            // 20130319
            try {
                runScriptBlocks(blocks, cmdlet, true, null);
            }
            catch (Exception eScriptBlocks) {

                cmdlet.WriteError(
                    cmdlet,
                    eScriptBlocks.Message,
                    "ScriptblocksFailed",
                    ErrorCategory.InvalidResult,
                    true);
            }
            // runEventScriptBlocks(blocks, cmdlet); //, true);
        }
Ejemplo n.º 15
0
 // 20120312 0.6.11
 //protected void SleepAndRunScriptBlocks(HasTimeoutCmdletBase cmdlet)
 protected internal void SleepAndRunScriptBlocks(HasControlInputCmdletBase cmdlet)
 {
     // 20130318
     //this.RunOnSleepScriptBlocks(cmdlet);
     this.RunOnSleepScriptBlocks(cmdlet, null);
     System.Threading.Thread.Sleep(Preferences.OnSleepDelay);
     // RunScriptBlocks(cmdlet);
 }