/// <summary> /// Finds any child window from specified that has a text value equals to specified. /// </summary> public static IWindowControl GetWindowControlByText(IWindowControl mainControl, string controlText) { object controlValue = mainControl.GetControlValue(); string text = controlValue.ToString(); if (text.Equals(controlText)) { return(mainControl); } IWindowControl foundControl = null; IEnumerable <IWindowControl> childs = mainControl.GetChildControls(); foreach (IWindowControl child in childs) { foundControl = GetWindowControlByText(child, controlText); if (foundControl != null) { controlValue = foundControl.GetControlValue(); text = controlValue.ToString(); if (text.Equals(controlText)) { break; } } } return(foundControl); }
static void Main(string[] args) { IntPtr windowHandle = NativeMethodsHelper.GetWindowByTitle("Test Form"); /* * Test sub-controls of a window. */ IWindowControlHandler windowsControlHandler = new WindowControlHandler(null); IWindowControl window = windowsControlHandler.GetWindowControl(windowHandle); IEnumerable <IWindowControl> childs = window.GetChildControls(); object windowTitle = window.GetControlValue(); Console.WriteLine($"Window title: { windowTitle }"); Console.WriteLine($"Child controls values:"); for (int i = 0; i < childs.Count(); ++i) { IWindowControl child = childs.ElementAt(i); object childControlValue = child.GetControlValue(); Console.WriteLine($"{ i } - { childControlValue }"); // Test button clicking. if (childControlValue.Equals("Test Button Text")) { child.Click(); } } for (int i = 0; i < childs.Count(); ++i) { IEnumerable <IWindowControl> childChilds = childs.ElementAt(i).GetChildControls(); Console.WriteLine(""); } }
private string FindCurrentPlayerChar() { IWindowControl mainControl = new WindowControl(this.WindowPtr, null); IWindowControl playerCharGroupBox = NativeMethodsHelper.GetWindowControlByText(mainControl, "Current Player Turn"); IWindowControl charControl = playerCharGroupBox.GetChildControls().FirstOrDefault(); string tttChar = charControl.GetControlValue().ToString(); return(tttChar); }
protected bool CanMakeMove() { IWindowControl mainControl = this.LearningModule.Bot.ConnectionHandler.ConnectedProcesses.ElementAt(0).WindowHandle.ToWindowControl(); IWindowControl playerCharGroupBox = NativeMethodsHelper.GetWindowControlByText(mainControl, TttSettings.CURRENT_PLAYER_TURN); IWindowControl charControl = playerCharGroupBox.GetChildControls().FirstOrDefault(); string tttChar = charControl.GetControlValue().ToString(); return(this.TttChar.Equals(tttChar)); }
private IEnumerable <IWindowControl> FindButtons() { IConnectedProcess gameProcess = this.GetGameProcess(); IntPtr windowHandler = gameProcess.WindowHandle; IWindowControl mainWindowControl = windowHandler.ToWindowControl(); IWindowControl buttonsGroupBox = NativeMethodsHelper.GetWindowControlByText(mainWindowControl, TttSettings.BUTTONS); IEnumerable <IWindowControl> buttons = buttonsGroupBox.GetChildControls(); return(buttons); }
static void PrintTexts(IEnumerable <IWindowControl> controls, int level) { for (int i = 0; i < controls.Count(); ++i) { IWindowControl groupBox = controls.ElementAt(i); object groupBoxValue = groupBox.GetControlValue(); string groupBoxText = groupBoxValue.ToString(); StringBuilder builder = new StringBuilder(); for (int j = 0; j < level; ++j) { builder.Append("-"); } Console.WriteLine($"{ i }. { builder.ToString() } { groupBoxText }"); PrintTexts(groupBox.GetChildControls(), level + 1); } }
static void Main(string[] args) { Process notepadProcess1 = StartNotepad(); Process notepadProcess2 = StartNotepad(); // Create connection handler and connection tools for Windows with double-side binding. var connectionHandler = new WindowsConnectionHandler(null); connectionHandler.AttachToProcess(notepadProcess1, IntPtr.Zero); connectionHandler.AttachToProcess(notepadProcess2, IntPtr.Zero); // Print all connected processes for (int i = 0; i < connectionHandler.ConnectedProcesses.Count; ++i) { IConnectedProcess connectedProcess = connectionHandler.ConnectedProcesses.ElementAt(i); Process process = connectedProcess.Process; Console.WriteLine($"{ i }. { process.ProcessName }, ProcessId: { process.Id }"); } Console.WriteLine(); // // Test - Return value from specified control. Use only in-AutoBot relations. // IWindowControlHandler windowsControlHandler = new WindowControlHandler(connectionHandler.PlatformConnectionTools); IConnectedProcess notepad1 = windowsControlHandler.PlatformConnectionTools.ConnectionHandler.ConnectedProcesses.ElementAt(0); IWindowControl windowsControl = windowsControlHandler.GetWindowControl(notepad1.Process.MainWindowHandle); IEnumerable <IWindowControl> childs = windowsControl.GetChildControls(); object controlValue = windowsControl.GetControlValue(); Console.WriteLine($"Control value: { controlValue }"); Console.WriteLine($"Child controls values:"); for (int i = 0; i < childs.Count(); ++i) { Console.WriteLine($"{ i } - { childs.ElementAt(i).GetControlValue() }"); } Console.ReadKey(); }
static void Main(string[] args) { IntPtr windowPtr = NativeMethodsHelper.GetWindowByTitle("TicTacToe"); // Find TextBox with current player char IWindowControl windowControl = new WindowControl(windowPtr, null); IEnumerable <IWindowControl> groupBoxes = windowControl.GetChildControls(); PrintTexts(groupBoxes, 1); IWindowControl control = NativeMethodsHelper.GetWindowControlByText(windowControl, "Current Player Turn"); string text = control.GetControlValue().ToString(); Console.WriteLine(text); // Check current player char IWindowControl charControl = control.GetChildControls().FirstOrDefault(); string tttChar = charControl.GetControlValue().ToString(); Console.WriteLine(tttChar); Console.ReadKey(); }