Example #1
0
        //build window command
        private static void BuildWindowCommand(IntPtr hWinEventHook, _systemEvents @event,
                                               IntPtr hwnd, int idObject, int idChild,
                                               uint dwEventThread, uint dwmsEventTime)
        {
            switch (@event)
            {
            case _systemEvents.EventMin:
                return;

            case _systemEvents.EventMax:
                return;

            case _systemEvents.EventSystemForeGround:
                break;

            case _systemEvents.MinimizeEnd:
                return;

            case _systemEvents.MinimizeStart:
                return;

            default:
                return;
            }

            int length     = GetWindowText(hwnd, _buffer, _buffer.Capacity);
            var windowName = _buffer.ToString();

            //bypass screen recorder and Cortana (Win10) which throws errors
            if ((windowName == "Screen Recorder") || (windowName == "Cortana"))
            {
                return;
            }

            if (length > 0)
            {
                //wait additional for window to initialize
                //System.Threading.Thread.Sleep(250);
                windowName = _buffer.ToString();

                ActivateWindowCommand activateWindowCommand = new ActivateWindowCommand
                {
                    v_WindowName = windowName,
                    v_Comment    = "Generated by Screen Recorder @ " + DateTime.Now.ToString()
                };

                GeneratedCommands.Add(activateWindowCommand);

                //detect if tracking window open location or activate windows to top left
                if (_trackWindowOpenLocations)
                {
                    User32Functions.GetWindowRect(hwnd, out Rect windowRect);

                    MoveWindowCommand moveWindowCommand = new MoveWindowCommand
                    {
                        v_WindowName     = windowName,
                        v_XMousePosition = windowRect.left.ToString(),
                        v_YMousePosition = windowRect.top.ToString(),
                        v_Comment        = "Generated by Screen Recorder @ " + DateTime.Now.ToString()
                    };

                    GeneratedCommands.Add(moveWindowCommand);
                }
                else if (_activateWindowTopLeft)
                {
                    //generate command to set window position
                    MoveWindowCommand moveWindowCommand = new MoveWindowCommand
                    {
                        v_WindowName     = windowName,
                        v_XMousePosition = "0",
                        v_YMousePosition = "0",
                        v_Comment        = "Generated by Screen Recorder @ " + DateTime.Now.ToString()
                    };

                    User32Functions.SetWindowPosition(hwnd, 0, 0);
                    GeneratedCommands.Add(moveWindowCommand);
                }

                //if tracking window sizes is set
                if (_trackActivatedWindowSizes)
                {
                    //create rectangle from hwnd
                    User32Functions.GetWindowRect(hwnd, out Rect windowRect);

                    //do math to get height, etc
                    var width  = windowRect.right - windowRect.left;
                    var height = windowRect.bottom - windowRect.top;

                    //generate command to set window position
                    ResizeWindowCommand reszWindowCommand = new ResizeWindowCommand
                    {
                        v_WindowName  = windowName,
                        v_XWindowSize = width.ToString(),
                        v_YWindowSize = height.ToString(),
                        v_Comment     = "Generated by Screen Recorder @ " + DateTime.Now.ToString()
                    };

                    //add to list
                    GeneratedCommands.Add(reszWindowCommand);
                }
            }
        }
        public async override Task RunCommand(object sender)
        {
            var engine        = (IAutomationEngineInstance)sender;
            var browserObject = ((OBAppInstance)await v_InstanceName.EvaluateCode(engine)).Value;
            var chromeProcess = (Process)browserObject;
            var vTimeout      = (int)await v_Timeout.EvaluateCode(engine);

            var vXAdjustment = (int)await v_XAdjustment.EvaluateCode(engine);

            var vYAdjustment = (int)await v_YAdjustment.EvaluateCode(engine);

            User32Functions.GetWindowRect(chromeProcess.MainWindowHandle, out Rect chromeRect);

            WebElement webElement = await NativeHelper.DataTableToWebElement(v_NativeSearchParameters, engine);

            webElement.Value = v_OpenLinkOptions;

            User32Functions.BringWindowToFront(chromeProcess.MainWindowHandle);

            string responseText;

            if ((v_ClickButton == "Left" || v_ClickButton == "Middle") && v_ClickType == "Simulate Click")
            {
                string clickButton = v_ClickButton.ToLower() + "click";
                NativeRequest.ProcessRequest(clickButton, JsonConvert.SerializeObject(webElement), vTimeout, out responseText);
                NativeResponse responseObject = JsonConvert.DeserializeObject <NativeResponse>(responseText);

                if (responseObject.Status == "Failed")
                {
                    throw new Exception(responseObject.Result);
                }

                if (v_OpenLinkOptions == "New Window" && v_ClickButton == "Left")
                {
                    //Create new browser
                    int chromeProcessCount = Process.GetProcessesByName("chrome").Length;
                    var process            = Process.Start("chrome.exe", responseObject.Result + " --new-window");

                    if (chromeProcessCount > 0)
                    {
                        while (process.HasExited == false)
                        {
                            Thread.Sleep(100);
                            process.Refresh();
                        }
                    }
                    //Delay 7 seconds
                    Thread.Sleep(7000);

                    Process[] procsChrome = Process.GetProcessesByName("chrome");

                    foreach (Process chrome in procsChrome)
                    {
                        // the chrome process must have a window
                        if (chrome.MainWindowHandle == IntPtr.Zero)
                        {
                            continue;
                        }

                        process = chrome;
                        break;
                    }

                    new OBAppInstance(v_NewInstanceName, process).SetVariableValue(engine, v_NewInstanceName);
                }
            }
            else
            {
                NativeRequest.ProcessRequest("getcoordinates", JsonConvert.SerializeObject(webElement), vTimeout, out responseText);
                NativeResponse responseObject = JsonConvert.DeserializeObject <NativeResponse>(responseText);

                if (responseObject.Status == "Failed")
                {
                    throw new Exception(responseObject.Result);
                }

                var screenPosition = responseObject.Result.Split(',');

                /* Following are the values returned from extension
                 * screenPosition[0] = element.left, screenPosition[1] = element.top,
                 * screenPosition[2] = element.width, screenPosition[3] = element.height,
                 * screenPosition[4] = chrome toolbar height */
                int xPosition = chromeRect.left + Convert.ToInt32(Convert.ToDouble(screenPosition[0])) + (Convert.ToInt32(Convert.ToDouble(screenPosition[2])) / 2) + vXAdjustment;
                int yPosition = chromeRect.top + Convert.ToInt32(Convert.ToDouble(screenPosition[1])) + Convert.ToInt32(Convert.ToDouble(screenPosition[4])) + (Convert.ToInt32(Convert.ToDouble(screenPosition[3])) / 2) + vYAdjustment;

                User32Functions.SetCursorPosition(xPosition, yPosition);

                if (v_ClickButton == "Left")
                {
                    User32Functions.SendMouseClick("Left Click", xPosition, yPosition);
                }
                else if (v_ClickButton == "Right")
                {
                    User32Functions.SendMouseClick("Right Click", xPosition, yPosition);
                }
                else
                {
                    User32Functions.SendMouseClick("Middle Click", xPosition, yPosition);
                }
            }
        }