Exemplo n.º 1
0
        public void Execute(Arguments arguments)
        {
            if (arguments.Threshold.Value < 0 || arguments.Threshold.Value > 1)
            {
                throw new ArgumentOutOfRangeException("Threshold must be a value from 0 to 1.");
            }

            using (Bitmap bitmap1 = Imaging.OpenImageFile(arguments.Image1.Value, nameof(arguments.Image1)))
                using (Bitmap bitmap2 = (string.IsNullOrEmpty(arguments.Image2?.Value)) ?
                                        RobotWin32.GetPartOfScreen(
                           Imaging.ParseRectanglePositionFromArguments(arguments.ScreenSearchArea.Value, arguments.Relative.Value),
                           bitmap1.PixelFormat) :
                                        Imaging.OpenImageFile(arguments.Image2.Value, nameof(arguments.Image2)))
                {
                    Rectangle foundRectangle = Imaging.IsImageInImage(bitmap1, bitmap2, (double)arguments.Threshold.Value);


                    if (foundRectangle == Rectangle.Empty)
                    {
                        throw new ArgumentException("Image was not found in specified search area.");
                    }
                    else
                    {
                        Point foundPoint = (!arguments.CenterResult.Value) ?
                                           new Point(foundRectangle.X, foundRectangle.Y) :
                                           new Point(foundRectangle.X + foundRectangle.Width / 2, foundRectangle.Y + foundRectangle.Height / 2);
                        foundPoint = new Point(foundPoint.X + arguments.OffsetX.Value, foundPoint.Y + arguments.OffsetY.Value);
                        Scripter.Variables.SetVariableValue(arguments.Result.Value, new PointStructure(foundPoint));
                    }
                }
        }
Exemplo n.º 2
0
 private void highlightToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         if (controlsTree.SelectedNode != null)
         {
             if (controlsTree.SelectedNode.Tag is UIElement uiElement)
             {
                 var element = UIElement.FromWPath(uiElement.ToWPath());
                 if (element != null)
                 {
                     var window = GetTopLevelWindow(uiElement.AutomationElement);
                     if (window != null)
                     {
                         var iHandle = window.FrameworkAutomationElement.NativeWindowHandle;
                         if (iHandle != IntPtr.Zero)
                         {
                             RobotWin32.BringWindowToFront(iHandle);
                             var rect = element.GetRectangle();
                             if (rect != null)
                             {
                                 InitializeRectangleForm(rect);
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         RobotMessageBox.Show(ex.Message, "Error");
     }
 }
Exemplo n.º 3
0
        public void Execute(Arguments arguments)
        {
            var wrapper = MsCrmManager.CurrentCRM;

            if (wrapper == null)
            {
                throw new ApplicationException("Could not activate Dynamics CRM instance. It has to be attached first.");
            }
            IntPtr iHandle = wrapper.Ie.NativeBrowser.hWnd;

            Scripter.Log.Log(AbstractLogger.Level.Info, "Window '" + wrapper.Title + "' has been found");
            Scripter.LastWindow = (iHandle);
            RobotWin32.BringWindowToFront(iHandle);
            if (arguments.Style.Value.ToLower() == "maximize")
            {
                RobotWin32.ShowWindow(iHandle, RobotWin32.ShowWindowEnum.Maximize);
            }
            else if (arguments.Style.Value.ToLower() == "minimize")
            {
                RobotWin32.ShowWindow(iHandle, RobotWin32.ShowWindowEnum.Minimize);
            }
            else if (arguments.Style.Value.ToLower() == "normal")
            {
                RobotWin32.ShowWindow(iHandle, RobotWin32.ShowWindowEnum.Restore);
            }
        }
        public void Execute(Arguments arguments)
        {
            manager = AbbyyManager.Instance;

            System.Drawing.Rectangle rectangle    = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates();
            System.Drawing.Bitmap    partOfScreen = RobotWin32.GetPartOfScreen(rectangle);

            IEngine engine = manager.Engine;
            DocumentProcessingParams processingParams  = engine.CreateDocumentProcessingParams();
            RecognizerParams         recognizingParams = processingParams.PageProcessingParams.RecognizerParams;

            recognizingParams.SetPredefinedTextLanguage(arguments.Language.Value);
            engine.LoadPredefinedProfile(AbbyyManager.TextAccuracyProfile);
            FRDocument imageDocument = engine.CreateFRDocument();

            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                partOfScreen.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                stream.Position = 0;
                IReadStream imageStream = new StreamNet2AbbyyAdapter(stream);
                imageDocument.AddImageFileFromStream(imageStream);
            }

            imageDocument.Process(processingParams);


            Scripter.Variables.SetVariableValue(arguments.Result.Value, new TextStructure(imageDocument.PlainText.Text));
        }
 public void BringWindowToForeground()
 {
     if (MainWindowHandle != IntPtr.Zero)
     {
         RobotWin32.SetForegroundWindow(MainWindowHandle);
     }
 }
        public void WaitForExistingImageTest()
        {
            testerApp = SDK.Tester.RunFormTester("Title TestApp");

            Color  color     = Color.Gainsboro;
            string colorCode = "FFDCDCDC";

            RobotWin32.ShowWindow(testerApp.MainWindowHandle, RobotWin32.ShowWindowEnum.ShowNormal);

            Scripter scripter = new Scripter();

            scripter.InitVariables.Clear();
            scripter.RunLine("window TestApp");
            scripter.InitVariables.Add(nameof(colorCode), new TextStructure(colorCode));
            scripter.Text = $@"keyboard {TextChar}FocusOnControl tbColorRGB{TextChar}
				            keyboard {SpecialChars.KeyBegin}enter{SpecialChars.KeyEnd}
                            keyboard {TextChar}{SpecialChars.Variable}{nameof(colorCode)}{TextChar} 
                            keyboard {SpecialChars.KeyBegin}enter{SpecialChars.KeyEnd}";

            scripter.Run();

            string path1 = Assembly.GetExecutingAssembly().UnpackResourceToFile("Resources." + nameof(Resources.littleGrayRectangle), "bmp");

            scripter.RunLine($"waitfor.image image {SpecialChars.Text}{path1}{SpecialChars.Text}");
        }
Exemplo n.º 7
0
        public static Rectangle ParseRectanglePositionFromArguments(Rectangle screenSearchArea, bool relative)
        {
            Rectangle result = screenSearchArea;

            if (result.X < 0 ||
                result.Y < 0 ||
                result.Width < 1 ||
                result.Height < 1)
            {
                throw new ArgumentException("ScreenSearchArea argument's parts can't be negative. Both width and height must be bigger than zero.");
            }

            if (relative)
            {
                IntPtr handle = RobotWin32.GetForegroundWindow();
                if (handle.ToInt32() == 0)
                {
                    throw new ApplicationException("Cannot find foreground window.");
                }
                RobotWin32.Rect foregroundWindowRect = new RobotWin32.Rect();
                if (RobotWin32.GetWindowRectangle(handle, ref foregroundWindowRect) == false)
                {
                    throw new ApplicationException("Cannot get foreground window rect.");
                }

                result = new Rectangle(result.X + foregroundWindowRect.Left,
                                       result.Y + foregroundWindowRect.Top,
                                       result.Width,
                                       result.Height);
            }
            return(result);
        }
Exemplo n.º 8
0
        public void Execute(Arguments arguments)
        {
            var control = AccessManager.CurrentAccess.GetControlByPath(arguments.Path.Value);

            var handle = (IntPtr)control.GetFormHwnd();

            RobotWin32.BringWindowToFront(handle);
            RobotWin32.ShowWindow(handle, RobotWin32.ShowWindowEnum.Maximize);
            control.SetFocus();
        }
Exemplo n.º 9
0
        private void ActivateBrowserWindow(MsCrmWrapper wrapper)
        {
            if (wrapper == null)
            {
                throw new ApplicationException("Could not activate Dynamics CRM instance. It has to be attached first.");
            }
            IntPtr iHandle = wrapper.Ie.NativeBrowser.hWnd;

            Scripter.Log.Log(AbstractLogger.Level.Info, "Window '" + wrapper.Title + "' has been found");
            Scripter.LastWindow = (iHandle);
            RobotWin32.BringWindowToFront(iHandle);
        }
        internal void OpenAccessObject(AccessObjectModel report)
        {
            try
            {
                var app  = GetCurrentApplication();
                var name = report.FullName ?? report.Name;

                switch (report.Type)
                {
                case MSAccess.AcObjectType.acReport:
                    app.DoCmd.OpenReport(name);
                    break;

                case MSAccess.AcObjectType.acTable:
                    app.DoCmd.OpenTable(name, MSAccess.AcView.acViewNormal, MSAccess.AcOpenDataMode.acReadOnly);
                    break;

                case MSAccess.AcObjectType.acServerView:
                    app.DoCmd.OpenView(name, MSAccess.AcView.acViewNormal, MSAccess.AcOpenDataMode.acReadOnly);
                    break;

                case MSAccess.AcObjectType.acStoredProcedure:
                    app.DoCmd.OpenStoredProcedure(name, MSAccess.AcView.acViewNormal, MSAccess.AcOpenDataMode.acReadOnly);
                    break;

                case MSAccess.AcObjectType.acQuery:
                    app.DoCmd.OpenQuery(name, MSAccess.AcView.acViewNormal, MSAccess.AcOpenDataMode.acReadOnly);
                    break;

                case MSAccess.AcObjectType.acFunction:
                    app.DoCmd.OpenFunction(name, MSAccess.AcView.acViewNormal, MSAccess.AcOpenDataMode.acReadOnly);
                    break;

                case MSAccess.AcObjectType.acDiagram:
                    app.DoCmd.OpenDiagram(name);
                    break;

                case MSAccess.AcObjectType.acMacro:
                    app.DoCmd.RunMacro(name, 1, true);
                    break;

                default:
                    throw new NotImplementedException($"Opener for {report.TypeName} not implemented.");
                }

                RobotWin32.BringWindowToFront((IntPtr)app.hWndAccessApp());
            }
            catch (Exception ex)
            {
                RobotMessageBox.Show(ex.Message);
            }
        }
        public void WaitForImageTest()
        {
            Color  color     = Color.Pink;
            string colorCode = "FFFFC0CB";

            string path = Assembly.GetExecutingAssembly().UnpackResourceToFile("Resources." + nameof(Resources.smallPink), "bmp");

            Scripter scripter = new Scripter();

            testerApp = SDK.Tester.RunFormTester($"CenterOfScreen FocusOnControl tbColorRGB ChangeColor {colorCode} ");
            RobotWin32.ShowWindow(testerApp.MainWindowHandle, RobotWin32.ShowWindowEnum.ShowNormal);

            scripter.Text = $"waitfor.image image {SpecialChars.Text}{path}{SpecialChars.Text} timeout 8000";
            scripter.Run();
        }
 public void Close()
 {
     try
     {
         int  hWnd = application.Application.Hwnd;
         uint processId;
         RobotWin32.GetWindowThreadProcessId((IntPtr)hWnd, out processId);
         Process proc = Process.GetProcessById((int)processId);
         proc.Kill();
     }
     catch (Exception ex)
     {
         throw new ApplicationException($"Error occurred while closing current Excel Instance. Message: {ex.Message}");
     }
 }
Exemplo n.º 13
0
        public void Execute(Arguments arguments)
        {
            string errorJump = arguments.ErrorJump != null && arguments.ErrorJump != null?arguments.ErrorJump.ToString() : string.Empty;

            string host = arguments.Host.Value;

            //telnet = System.Diagnostics.Process.Start(pathToTelnet, host);
            putty = System.Diagnostics.Process.Start(pathToPutty, "-load AS400");
            System.Threading.Thread.Sleep(2000);
            //RobotWin32.ShowWindow(telnet.MainWindowHandle, RobotWin32.ShowWindowEnum.ShowNormal);
            //IntPtr iHandle = RobotWin32.FindWindow(null, telnet.MainWindowTitle);
            RobotWin32.ShowWindow(putty.MainWindowHandle, RobotWin32.ShowWindowEnum.ShowNormal);
            IntPtr iHandle = RobotWin32.FindWindow(null, putty.MainWindowTitle);

            RobotWin32.SetForegroundWindow(iHandle);
        }
Exemplo n.º 14
0
        public void Execute(Arguments arguments)
        {
            Rectangle      rectangle    = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates();
            Bitmap         partOfScreen = RobotWin32.GetPartOfScreen(rectangle);
            int            timeout      = (int)arguments.Timeout.Value.TotalMilliseconds;
            List <string>  languages    = arguments.Languages.Value.Split(',').ToList();
            string         search       = arguments.Search.Value;
            GoogleCloudApi googleApi    = new GoogleCloudApi();
            Rectangle      output       = googleApi.RecognizeText(partOfScreen, search, languages, timeout);

            if (Equals(output, new Rectangle(-1, -1, -1, -1)))
            {
                throw new NullReferenceException("Ocr was unable to find text");
            }
            Scripter.Variables.SetVariableValue(arguments.Result.Value, new RectangleStructure(output));
        }
Exemplo n.º 15
0
        public void Execute(Arguments arguments)
        {
            string host = arguments.Host.Value;
            string port = arguments.Port.Value;
            string pass = arguments.Password.Value;

            if (host == string.Empty || port == string.Empty || pass == string.Empty)
            {
                throw new ApplicationException("Host or port or pass is empty");
            }

            testerApp = System.Diagnostics.Process.Start(pathToVNC, "-Scaling Fit -Encryption Server " + host + " " + port + " " + pass);
            bool result = RobotWin32.ShowWindow(testerApp.MainWindowHandle, RobotWin32.ShowWindowEnum.ShowNormal);

            Scripter.Variables.SetVariableValue(arguments.Result.Value, new BooleanStructure(result));
        }
Exemplo n.º 16
0
        public void Execute(Arguments arguments)
        {
            var screenArea  = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates();
            var screenImage = RobotWin32.GetPartOfScreen(screenArea);
            var timeout     = (int)arguments.Timeout.Value.TotalMilliseconds;
            var languages   = arguments.Languages.Value.Split(',').ToList();
            var googleApi   = new GoogleCloudApi();
            var text        = googleApi.RecognizeText(screenImage, languages, timeout);

            if (string.IsNullOrEmpty(text))
            {
                throw new NullReferenceException("Ocr was unable to find text");
            }

            Scripter.Variables.SetVariableValue(arguments.Result.Value, new TextStructure(text));
        }
        public void Execute(Arguments arguments)
        {
            var screenArea  = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates();
            var screenImage = RobotWin32.GetPartOfScreen(screenArea);
            var timeout     = (int)arguments.Timeout.Value.TotalMilliseconds;
            var languages   = arguments.Languages.Value.Split(',').ToList();
            var search      = arguments.Search.Value;
            var googleApi   = new GoogleCloudApi();
            var rectangle   = googleApi.FindTextPosition(screenImage, search, languages, timeout);

            if (rectangle == null)
            {
                throw new NullReferenceException("Ocr was unable to find text position");
            }

            Scripter.Variables.SetVariableValue(arguments.Result.Value, new RectangleStructure(rectangle.Value));
        }
        public void Execute(Arguments arguments)
        {
            var pathToVNC = Path.Combine(AbstractSettingsContainer.Instance.UserDocsAddonFolder.FullName, @"VNC.exe");
            var host      = arguments.Host.Value;
            var port      = arguments.Port.Value;
            var pass      = arguments.Password.Value;

            if (host == string.Empty || port == string.Empty || pass == string.Empty)
            {
                throw new ApplicationException("Host or port or pass is empty");
            }

            var testerApp = Process.Start(pathToVNC, "-Scaling Fit -Encryption Server " + host + " " + port + " " + pass);
            var result    = RobotWin32.ShowWindow(testerApp.MainWindowHandle, RobotWin32.ShowWindowEnum.ShowNormal);

            Scripter.Variables.SetVariableValue(arguments.Result.Value, new BooleanStructure(result));
        }
        public void Execute(Arguments arguments)
        {
            var rectangle = arguments.Area.Value;

            if (!rectangle.IsValidRectangle())
            {
                throw new ArgumentException("Argument Area is not a valid rectangle");
            }
            if (arguments.Relative.Value)
            {
                var foregroundWindowRect = new RobotWin32.Rect();
                RobotWin32.GetWindowRectangle(RobotWin32.GetForegroundWindow(), ref foregroundWindowRect);
                rectangle = new Rectangle(rectangle.X + foregroundWindowRect.Left,
                                          rectangle.Y + foregroundWindowRect.Top,
                                          Math.Min(rectangle.Width, foregroundWindowRect.Right - foregroundWindowRect.Left) - rectangle.X,
                                          Math.Min(rectangle.Height, foregroundWindowRect.Bottom - foregroundWindowRect.Top) - rectangle.Y);
            }
            var partOfScreen = RobotWin32.GetPartOfScreen(rectangle);
            var imgToParse   = OcrOfflineHelper.RescaleImage(partOfScreen, arguments.Sensitivity.Value);
            var language     = arguments.Language.Value;
            var dataPath     = OcrOfflineHelper.GetResourcesFolder(language);

            try
            {
                using (var tEngine = new TesseractEngine(dataPath, language, EngineMode.TesseractAndCube))
                    using (var img = PixConverter.ToPix(imgToParse))
                        using (var page = tEngine.Process(img))
                        {
                            var text = page.GetText();
                            if (string.IsNullOrEmpty(text))
                            {
                                throw new NullReferenceException("Ocr was unable to find any text");
                            }
                            Scripter.Variables.SetVariableValue(arguments.Result.Value, new Language.TextStructure(text));
                        }
            }
            catch (TesseractException)
            {
                throw new ApplicationException("Ocr engine exception, possibly missing language data in folder: " + dataPath);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 20
0
        public void Execute(Arguments arguments)
        {
            if (arguments.Threshold.Value < 0 || arguments.Threshold.Value > 1)
            {
                throw new ArgumentOutOfRangeException("Threshold must be a value from 0 to 1.");
            }

            using (Bitmap bitmap1 = Imaging.OpenImageFile(arguments.Image1.Value, nameof(arguments.Image1)))
                using (Bitmap bitmap2 = (string.IsNullOrEmpty(arguments.Image2?.Value)) ?
                                        RobotWin32.GetPartOfScreen(
                           Imaging.ParseRectanglePositionFromArguments(arguments.ScreenSearchArea.Value, arguments.Relative.Value),
                           bitmap1.PixelFormat) :
                                        Imaging.OpenImageFile(arguments.Image2.Value, nameof(arguments.Image2)))
                {
                    bool found = Rectangle.Empty != Imaging.IsImageInImage(bitmap1, bitmap2, (double)arguments.Threshold.Value);
                    Scripter.Variables.SetVariableValue(arguments.Result.Value, new BooleanStructure(found));
                }
        }
Exemplo n.º 21
0
        public Rectangle GetRectangle()
        {
            if (AutomationElement.Properties.BoundingRectangle.TryGetValue(out var boundingRectNoDefault))
            {
                return(boundingRectNoDefault);
            }

            if (AutomationElement.FrameworkAutomationElement.NativeWindowHandle != IntPtr.Zero)
            {
                var    rect      = new RobotWin32.Rect();
                IntPtr wndHandle = AutomationElement.FrameworkAutomationElement.NativeWindowHandle;
                if (RobotWin32.GetWindowRectangle(wndHandle, ref rect))
                {
                    return(new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top));
                }
            }
            throw new NotSupportedException("Cannot get rectangle for that kind of UI element.");
        }
Exemplo n.º 22
0
        public void Execute(Arguments arguments)
        {
            var pathToTelnet = Path.Combine(AbstractSettingsContainer.Instance.UserDocsAddonFolder.FullName, @"telnet.exe");
            var pathToPutty  = Path.Combine(AbstractSettingsContainer.Instance.UserDocsAddonFolder.FullName, @"putty.exe");
            var errorJump    = arguments.ErrorJump != null && arguments.ErrorJump != null?arguments.ErrorJump.ToString() : string.Empty;

            var host = arguments.Host.Value;
            //telnet = System.Diagnostics.Process.Start(pathToTelnet, host);
            var putty = Process.Start(pathToPutty, "-load AS400");

            Thread.Sleep(2000);
            //RobotWin32.ShowWindow(telnet.MainWindowHandle, RobotWin32.ShowWindowEnum.ShowNormal);
            //IntPtr iHandle = RobotWin32.FindWindow(null, telnet.MainWindowTitle);
            RobotWin32.ShowWindow(putty.MainWindowHandle, RobotWin32.ShowWindowEnum.ShowNormal);
            var iHandle = RobotWin32.FindWindow(null, putty.MainWindowTitle);

            RobotWin32.SetForegroundWindow(iHandle);
        }
        private void OpenForm(AccessObjectModel formToLoad, bool openInDesigner, MSAccess.Application application)
        {
            try
            {
                var formName = formToLoad.FullName ?? formToLoad.Name;
                application.DoCmd.OpenForm(
                    formName,
                    openInDesigner ? MSAccess.AcFormView.acDesign : MSAccess.AcFormView.acNormal
                    );

                var form = application.Forms[formName];
                form.SetFocus();
                RobotWin32.BringWindowToFront((IntPtr)form.Hwnd);
            }
            catch (Exception ex)
            {
                RobotMessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 24
0
        private void ExecuteCode(AccessFormModel formModel, AccessControlModel control, string code)
        {
            if (code == "[Event Procedure]")
            {
                //form.SetFocus();
                control.SetFocus();

                RobotWin32.SetForegroundWindow((IntPtr)formModel.Hwnd);
                System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            }
            else if (!string.IsNullOrEmpty(code))
            {
                application.DoCmd.RunMacro(code);
            }
            else
            {
                throw new Exception("Code to execute is empty");
            }
        }
Exemplo n.º 25
0
        public static Process StartFormTester(string arguments)
        {
            string location = null;//TODO typeof(Robot.FormTester.G1ANTRobotFormTester).Assembly.Location;

            var process = Process.Start(location, arguments);

            while (process.MainWindowHandle == IntPtr.Zero)
            {
                System.Threading.Thread.Sleep(20);
            }
            process.WaitForInputIdle();
            Thread.Sleep(5000);
            while (IsIconic(process.MainWindowHandle))
            {
                RobotWin32.ShowWindow(process.MainWindowHandle, RobotWin32.ShowWindowEnum.Show);
            }
            process.WaitForInputIdle();
            return(process);
        }
        public void RelativeTest()
        {
            IntPtr hTesterAppWindow = testerApp.MainWindowHandle;

            RobotWin32.Rect windowRect = new RobotWin32.Rect();
            RobotWin32.GetWindowRectangle(hTesterAppWindow, ref windowRect);
            int titleBarHeight = 24;

            scripter.Text = ($"ocrabbyy.processscreen area {SpecialChars.Text}0,0,{windowRect.Right - windowRect.Left},{titleBarHeight}{SpecialChars.Text} relative true");
            scripter.Run();
            int documentId = scripter.Variables.GetVariableValue <int>("result");

            FineReaderDocument document = AbbyyManager.Instance.GetDocument(documentId);

            Assert.IsNotNull(document);
            string plainText = document.GetAllText();

            Assert.IsTrue(plainText.Contains(appTitle));
        }
Exemplo n.º 27
0
        public System.Windows.Rect GetRectangle()
        {
            object boundingRectNoDefault =
                automationElement.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty, true);

            if (boundingRectNoDefault != AutomationElement.NotSupported)
            {
                return((System.Windows.Rect)boundingRectNoDefault);
            }
            else if (automationElement.Current.NativeWindowHandle != 0)
            {
                RobotWin32.Rect rect      = new RobotWin32.Rect();
                IntPtr          wndHandle = new IntPtr(automationElement.Current.NativeWindowHandle);
                if (RobotWin32.GetWindowRectangle(wndHandle, ref rect))
                {
                    return(new System.Windows.Rect(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top));
                }
            }
            throw new NotSupportedException("Cannot get rectangle for that kind of UI element.");
        }
        public void FindWindowTitleTest()
        {
            string   appTitle = "TestApp";
            Scripter scripter = new Scripter();

            scripter.InitVariables.Clear();
            System.Diagnostics.Process testerApp       = null;
            List <GStruct.Structure>   windowTitleRect = null;

            try
            {
                scripter = new Scripter();
                scripter.InitVariables.Clear();
                testerApp = AbbyTests.StartFormTester($"Title {appTitle}");
                IntPtr          hTesterAppWindow = testerApp.MainWindowHandle;
                RobotWin32.Rect windowRect       = new RobotWin32.Rect();
                RobotWin32.GetWindowRectangle(hTesterAppWindow, ref windowRect);
                int titleBarHeight = 24;

                scripter.Text = ($@"ocrabbyy.processscreen area {SpecialChars.Text}{windowRect.Left},{windowRect.Top},{windowRect.Right},{windowRect.Bottom}{SpecialChars.Text}
                                    ocrabbyy.find {SpecialChars.Text}{appTitle}{SpecialChars.Text} result {nameof(windowTitleRect)}");
                scripter.Run();
                windowTitleRect = scripter.Variables.GetVariableValue <List <GStruct.Structure> >(nameof(windowTitleRect));
                Assert.AreNotEqual(0, windowTitleRect.Count);
                System.Drawing.Rectangle titleRect = ((GStruct.RectangleStructure)windowTitleRect[0]).Value;

                Assert.IsTrue(titleRect.Top > 0 & titleRect.Top < titleBarHeight, "Top edge position of found rectangle is incorrect");
                Assert.IsTrue(titleRect.Bottom > 0 & titleRect.Bottom <= titleBarHeight, "Bottom edge position of found rectangle is incorrect");
                Assert.IsTrue(titleRect.Left > 0 & titleRect.Left < 50, "Left edge position of found rectangle is incorrect");
                Assert.IsTrue(titleRect.Right > 0 & titleRect.Right < windowRect.Right);
            }
            finally
            {
                if (testerApp != null && !testerApp.HasExited)
                {
                    testerApp.Kill();
                }
            }
        }
        public void Execute(Arguments arguments)
        {
            if (arguments.Threshold.Value < 0 || arguments.Threshold.Value > 1)
            {
                throw new ArgumentOutOfRangeException("Threshold must be a value from 0 to 1.");
            }

            using (Bitmap bitmap1 = Imaging.OpenImageFile(arguments.Image.Value, nameof(arguments.Image)))
            {
                int       timeout        = (int)arguments.Timeout.Value.TotalMilliseconds;
                long      start          = Environment.TickCount;
                Rectangle foundRectangle = Rectangle.Empty;
                while (Math.Abs(Environment.TickCount - start) < timeout && Scripter.Stopped == false && foundRectangle == Rectangle.Empty)
                {
                    using (
                        Bitmap bitmap2 = RobotWin32.GetPartOfScreen(
                            Imaging.ParseRectanglePositionFromArguments(arguments.ScreenSearchArea.Value, arguments.Relative.Value),
                            bitmap1.PixelFormat))
                    {
                        foundRectangle = Imaging.IsImageInImage(bitmap1, bitmap2, (double)arguments.Threshold.Value);
                        Application.DoEvents();
                    }
                }

                if (foundRectangle == Rectangle.Empty)
                {
                    throw new TimeoutException("Image was not found in specified search area.");
                }
                else
                {
                    Point foundPoint = (!arguments.CenterResult.Value) ?
                                       new Point(foundRectangle.X, foundRectangle.Y) :
                                       new Point(foundRectangle.X + foundRectangle.Width / 2, foundRectangle.Y + foundRectangle.Height / 2);
                    foundPoint = new Point(foundPoint.X + arguments.OffsetX.Value, foundPoint.Y + arguments.OffsetY.Value);
                    Scripter.Variables.SetVariableValue(arguments.Result.Value, new PointStructure(foundPoint));
                }
            }
        }
        public static Bitmap GetScreenshot(this RectangleStructure screenArea, BooleanStructure relative)
        {
            if (screenArea.Value.Width < 1 || screenArea.Value.Height < 1)
            {
                throw new ArgumentException("ScreenSearchArea argument's parts can't be negative. Both width and height must be bigger than zero.");
            }

            try
            {
                var screenSearchArea = screenArea.Value;

                if (relative.Value)
                {
                    screenSearchArea = screenArea.Value.GetRelativeArea();
                }

                return(RobotWin32.GetPartOfScreen(screenSearchArea, PixelFormat.Format24bppRgb));
            }
            catch (Exception ex)
            {
                throw new Exception($"Could not get the screenshot image. Message: {ex.Message}", ex);
            }
        }