public void spy()
        {
            var curtime = DateTime.Now;

            try
            {
                log.Info("BEGIN SPY FROM HIGHLIGHT FORM");
                Process targetProcess = WindowInteraction.GetProcess(ProcessName);
                //WindowInteraction.FocusWindow(targetProcess);
                App         = Gu.Wpf.UiAutomation.Application.Attach(targetProcess.Id);
                MainWindow  = App.MainWindow;
                ElementList = MainWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.FrameworkIdProperty, "WPF"));
                dataGridView1.Rows.Clear();
                dataGridView1.AllowUserToAddRows = true;

                SpyObjectList = new SpyObject[ElementList.Count];
                int SpyObjectIndex = 0;

                //comboBox1.Enabled = false;

                for (int i = 0; i < ElementList.Count; i++)
                {
                    SpyObjectList[SpyObjectIndex]       = new SpyObject();
                    SpyObjectList[SpyObjectIndex].index = SpyObjectIndex;
                    if (ElementList[i].AutomationId == "" && SpyObjectIndex - 1 > 0 && ElementList[i - 1].Name != "" && ElementList[i].Name == "")
                    {
                        SpyObjectList[SpyObjectIndex].automationId = (ElementList[i - 1].Name + "_" + ElementList[i].ClassName).Replace(" ", "_").Replace(":", "");
                    }
                    else
                    {
                        SpyObjectList[SpyObjectIndex].automationId = ElementList[i].AutomationId;
                    }
                    SpyObjectList[SpyObjectIndex].name = ElementList[i].Name;
                    SpyObjectList[SpyObjectIndex].type = ElementList[i].ClassName;
                    DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[i].Clone();
                    row.Cells[0].Value = SpyObjectList[SpyObjectIndex].index;
                    row.Cells[1].Value = SpyObjectList[SpyObjectIndex].automationId;
                    row.Cells[2].Value = SpyObjectList[SpyObjectIndex].name;
                    row.Cells[3].Value = SpyObjectList[SpyObjectIndex].type;
                    dataGridView1.Rows.Add(row);
                    SpyObjectIndex++;
                }
                log.Info("SPY DONE OF HIGHLIGHT");



                dataGridView1.AllowUserToAddRows = false;
            }
            catch (Exception ex)
            {
                log.Error("ERROR CODE: " + ex.HResult + "  -----  " + "detail: " + ex.Message);
            }
        }
        public void highlight(int objectID)
        {
            var curtime = DateTime.Now;

            try
            {
                log.Info("BEGIN HIGHLIGHT");
                Process targetProcess = WindowInteraction.GetProcess(ProcessName);

                App           = Gu.Wpf.UiAutomation.Application.Attach(targetProcess.Id);
                MainWindow    = App.MainWindow;
                ElementList   = MainWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.FrameworkIdProperty, "WPF"));
                SpyObjectList = new SpyObject[ElementList.Count];
                int SpyObjectIndex = 0;
                for (int i = 0; i < ElementList.Count; i++)
                {
                    SpyObjectList[SpyObjectIndex]       = new SpyObject();
                    SpyObjectList[SpyObjectIndex].index = SpyObjectIndex;
                    if (ElementList[i].AutomationId == "" && SpyObjectIndex - 1 > 0 && ElementList[i - 1].Name != "" && ElementList[i].Name == "")
                    {
                        SpyObjectList[SpyObjectIndex].automationId = (ElementList[i - 1].Name + "_" + ElementList[i].ClassName).Replace(" ", "_").Replace(":", "");
                    }
                    else
                    {
                        SpyObjectList[SpyObjectIndex].automationId = ElementList[i].AutomationId;
                    }
                    SpyObjectList[SpyObjectIndex].name = ElementList[i].Name;
                    SpyObjectList[SpyObjectIndex].type = ElementList[i].ClassName;


                    if (SpyObjectList[SpyObjectIndex].index == objectID)
                    {
                        ElementList[SpyObjectList[SpyObjectIndex].index].DrawHighlight(true, Color.Red, TimeSpan.FromSeconds(2));
                    }

                    SpyObjectIndex++;
                }
                log.Info("DONE HIGHLIGHT");
            }
            catch (Exception ex)
            {
                log.Error("ERROR CODE: " + ex.HResult + "  -----  " + "detail: " + ex.Message);
            }
        }
 public IReadOnlyList <UiElement> ElementClass(string type)
 {
     return(MainWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, type)));
 }
        public static void DoCapture(string Proc)
        {
            var curtime = DateTime.Now;

            var path      = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var imagePath = path + @"\Botsina\SpyObjects\";

            System.IO.FileInfo file = new System.IO.FileInfo(imagePath);

            if (file.Directory.Exists)
            {
                DirectoryInfo dir = new DirectoryInfo(imagePath);

                foreach (FileInfo fi in dir.GetFiles())
                {
                    fi.Delete();
                }
            }
            else
            {
                file.Directory.Create();
            }

            try
            {
                //this.Hide();

                log.Info("BEGIN CAPTURE TO FILE");

                file.Directory.Create();

                Process targetProcess = WindowInteraction.GetProcess(Proc);

                App           = Gu.Wpf.UiAutomation.Application.Attach(targetProcess.Id);
                MainWindow    = App.MainWindow;
                ElementList   = MainWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.FrameworkIdProperty, "WPF"));
                SpyObjectList = new SpyObject[ElementList.Count];
                int SpyObjectIndex = 0;

                SendKeys.Send("{PRTSC}");

                Image img = Clipboard.GetImage();

                for (int i = 0; i < ElementList.Count; i++)
                {
                    SpyObjectList[SpyObjectIndex]       = new SpyObject();
                    SpyObjectList[SpyObjectIndex].index = SpyObjectIndex;
                    if (ElementList[i].AutomationId == "" && SpyObjectIndex - 1 > 0 && ElementList[i - 1].Name != "" && ElementList[i].Name == "")
                    {
                        SpyObjectList[SpyObjectIndex].automationId = (ElementList[i - 1].Name + "_" + ElementList[i].ClassName).Replace(" ", "_").Replace(":", "");
                    }
                    else
                    {
                        SpyObjectList[SpyObjectIndex].automationId = ElementList[i].AutomationId;
                    }
                    SpyObjectList[SpyObjectIndex].name = ElementList[i].Name;
                    SpyObjectList[SpyObjectIndex].type = ElementList[i].ClassName;

                    if ((SpyObjectList[SpyObjectIndex].type == "ComboBox" ||
                         SpyObjectList[SpyObjectIndex].type == "ComboBoxEdit" ||
                         SpyObjectList[SpyObjectIndex].type == "DataGrid" ||
                         SpyObjectList[SpyObjectIndex].type == "TextBox" ||
                         SpyObjectList[SpyObjectIndex].type == "Button" ||
                         SpyObjectList[SpyObjectIndex].type == "RadioButton" ||
                         SpyObjectList[SpyObjectIndex].type == "AutoCompleteCombobox")

                        && SpyObjectList[SpyObjectIndex].automationId.Contains("PART") != true)
                    {
                        if (ElementList[SpyObjectIndex].Bounds.IsEmpty == false && ElementList[SpyObjectIndex].IsOffscreen == false)
                        {
                            ElementList[SpyObjectIndex].CaptureToFile(imagePath + SpyObjectIndex + ".png");
                            log.Info(ElementList[SpyObjectIndex].ClassName);
                        }
                    }

                    SpyObjectIndex++;
                }
                img.Save(imagePath + "thisScreen" + ".png");
                log.Info("DONE CAPTURE TO FILE");

                //this.Show();
            }
            catch (Exception ex)
            {
                log.Error("ERROR CODE: " + ex.HResult + "  -----  " + "detail: " + ex.Message);
                //this.Show();
            }
        }
        // SPY FUNCTION
        public void Spy()
        {
            IntPtr hWnd = flexProc[0].MainWindowHandle;
            if (hWnd != IntPtr.Zero)
            {
                SetForegroundWindow(hWnd);
                ShowWindow(hWnd, SW_MAXIMIZE);
            }
            App = Gu.Wpf.UiAutomation.Application.Attach(flexProc[0].Id);
            MainWindow = App.MainWindow;

            Element = MainWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.FrameworkIdProperty, "WPF"));
            try
            {

                //Clear SpyIMG files
                deletefiles();

                var curtime = DateTime.Now;
                var day = curtime.Day;
                var month = curtime.Month;
                var year = curtime.Year;

                var sec = curtime.Second;
                var hour = curtime.Hour;
                var minute = curtime.Minute;
                var longTimeString = DateTime.Now.ToOADate();


                var reformat = day + "-" + month + "-" + year + "__" + hour + "-" + minute + "-" + sec + "-";


                using (System.IO.StreamWriter file =
                new System.IO.StreamWriter(@"C:\Users\" + SR.username + @"\Documents\GUWPF\" + reformat + "SpyResult.json"))
                {
                    this.WindowState = FormWindowState.Minimized;
                    Thread.Sleep(500);
                    SR.currenttime = longTimeString.ToString();

                    id = 0;
                    foreach (UiElement UIE in Element)
                    {
                        if (!UIE.Bounds.IsEmpty)
                        {
                            SR.index = id;
                            SR.autoDI = UIE.AutomationId;
                            SR.classname = UIE.ClassName;
                            SR.name = UIE.Name;
                            SR.directory = @"C:\Users\" + SR.username + @"\Documents\GUWPF\SpyIMG\" + SR.index + " - " + SR.classname + ".png";
                            //SR.currenttime = curtime.ToString();


                            if (UIE.AutomationId == "")
                                SR.autoDI = "No AutomationID";
                            if (UIE.Name == "")
                                SR.name = "No Name";

                            UIE.CaptureToFile(@"C:\Users\" + SR.username + @"\Documents\GUWPF\SpyIMG\" + SR.index + " - " + SR.classname + ".png");
                            id++;



                            listBox1.Items.Add("ID: " + SR.index + " - " + SR.autoDI + " - " + SR.classname + " - " + SR.name);
                            string ObjectUI = JsonConvert.SerializeObject(SR, Newtonsoft.Json.Formatting.Indented);

                            if (SR.index == 0)
                            {
                                file.WriteLine("[");
                                file.WriteLine(ObjectUI + ",");
                            }
                            else if (SR.index == Element.Count - 1)
                            {
                                file.WriteLine(ObjectUI);
                                file.WriteLine("]");
                            }

                            else file.WriteLine(ObjectUI + ",");

                        }

                    }
                    file.WriteLine("]");
                    this.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception err)
            {
                listBox1.Items.Add(id + " - " + SR.classname + " - " + err.Message);
            }

        }